Solved

Reading and writing text files in C++

Posted on 2000-03-07
5
189 Views
Last Modified: 2010-04-02
Hi!,
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........

Thanks.
 
#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:";
      cin.ignore(1,'\n');
      cin>>filenameOut;
      ofstream  outfile(filenameOut);    //open the file to write to
      cin.ignore(1,'\n');


                if(infile)            //read until end of file
      while(infile.get(ch)){
           infile.get(ch);
                     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
}
0
Comment
Question by:colleen_dana
  • 3
5 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 2593766
Change the loop to read

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

and it should work...
0
 
LVL 86

Expert Comment

by:jkr
ID: 2593770
To elaborate: the 2nd 'infile.get()' call skips every 2nd character from the input file, so you're getting 'garbage'...
0
 
LVL 4

Accepted Solution

by:
captainkirk earned 75 total points
ID: 2593815
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.ignore(1,'\n');
cin >> filenameOut;
ofstream outfile(filenameOut, ios::out, filebuf::openprot);
cin.ignore(1, '\n');

if(infile) // read until end of file
{      
     while(!infile.eof())
     {
      infile.get(ch);
      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
            
0
 
LVL 2

Expert Comment

by:VEngineer
ID: 2594007
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;
   }

   fin.close();
   fout.close();

   return 0;
}

0
 
LVL 86

Expert Comment

by:jkr
ID: 2594075
Thanx, captainkirk - I think there was a reason to immediatley post answers...
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now