#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Location
    currently Lisbone, Portugal
    Posts
    154
    Rep Power
    12

    Getline in Builder C++ 6.0


    Hello strange problem that I have got me to post a message here.
    I have a text file and need to read it from teh disk in a cycle, so when I encounter the end of the file (EOF), the program should seek the beginning of the file and start reading again.
    The program looks like this..


    #include <vcl.h>
    #include <fstream>
    #include <iostream>
    #include <string>
    #pragma hdrstop

    using namespace std;

    #include "Unit1.h"
    #pragma package(smart_init)
    #pragma resource "*.dfm"
    TForm1 *Form1;
    __fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner){}

    void __fastcall TForm1::BitBtn1Click(TObject *Sender)
    {
    string sIn;
    if (Form1->OpenDialog1->Execute())
    {
    ifstream infile;
    infile.open(Form1->OpenDialog1->FileName.c_str());
    infile.seekg(0,ios::end);
    infile.seekg(0,ios::beg);
    while (getline(infile,sIn))
    {
    Form1->Memo1->Lines->Add(sIn.c_str());
    Form1->Memo3->Lines->Add(IntToStr(infile.tellg()).c_str());
    }
    infile.seekg(0,ios::beg);
    while (getline(infile,sIn))
    {
    Form1->Memo2->Lines->Add(sIn.c_str());
    Form1->Memo4->Lines->Add(IntToStr(infile.tellg()).c_str());
    }
    }
    }

    void __fastcall TForm1::FormCreate(TObject *Sender)
    {
    Form1->Memo1->Clear();
    Form1->Memo2->Clear();
    Form1->Memo3->Clear();
    Form1->Memo4->Clear();
    }

    However, Memo2 and memo4 are always empty, no matter what I do. Seems that once EOF was encountered, there is no way but to close and open the file again. Is that true? Is there any way to bypass it ??
    Thanks for the answers
    Marek
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,625
    Rep Power
    4247
    The reason is because once you get to the end of file and attempt to read more data with getline(), the stream is unable to complete the operation. Hence, the first loop is exited since getline() returns false, but the stream object sets some bits (failbit and eofbit) to indicate that the stream is now in a abnormal condition, since it couldn't complete the getline() operation. You'll need the clear these status bits with the clear() method, before the stream object will process any other data for you. See the bold text additions I made below.
    Code:
    	string sIn;
    	if (Form1->OpenDialog1->Execute())
    	{
    		ifstream infile;
    		infile.open(Form1->OpenDialog1->FileName.c_str());
    		infile.seekg(0,ios::end);
    		infile.seekg(0,ios::beg);
    		while (getline(infile,sIn))
    		{
    			Form1->Memo1->Lines->Add(sIn.c_str());
    			Form1->Memo3->Lines->Add(IntToStr(infile.tellg()).c_str());
    		}
    		if (infile.fail())
    			infile.clear();
    		infile.seekg(0,ios::beg);
    		while (getline(infile,sIn))
    		{
    			Form1->Memo2->Lines->Add(sIn.c_str());
    			Form1->Memo4->Lines->Add(IntToStr(infile.tellg()).c_str());
    		}
    	}
    You can, of course, skip the check for infile.fail() and call infile.clear() unconditionally as well.

    See http://www.cplusplus.com/ref/iostream/ios/clear.html for more.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Location
    currently Lisbone, Portugal
    Posts
    154
    Rep Power
    12

    Thumbs up



    Thanks, that solves the problem. Stupid of me that I did not think about that myself ...
    Best greetings
    Marek

IMN logo majestic logo threadwatch logo seochat tools logo