Front Office Football Central  

Go Back   Front Office Football Central > Archives > FOFC Archive
Register FAQ Members List Calendar Mark Forums Read Statistics

Reply
 
Thread Tools
Old 04-21-2004, 07:36 PM   #1
Karim
College Starter
 
Join Date: Oct 2000
Location: Calgary
C++: I/O File Stream Objects

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.

Code:
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.

Karim is offline   Reply With Quote
Old 04-21-2004, 07:57 PM   #2
sabotai
General Manager
 
Join Date: Oct 2000
Location: The Satellite of Love
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 is offline   Reply With Quote
Old 04-21-2004, 08:07 PM   #3
sabotai
General Manager
 
Join Date: Oct 2000
Location: The Satellite of Love
Code:
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

Last edited by sabotai : 04-21-2004 at 08:08 PM.
sabotai is offline   Reply With Quote
Old 04-21-2004, 08:09 PM   #4
sabotai
General Manager
 
Join Date: Oct 2000
Location: The Satellite of Love
ugh...the formatting in the code blocks suck, but I think you can read it.
sabotai is offline   Reply With Quote
Old 04-22-2004, 01:16 AM   #5
Mr. Wednesday
Pro Starter
 
Join Date: Jul 2003
Location: South Bend, IN
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.
Mr. Wednesday is offline   Reply With Quote
Old 04-22-2004, 08:22 AM   #6
Karim
College Starter
 
Join Date: Oct 2000
Location: Calgary
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.

Last edited by Karim : 04-22-2004 at 08:25 AM.
Karim is offline   Reply With Quote
Old 04-23-2004, 12:13 AM   #7
Mr. Wednesday
Pro Starter
 
Join Date: Jul 2003
Location: South Bend, IN
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.

Last edited by Mr. Wednesday : 04-23-2004 at 12:14 AM.
Mr. Wednesday is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is On
Forum Jump


All times are GMT -5. The time now is 06:54 PM.



Powered by vBulletin Version 3.6.0
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.