Solved

File I/O with fstream

Posted on 1998-11-09
4
552 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

867 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

15 Experts available now in Live!

Get 1:1 Help Now