Reading and writing text files in C++

I am relatively new to C++ and I am trying to read in a number of text files (i.e. files which contain dictionary entries) and modify some things in each of the files.  After some changes have been made I then want to output the text to a new file. For the moment I am trying to read in one of the files and write it to a new file but only garbage is written to the file!
Can you have a look at some code........

#include <fstream.h>
#include <iostream.h>
#define MAXLENGTH 30

void main()

      char filenameIn[MAXLENGTH];  
      char filenameOut[MAXLENGTH];
      char ch;

      cout<<"Enter filename:";
      cin>>filenameIn;                    //get the filename
      ifstream infile(filenameIn);     //open the file
      cout<<"Enter the filename to write to:";
      ofstream  outfile(filenameOut);    //open the file to write to

                if(infile)            //read until end of file
                     cout<<ch;        //output the letters to the screen
           outfile<<ch;      //output to the new file
      }   //end while

      infile.close();            //close  the file for reading
      outfile.close();      //close the file for writing
Who is Participating?

Improve company productivity with a Business Account.Sign Up

captainkirkConnect With a Mentor Commented:
Here's the amended code - it does get one extra char, but you can fix that...

char filenameIn[MAXLENGTH];    
char filenameOut[MAXLENGTH];
char ch;

cout << "Enter filename: ";
cin >> filenameIn;                    // get the filename
ifstream infile(filenameIn, ios::in, filebuf::openprot); // open the file
cout << "Enter the filename to write to: ";
cin >> filenameOut;
ofstream outfile(filenameOut, ios::out, filebuf::openprot);
cin.ignore(1, '\n');

if(infile) // read until end of file
      cout << ch;    // output the letters to the screen
      outfile << ch; // output to the new file
     }   //end while
} // end if

infile.close();  // close  the file for reading
outfile.close(); // close the file for writing
Change the loop to read

                if(infile) //read until end of file
                     cout<<ch;   //output the letters to the screen
     outfile<<ch; //output to the new file
}   //end while

and it should work...
To elaborate: the 2nd 'infile.get()' call skips every 2nd character from the input file, so you're getting 'garbage'...
This is the way I learned how to do it - it might be a little clearer:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main() {
   string filenameIn;
   string filenameOut;
   char   ch;

   cout << "Enter filename: " << flush;
   cin  >> filenameIn;
   cout << endl;
   ifstream fin(filenameIn.c_str());

   if (!fin) {
      cerr << "Invalid infile" << endl;
      return 1;

   cout << "Enter the filename to write to: " << flush;
   cin  >> filenameOut;
   cout << endl;
   ofstream fout(filenameOut.c_str());

   char ch;
   while (fin >> ch) {
      // while you are able to read from the file
      cout << ch;
      fout << ch;


   return 0;

Thanx, captainkirk - I think there was a reason to immediatley post answers...
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.