Dec 14, 2011 at 2:30pm UTC
Hi. I am pretty new at C++ and am having a problem reading from one file and writing to another file in a different format (fixed positions). I am reading in the file as a string and assigning fields to each variable. When the program runs, the only line that is put out is the last line. I think it has something to do with the line not moving to the next line but I am not sure.
Any help/suggestions would be appreciated. thank you
My complete program is as follows:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <iomanip>
#include <stdio.h>
using namespace std;
int main()
{
string filename1 = "shipped.txt" ;
string filename2="shipped.new";
//Fields in the data file
string shipDate ;
string trackingNum ;
string partNum;
string fname;
string lname;
string company;
const int NUMELS=5;
ifstream inFile;
ofstream outFile;
inFile.open(filename1.c_str());
outFile.open(filename2.c_str());
if (inFile.fail())
{
cout << "The file was NOT successfully opened"
<< "\n Please check that the file currently exists"
<< endl ;
system("PAUSE");
exit(1);
}
string line;
int i;
for (i = 0;i < NUMELS; i++)
{
inFile >> shipDate >>trackingNum >>partNum >>fname >>lname >>company;
// The read properly and display properly
//using the cout command
//cout << "record" << shipDate << trackingNum << endl;
outFile.seekp(0) << shipDate ;
outFile.seekp(12 -1) << trackingNum;
outFile.seekp(22 -1) << partNum ;
outFile.seekp(31 -1) << fname ;
outFile.seekp(39 -1) << lname ;
outFile.seekp(51 -1) << company <<endl ;
}
inFile.close();
outFile.close();
cout << "SUCSESS --- file is written properly" << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
Dec 14, 2011 at 4:41pm UTC
Can you show your output file how you are getting and how you are expecting;
Dec 14, 2011 at 5:34pm UTC
***INPUT FILE
04/12/11 d50625 74444 James Lehoff Rotech
04/12/11 d60752 75255 Janet Lezar Rotech
04/12/11 d40295 74477 Bill McHenry Rotech
04/12/11 d23745 74470 Diane Kaiser Rotech
04/12/11 d50892 75155 Helen Richardson NapTime
*** EXPECTED OUTPUT FILE
04/12/11 d50625 74444 James Lehoff Rotech
04/12/11 d60752 75255 Janet Lezar Rotech
04/12/11 d40295 74477 Bill McHenry Rotech
04/12/11 d23745 74470 Diane Kaiser Rotech
04/12/11 d50892 75155 Helen Richardson NapTime
*** ACTUAL OUTPUT FILE
04/12/11 d50892 75155 Helen Richardson NapTime
Last edited on Dec 14, 2011 at 5:39pm UTC
Dec 14, 2011 at 6:17pm UTC
when i run the program i am able to open the file but was not able to read the content of the file .
Dec 14, 2011 at 6:36pm UTC
I think it has something to do with the line not moving to the next line but I am not sure.
Yes, It looks like everytime you output a record group you overwrite the previous... so only the last one shows up.
Try this instead:
1 2 3 4 5 6 7
int offset = i * 59;
outFile.seekp(0 + offset) << shipDate;
outFile.seekp(11 + offset) << trackingNum;
outFile.seekp(21 + offset) << partNum;
outFile.seekp(30 + offset) << fname;
outFile.seekp(38 + offset) << lname;
outFile.seekp(50 + offset) << company << endl;
Last edited on Dec 14, 2011 at 6:37pm UTC