BASICS OF THE C++ STANDARD FILE STREAM LIBRARY. For some sample code that does different things with files (opens them and then, depending on whether the file was opened for input or output, reads or writes to them) see my class page http://www.ccs.neu.edu/home/sbratus/com1100/ The whole science about files can be summarized as follows. To use files, you need to #include at the top of your program. We want to be able to treat files the same way we treat cin and cout -- as streams of characters which we read or write one by one or in groups, when we want to read in or write out a number or a word. In the latter case the characters are still scanned in or written out one by one, only the standard library software hides the necessary actions from us. There is a difference, though: cin and cout are streams that get created (and linked to keyboard and terminal window) by default, whereas the streams for files must be exlicitly (1) declared, so that they get created and (2) linked to the file with the name we provide. Of course, the files are known to the operating system by their names, which are strings from the point of view of a C++ program. So, let's say we need to read from a file called "mytext.txt". We must create a stream that reads characters from our file one by one (or in groups, until whitespace is encountered). We must give this stream a name, the same way as we need to name any variable. ifsteram f1 ; // create a stream called f1 // 'i' in ifstream stands for input, 'f' for file f1 is still an empty stream at this point. Reading from it now won't yield any useful results. We must link it to out file, using the file's name: f1.open( "mytext.txt" ); // this assumes that mytext.txt is in the same // folder with your executable file We must check if the operation was successful, so if ( ! f1 ) { cout << "Error opening file!\n"; exit(-1); // exits the program right away, almost the same as return. } Now we can read from f1 the characters from "myfile.txt" (and forget about the actual name of the file): Let's say that the file contains 100 integers, separated either by spaces or newlines or both. Then this code will add them all up: int x, sum=0; for(int i=0; i<100; i++){ f1 >> x; // read an integer from f1 (="myfile.txt"), put in x sum += x; } Notice that f1 is used the same way as cin, only now the input comes from a file, not the keyboard. Another task: read characters from file until the file is over, and count (1) the number of spaces (2) the number of all characters. For this we need a function that reads just one character from a file. A bad thing can happen -- since the file ends somewhere, we can run out of characters, come to the end of the stream. For this reason the function bool get( char& ) that reads from the file fills it argument (passed by reference, as you must have noticed) with the new character if there is a new character to be read, and returns true, OR returns false if there are no more chars. So the code reads: int space_count = 0, total_count = 0; char ch; // will receive new chars on each iteration while ( f1.get( ch ) ){ // false if ch was not filled, true if it was total_count++; if( ch == ' ') space_count++; } // go get next character Note that if the file is at an end, get returns false, and the loop boby isn't entered. By the same token, if you want to print to a file, you have to (1) create a stream like cout (2) link if with a file by the name . ofstream f2; // 'o' for output, 'f' for file f2.open( "mylog.txt" ); if( ! f2 ) { cout << "Error opening file!\n"; exit(-1); // exits the program right away, almost the same as return. } Then use f2 the same way you would use cout, i.e. do things like f2 << "Hello world"; f2 << 25*25 << endl; //will print 625 (three characters) into the file //followed by a newline f2 << char( '1' + 2 ); //will print character '3' into the file etc. NOTE: By default, if the file you open for output exists, its contents are overwritten. If the file dosn't exist as yet, it is created. These behaviors can be modified by providing additional arguments for open( "filename", ... ) . When you are done with your file, you can close it (dissociate the stream from the physical file on your disk): f1.close(); f2.close(); When your program finishes, all open files will be closed automatically. DIRECTORIES / FOLDERS The file you want to read from or write to may not always lie in the same directory as your executable file. In that case you need to specify the PATH, i.e. the sequence of folders that leads to the file, in your string argument to open . For example, if I want to read the file my_novel.txt that is on my Mac (called Mudd) in the folder named Books on my desktop, I have to say: ifstream f1; f1.open( "Mudd:Desktop Folder:Books:my_novel.txt" ); On a PC the separator between the directories is \ , on unix / . On a PC you must also specify the drive letter ( c:\ or d:\ etc.) This string can be put together automatically by a file browser dialog box. See next section. CORE TOOLS LIBRARY : FILE TOOLS bool SelectOldFileName( string& ) will open a file browser dialog box and let you pick the file by navigating to it. When you press OK, the function fills in the string it gets as an argument, and returns true. If you canceled navigation by clicking CANCEL, it returns false. Here's how to find out what file names on your Mac look like. bool status; string s1; status = SelectOldFileName( s1 ); if( status ) { cout << "You picked : " << s1 << endl; //print the full name of the file // ... do some work } else cout << "You clicked CANCEL. No file was chosen.\n"; A WORD OF WARNING: SelectOldFileName takes a string, a C++ class object. File open function requires that the name of the file is the OLD type string, not a class string, but a zero-terminated array of char . Therefore you CANNOT say ifstream f3; f3.open( s1 ); // doesn't work because s1 is a string, not a char[] . You either need to use the CoreTools own OpenFile function, or convert s1 to the old style array of chars: OpenFile( f3, s1 ); // this works OR f3.open( s1.c_str() ); // c_str() makes the old style char array // out of the contents of s1 . In fact, OpenFile does almost the same, and only hides it from you.