Solved

File I/O with fstream

Posted on 1998-11-09
4
561 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

632 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