PDA

View Full Version : C++: I/O File Stream Objects


Karim
04-21-2004, 07:36 PM
Every example I've seen involving file stream objects either hardcodes the filename or when writing to file, hardcodes the input or lets the user type in a few lines.

What I'm trying to do is open a pre-existing text file (done), have the user type in a file name (done), and save the data from the opened text file into the user-defined output file (not done).

I can display the contents on-screen. The biggest problem is finding a way to link the data to the user-defined filename in a function that can handle the task.


ifstream filein;
openfile(filein); //function call passing data from pre-existing text file

ofstream fileout;
outfile(fileout); //function call creating user defined text file


I know I'm misunderstanding something because it shouldn't be as difficult as I'm making it. Any help is always appreciated.

sabotai
04-21-2004, 07:57 PM
Well, you could get the info from the first file using the member function getline. And then put that into the new file. I'll try to write up a small program showing how to do it. (In the meantime, someone else might answer better. :) )

sabotai
04-21-2004, 08:07 PM
ifstream filein; // Input file
ofstream fileout; // Output file

char buffer; // temporary char to hold data read in

filein.open("testin.txt"); // Open the input file

fileout.open("testout.txt"); // Open the output file

while (!filein.eof()) // until you get to the end of the file...
{
filein.get(buffer); // Read in a character from the input file...
fileout.put(buffer); // And write it to the output file
}

filein.close(); // Close the input file
fileout.close(); // Close the output file

sabotai
04-21-2004, 08:09 PM
ugh...the formatting in the code blocks suck, but I think you can read it. :)

Mr. Wednesday
04-22-2004, 01:16 AM
Hmm... there ought to be a cute way to do it using stream iterators. If I remember right, though, you're in a class, and stream iterators are probably beyond the scope at this point (although they probably ought to be included in any serious course work on the streams).

Anyway, sabotai's method looks good to me, and I think the streams will take care of buffering to make sure that the performance is competitive.

Karim
04-22-2004, 08:22 AM
That's great. Thank-you.

I'm writing my final today. One trace (most likely involving 2-D arrays), one program we'll have to code, 6 short answer and a small section dealing with classes - a topic we just touched on in the last week (wouldn't you know).

I think for the summer I'm going to pick up a better text and hack away on my own.

Thanks again for the help.

Mr. Wednesday
04-23-2004, 12:13 AM
If you're looking for good general C++ texts, a couple are:
Accelerated C++ by Koenig and Moo, has a great reputation as a pure C++ text (rather than text that starts with C and moves on to C++ features)
The C++ Standard Library - A Tutorial and Reference by Josuttis, a great way to learn how much you can do with the standard library

I have More Exceptional C++ by Sutter and More Effective C++ by Meyers on my bookshelf, but I haven't gotten to them. I'd also like to pick up the originals if I get a chance.