Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Searching from the begining of a text file ...seekg() ? 1

Status
Not open for further replies.

TalosBlack

Programmer
Jan 6, 2004
33
0
0
US
I am trying to search a text file for a given string. After i do that search i would like to search it for another string. I was wondering how i can start searching at the begining of the text file. I thought i should use seekg(0) and i made a practice program that does just that. ( I am using while(!in.eof()) {in.getline} to pull the data in and in.seekg(0) to try to start at the begining of the file.) It seems to work. However, when i tried to add that into my more complicated program it doens't seem to be working. Any idea of what i'm doing wrong ?


Thanks,

John
 
seekg(0) always will reset get position to 0, so you should seekg(xxx) where xxx you will change to some last position.

Ion Filipski
1c.bmp
 
Isn't position 0 the start of the file ? In which case that is where i want to be anyway. If not then how should i figure out what xxx is at the start of the file ?
 
/*
This function is suppose to check global variable
not declared here against the declared local variables
that are input from a file. It seems to be getting into
an infinite loop or having another error. I believe this is becauce it is getting to the end of the file and then when this function is called a second time it is already at the files end. If you notice anything wrong with this code please let me know.
*/

void CheckForFile()
{

//CheckForFile function
char nameofile[MAX_PATH];
char sizeofile[105];
char timewrite[105];
char emptyline[10];
char * stopstring;

int sizeofile2;

//resets to the beginning of the in file
in.seekg( 0,ios::beg);

//Searching for File Listing / Where data starts in infile
char temp[105];
do
{
in.getline(temp,100);
}while((strncmp(temp, "File Listing",12)) != 0);

//Looped that ins files
while(!in.eof())
{//infile while
//get data
in.getline(nameofile, 200);
in.getline(sizeofile, 100);
//chart to int
sizeofile2 = strtol(sizeofile, &stopstring, 10);
in.getline(timewrite, 100);

cout<<nameofile<<endl; //iamhere

//get empty line
in.getline(emptyline, 5);

//comparision
if((strncmp(nameofile, nameoffile,stringlength2))==0)
{
if((strncmp(timewrite, timewritten,stringlength))==0)
{
if(sizeofile2 == sizeoffile)
{
TheFlag = 1;
}
}
}
}//end in file while
}//end CheckForFile function
 
infinitie loop in this loop:

do
{
in.getline(temp,100);
}while((strncmp(temp, &quot;File Listing&quot;,12)) != 0);
 
I believe my problem was the eof error flag once set high didn't go low when i gave it a new pointer to the begining of the file. So now after the while loop finishes and eof state is reached I use in.clear() to clear the error states and then continue with the other things i was trying to do.

John
 
May be you have another problem. If in.getline(buffer,counter) reads counter-1 chars and stops before line delimiter, it sets failbit for in stream (see basic_istream::getline() member spec). Then your program stops reading (in.fail() == true, but in.eof() == false;).
Include in.fail() check and in.clear() code in the proper place(s).
But I think there is the better way. Don't use in.getline(0 with counter, avoid (or reduce) C++ stream/C string mixture. Try this pattern, for example:
Code:
void DoThat(const char* fname)
{
  ifstream in(fname);
  if (!in)
  {
    cout << &quot;Can\'t open.&quot; << endl; // print to demonstrate
    return;
  }
  string s; // Key point: robust std::string input buffer!
  while (getline(in,s)) // template getline, not a member!
  {
    if (s.find(&quot;2nd&quot;,0,3) != string::npos) // leading 3 pos
      cout << &quot;*&quot;;
    cout << s << endl; // Replace <<, perform you code
  }
  if (in.eof()) // 2nd pass after rewind example
    in.clear();
  if (!in.seekg(0)) // always check positioning result.
  {
    cout << &quot;seekg failed.&quot; << endl;
    return;
  }
  cout << &quot;--- Pass 2 ---&quot; << endl;
  while (getline(in,s)) // Do the same or other job...
  {
    if (s.find(&quot;1st&quot;,0,3) != string::npos)
      cout << &quot;*&quot;;
    cout << s << endl;
  }
}
Play with this sceleton then add your functionality (try before buy;)...
 
Sounds good. I don't see exactly what the code is doing yet but i will do as you say and play with the skeleton.

Thanks a lot,

John
 
I believe i need namespace to use some of those functions. I currently can't do that because i'm using msvc++ 4.0. I talked to my boss and should be getting 6.0 or .NET soon, however, i don't have it yet. Nonetheless thanks for all your help.

John
 
Probably in the old C++ environment the better way is: don't use C++ (old) streams (and std:string). Use old good fgets(), lseek()/rewind() etc to input via a proper sized buffer.
You will have more robust (and cumbersome;) code...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top