decornez
asked on
File I/O with fstream
I have some code where I first open the file to count the number of lines in it. I then close the fstream object an re-open it to extract data out of the file. But I seem to get rubbish out as it looks like the file pointer is running in the wilderness somewhere... I wonder if I closed and re-opened the fstream object correctly ? Any suggestions ?
Below is the code:
fstream InpFil("C:\\Tads\\DPRM
files\\MDRYieldCurvesCcy.H KD.01",ios ::in);
string CurLin;
int counter = 0;
while(!InpFil.eof())
{
getline(InpFil,CurLin);
counter = counter +1;
}
//We must close the stream before we reopen it
//or it will start reading rubbish
//fstream::close;
InpFil.close();
//We reopen the stream
InpFil.open("C:\\Tads\\DPR M files\\MDRYieldCurvesCcy.H KD.01",ios ::in);
int FirstInt;
char MOrY;
string Instru;
double Bid;
double Ask;
string Hyphen;
const int arsize1 = 40;
CString FirstInts[arsize1];
CString MOrYs[arsize1];
CString Tenors[arsize1];
CString Instrus[arsize1];
double Bids[arsize1];
CString Hyphens[arsize1];
double Asks[arsize1];
//We now start by skipping the first few lines
//This is standard for all files
getline(InpFil,CurLin,'\n' ); // Get and ignore date line.
getline(InpFil,CurLin,'\n' ); // Get and ignore run-by line.
getline(InpFil,CurLin,'\n' ); // Get and ignore blank line.
getline(InpFil,CurLin,'\n' ); // Get and ignore blank line.
getline(InpFil,CurLin,'\n' ); // Get and ignore "yield" line.
getline(InpFil,CurLin,'\n' ); // Get and ignore blank line. getline(InpFil,CurLin,'\n' ); // Get and ignore FX line.
getline(InpFil,CurLin,'\n' ); // Get and ignore FX line.
getline(InpFil,CurLin,'\n' ); // Get and ignore blank line.
//We are now going to loop using the previous counter figure
//as our number of steps to read the full table
int counter2 = counter - 20;
//int counter2 = counter - 11;
int i = 0;
for (i = 1; i <= counter2; i++) {//opening of the for loop
InpFil >> FirstInt >> MOrY >> Instru >> Bid >> Hyphen >> Ask;
getline(InpFil,CurLin); // Get current line.
//we first get the tenor.
//to do this we convert the int into a string
//we then concatenate the two string into one
char buffer1[3];
_itoa(FirstInt, buffer1, 10);
FirstInts[i] = buffer1;
Rgds,
A.
Below is the code:
fstream InpFil("C:\\Tads\\DPRM
files\\MDRYieldCurvesCcy.H
string CurLin;
int counter = 0;
while(!InpFil.eof())
{
getline(InpFil,CurLin);
counter = counter +1;
}
//We must close the stream before we reopen it
//or it will start reading rubbish
//fstream::close;
InpFil.close();
//We reopen the stream
InpFil.open("C:\\Tads\\DPR
int FirstInt;
char MOrY;
string Instru;
double Bid;
double Ask;
string Hyphen;
const int arsize1 = 40;
CString FirstInts[arsize1];
CString MOrYs[arsize1];
CString Tenors[arsize1];
CString Instrus[arsize1];
double Bids[arsize1];
CString Hyphens[arsize1];
double Asks[arsize1];
//We now start by skipping the first few lines
//This is standard for all files
getline(InpFil,CurLin,'\n'
getline(InpFil,CurLin,'\n'
getline(InpFil,CurLin,'\n'
getline(InpFil,CurLin,'\n'
getline(InpFil,CurLin,'\n'
getline(InpFil,CurLin,'\n'
getline(InpFil,CurLin,'\n'
getline(InpFil,CurLin,'\n'
//We are now going to loop using the previous counter figure
//as our number of steps to read the full table
int counter2 = counter - 20;
//int counter2 = counter - 11;
int i = 0;
for (i = 1; i <= counter2; i++) {//opening of the for loop
InpFil >> FirstInt >> MOrY >> Instru >> Bid >> Hyphen >> Ask;
getline(InpFil,CurLin); // Get current line.
//we first get the tenor.
//to do this we convert the int into a string
//we then concatenate the two string into one
char buffer1[3];
_itoa(FirstInt, buffer1, 10);
FirstInts[i] = buffer1;
Rgds,
A.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Nietod,
Your answer works in conjunction with jim_g 's comment. As actually if I do not clear the fstream object then even while repointing at the beg of file it still gives me rubbish. But still, I needed to go back to the beg of the file when implementing jim_g 's comment.
Jim_g,
Your comment was an excellent one and you deserve some points for that. I will post a question to give you some points.
Thanks to both of you.
Rgds,
A.
Your answer works in conjunction with jim_g 's comment. As actually if I do not clear the fstream object then even while repointing at the beg of file it still gives me rubbish. But still, I needed to go back to the beg of the file when implementing jim_g 's comment.
Jim_g,
Your comment was an excellent one and you deserve some points for that. I will post a question to give you some points.
Thanks to both of you.
Rgds,
A.
>> As actually if I do not clear the fstream object then even
>> while repointing at the beg of file it still gives me rubbish
I guess it doesn't reset the information it uses when it parses a text file. That is surprising though, I would have thought it would have. In binary mode it should not be necessary to do this.
>> while repointing at the beg of file it still gives me rubbish
I guess it doesn't reset the information it uses when it parses a text file. That is surprising though, I would have thought it would have. In binary mode it should not be necessary to do this.
After closing the file, make sure to clear the status bits
using, for example, ``InpFil.clear( );'' before re-opening
it. It's not clearly defined what the state of these bits
should be after closing a file, so different implementations
do different things. In any case, explicitly clearing them
should fix your problem.