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!

#include <iostream>
#include <fstream>
#include <cstdlib> 

using namespace std;

int item_number, item_name, unit_price; //declaring variables

int main()

{ 

ifstream indata;
indata.open("data.txt",ios::in); // opens the infile 
ofstream outdata;
outdata.open("YourLastName_YourFirstName.txt",ios::out); //opens the outfile
outdata<<item_number<<item_name<<unit_price;
indata.close (); //closes the infile
outdata.close (); // closes the outfile

return 0;

}

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 :)

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!

This article has been dead for over six months. Start a new discussion instead.