Solved

Reading and writing text files in C++

Posted on 2000-03-07
5
196 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

617 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