Solved

File I/O with fstream

Posted on 1998-11-09
4
549 Views
Last Modified: 2008-03-17
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.HKD.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\\DPRM files\\MDRYieldCurvesCcy.HKD.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.
0
Comment
Question by:decornez
  • 2
4 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 30 total points
ID: 1177383
You don't need to close and reopen the file.  You need to return to the start of the file.  Use

InpFil.seekg(0);
0
 

Expert Comment

by:jim_g
ID: 1177384

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.
0
 

Author Comment

by:decornez
ID: 1177385
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.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1177386
>> 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.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now