Intro to file input/file output in C++
Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Intro to file input/file output in C++

  1. #1
    Senior Member
    Join Date
    May 2002
    Posts
    344

    Intro to file input/file output in C++

    Hey guys, this is my second tutorial, so dont expect something insanely good. You have to download it and unzip the file fstream.doc. I wrote it in Microsoft Word and it is about 6 pages long (it has a bunch of examples and other stuff in it). Also, i would recommend that you turn off autoformatting so Microsoft Word doesnt start to move the example code around. Also, turn off spelling and grammar check unless you want a lot of funky red and green lines. Hope you like it!! Also, please email me at wdpballa1@aol.com if you found a bug in the code or if i made an error somewhere.
    Support your right to arm bears.


    ^^This was the first video game which i played on an old win3.1 box

  2. #2
    er0k
    Guest
    very nice.. this is a great follow up to your intro to pointers..

  3. #3
    Senior Member
    Join Date
    May 2003
    Posts
    207
    hehe... very nicely done man! supreme work

  4. #4
    Senior Member
    Join Date
    May 2002
    Posts
    344
    thank you very much i appriciate it
    Support your right to arm bears.


    ^^This was the first video game which i played on an old win3.1 box

  5. #5
    Senior Member
    Join Date
    Jul 2002
    Posts
    117
    White,

    Read both your tuts and I must say they are excellently written. Even though I'm just recently getting back into the whole C scene, I could follow along with ease... even with out any C++ experience. Nice work...

  6. #6
    Banned
    Join Date
    Mar 2002
    Posts
    594
    As I was going through the tutorial, freshening up my fstream knowledge, I came to an error in one of your programs and a warning in another... thought I'd let you know so you could fix it...

    Your code:

    Code:
    #include <iostream>
    #include <fstream>//includes ifstream class for file  
    		      //input and ofstream class for file output
    using namespace std;
    
    int main ()
    {
    	ifstream fin; //creates ifstream object called fin
    	
    	fin.open("filename3.txt"); //opens "filename.txt"
    	if (!fin.is_open()) //if you cannot open the file…
    	{
    		cerr << "Error 1--Cannot find or open file "filename3.txt";
    		fin.clear(); //reset failbit 
    		return 0;
    	}
    	
    	else
    	{
    		cout << "This is what is inside of filename3.txt: \n";
    		
    		char ch[80];
    		while(fin.get(ch[80])) //reads characters from the file
    			cout << ch[80]; //prints characters to the screen
    		
    		fin.close();
    
    		return 0;
    	}
    }
    Fixed:

    Code:
    #include <iostream>
    #include <fstream>//includes ifstream class for file  
    		      //input and ofstream class for file output
    using namespace std;
    
    int main ()
    {
    	ifstream fin; //creates ifstream object called fin
    	
    	fin.open("filename3.txt"); //opens "filename.txt"
    	if (!fin.is_open()) //if you cannot open the file…
    	{
    		cerr << "Error 1--Cannot find or open file 'filename3.txt'";
    		fin.clear(); //reset failbit 
    		return 0;
    	}
    	
    	else
    	{
    		cout << "This is what is inside of filename3.txt: \n";
    		
    		char ch[80];
    		while(fin.get(ch[80])) //reads characters from the file
    			cout << ch[80]; //prints characters to the screen
    		
    		fin.close();
    
    		return 0;
    	}
    }
    And then in your final program (very nicely written), there isn't a big error but its a warning so I thought I drop it in here:

    Your code:

    Code:
    #include <iostream>
    #include <fstream>
    #include <cstdlib>
    
    using namespace std;
    
    const char *fileA="bat1.txt";
    const char *fileB="bat2.txt";
    const char *fileC="bat3.txt";
    
    int main (void)
    {
       char ch, ch1, c[80]; //sorry for bad variable names
    	
    	ifstream fin, fin1, fin2; //creates 3 ifstream objects
    	ofstream fout; //creates 1 fout object
    	
    	fin.open(fileA); //opens "bat1.txt" for reading
    	
    	//checks to see if "bat1.txt" can be open
    	if (!fin.is_open())
    	{
    		cerr << "Couldn't open file " << fileA << " \n";
    		return 0;
    	}
    	
    	fin1.open(fileB); //opens "bat2.txt" for reading
    	
    	//checks to see if "bat2.txt" can be opened
    	if (!fin1.is_open())
    	{
    		cerr << "Couldn't open file " << fileB << " \n";
    		return 0;
    	}
    	
    	if (fin.is_open() && fin1.is_open())
    	{
    	   fout.open(fileC); //opens "bat3.txt" writing
    	   while (fin && fin)
    	   {
    	      while ((fin.get(ch)) && ch != '\n')
    	      {
    	         fout << ch; //writes content of "bat1.txt"
    	         		  //to "bat3.txt"
    	      }
    	      while ((fin1.get(ch1)) && (ch1 != '\n'))
    	      {
    	         fout << ch1; //writes content of "bat2.txt"
    	         		   //to "bat3.txt"
    	      }
    	      fout << endl;
    	   }
    	   fout.close(); //closes "bat3.txt" for writing
    	   
    	cout << "Finished coping " << fileA << " and ";
    	cout << fileB << " to " << fileC << endl << endl;
    	
    	cout << "This is what is inside of " << fileC << ":\n";
    	
    	fin2.open(fileC); //opens "bat3.txt" for reading
    	
    	while(fin2.get(c[80])) //reads characters from the file
    		cout << c[80]; //prints characters to the screen
    	}
    	
    
    	
    	fin.close(); //closes "bat1.txt" for writing
    	fin1.close(); //closes "bat2.txt" for writing
    	fin2.close(); //closes "bat3.txt" for writing	
    
            // Missing a return statement... 
    }
    Fixed:

    Code:
    #include <iostream>
    #include <fstream>
    #include <cstdlib>
    
    using namespace std;
    
    const char *fileA="bat1.txt";
    const char *fileB="bat2.txt";
    const char *fileC="bat3.txt";
    
    int main (void)
    {
       char ch, ch1, c[80]; //sorry for bad variable names
    	
    	ifstream fin, fin1, fin2; //creates 3 ifstream objects
    	ofstream fout; //creates 1 fout object
    	
    	fin.open(fileA); //opens "bat1.txt" for reading
    	
    	//checks to see if "bat1.txt" can be open
    	if (!fin.is_open())
    	{
    		cerr << "Couldn't open file " << fileA << " \n";
    		return 0;
    	}
    	
    	fin1.open(fileB); //opens "bat2.txt" for reading
    	
    	//checks to see if "bat2.txt" can be opened
    	if (!fin1.is_open())
    	{
    		cerr << "Couldn't open file " << fileB << " \n";
    		return 0;
    	}
    	
    	if (fin.is_open() && fin1.is_open())
    	{
    	   fout.open(fileC); //opens "bat3.txt" writing
    	   while (fin && fin)
    	   {
    	      while ((fin.get(ch)) && ch != '\n')
    	      {
    	         fout << ch; //writes content of "bat1.txt"
    	         		  //to "bat3.txt"
    	      }
    	      while ((fin1.get(ch1)) && (ch1 != '\n'))
    	      {
    	         fout << ch1; //writes content of "bat2.txt"
    	         		   //to "bat3.txt"
    	      }
    	      fout << endl;
    	   }
    	   fout.close(); //closes "bat3.txt" for writing
    	   
    	cout << "Finished coping " << fileA << " and ";
    	cout << fileB << " to " << fileC << endl << endl;
    	
    	cout << "This is what is inside of " << fileC << ":\n";
    	
    	fin2.open(fileC); //opens "bat3.txt" for reading
    	
    	while(fin2.get(c[80])) //reads characters from the file
    		cout << c[80]; //prints characters to the screen
    	}
    	
    
    	
    	fin.close(); //closes "bat1.txt" for writing
    	fin1.close(); //closes "bat2.txt" for writing
    	fin2.close(); //closes "bat3.txt" for writing	
    
            return 0;
    }
    Anyways, very nicely written tutorial, not confusing at all and covers everything thats needed... maybe for your next tutorial you should do something like writing a header file or etc...

    jag291

  7. #7
    Senior Member
    Join Date
    May 2002
    Posts
    344
    thanks for the error spotting. I actually save all of my files the comiler creates as .bat files, so i had to redo the whole code to make it .txt files. Then, i realized while i was working in Microsoft word that my comments were still using the .bat extension, so i had to change them all around. The first error you pointed out is actually an important error, and i very much appoligize. It was around 11 pm and i was making some minor corrections not in my IDE but in Mircosoft word cause i didnt want to recompile everything. Anyways, thanks again for pointing these errors out, i will defiently remember them for next time
    Support your right to arm bears.


    ^^This was the first video game which i played on an old win3.1 box

  8. #8
    Junior Member
    Join Date
    Sep 2003
    Posts
    4
    besides the errors, very nice tutorial white...good work
    If there isn\'t a path, then make a path

  9. #9
    AO Antique pwaring's Avatar
    Join Date
    Aug 2001
    Posts
    1,409
    Excellent tutorial, the only criticism I could possibly make is the use of 'using namespace std' (which I personally dislike, but a lot of people seem to use).
    Paul Waring - Web site design and development.

  10. #10
    Senior Member
    Join Date
    May 2002
    Posts
    344
    Why dont you like using namespace std;? I mean, it is a i know it is a using directive (or is it a using declaration? i always confuse those two ) Anyways, do you think my code should have looked like this:

    using std::cout;
    using std::cin;
    using std::endl;

    or should it have looked like this:

    std::cout << "Hello World" << std::endl;

    its been a long time since i have done stuff like this, so i cant remember if the examples above are correct, but my final quesiton is why dont you like using "using namespace std;"?
    Support your right to arm bears.


    ^^This was the first video game which i played on an old win3.1 box

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

 Security News

     Patches

       Security Trends

         How-To

           Buying Guides