Solved

Reading and writing text files in C++

Posted on 2000-03-07
5
192 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

791 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