Read / Write files

This tutorial will demonstrate how to read in a file and also output the details to another file. The headers
fstream and iostream
basically include details on File-streaming (fstream) and In-Output-Streaming (iostream).

I have attached the code within a zip file with the required input file as well.

The namespace denotes which area are you in, for example : maths:algebra would mean you are doing maths and with the topic of Algebra, within the c/c++ example std::ifstream means that the namespace is std and the function/variable is ifstream.

NOTE: the { } are the begin and end of a code structure, e.g. While begin; do something; end;


using namespace std;// use the namespace (e.g. instead of putting std::ifstream you just need to put fstream.)

int main(void)
char InStr[100];
ifstream InFile("countrys.txt",ios::in);// infile
ofstream OutFile("outcountrys.txt", ios::out);// outfile

while (InFile.getline(InStr,100))// while reading each line within the file
printf("%s\n", InStr);// output the read line to the console
OutFile <<

Leave a Reply

Your email address will not be published. Required fields are marked *