Solved

Reading and writing text files in C++

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

Technology Partners: 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

Suggested Solutions

Title # Comments Views Activity
C Language combined operators 28 122
Indy 10 not Receiving UDP broadcast 3 43
I could not set window to top 4 40
How do I get Window Title of all opened process? 4 33
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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

763 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