Help? reading from one file to another...

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2008
Posts: 3
Reputation: Student_ABC is an unknown quantity at this point 
Solved Threads: 0
Student_ABC Student_ABC is offline Offline
Newbie Poster

Help? reading from one file to another...

 
0
  #1
Oct 8th, 2008
Hi. I'm new here and would appreciate any help you can give me.

I am a college student and I'm supposed to do this assignment where I take data from one txt file and put it into another. This is step one of a larger project. I have some code done but it's not that good. The infile is data for grocery receipt tape with five pieces of data across it. I have the outfile creating itself but it only contains zero's- three of them for each variable I created? How do I specify what to read from the infile? I need to read the first three of the five items. Thanks!
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstdlib>
  4.  
  5. using namespace std;
  6.  
  7. int item_number, item_name, unit_price; //declaring variables
  8.  
  9. int main()
  10.  
  11. {
  12.  
  13. ifstream indata;
  14. indata.open("data.txt",ios::in); // opens the infile
  15. ofstream outdata;
  16. outdata.open("YourLastName_YourFirstName.txt",ios::out); //opens the outfile
  17. outdata<<item_number<<item_name<<unit_price;
  18. indata.close (); //closes the infile
  19. outdata.close (); // closes the outfile
  20.  
  21. return 0;
  22.  
  23. }
Last edited by Ancient Dragon; Oct 8th, 2008 at 7:50 am. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Help? reading from one file to another...

 
0
  #2
Oct 8th, 2008
You could help us by reading the forum rules on how to post code
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 305
Reputation: stilllearning has a spectacular aura about stilllearning has a spectacular aura about 
Solved Threads: 43
stilllearning stilllearning is offline Offline
Posting Whiz

Re: Help? reading from one file to another...

 
0
  #3
Oct 8th, 2008
Well, you should be checking to see if your file pointers are valid or not before proceeding.

And you need to read the data from your infile before you can write it to your outfile. You can have a counter that counts how many elements you are reading from your infile, and write them to the outfile.

Ah yes and what Salem just said
Last edited by stilllearning; Oct 8th, 2008 at 2:12 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 3
Reputation: Student_ABC is an unknown quantity at this point 
Solved Threads: 0
Student_ABC Student_ABC is offline Offline
Newbie Poster

Re: Help? reading from one file to another...

 
0
  #4
Oct 8th, 2008
Thanks guys or gals. I did read around last night but I missed the code tag part. Sorry! So, I'm still working on this today. I added the error code for the file open and found out that I had assigned the incorrect file name so it wasn't opening correctly. Now, I just need to get the right thing read and I am in business!
Last edited by Student_ABC; Oct 8th, 2008 at 9:26 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC