This is some code I wrote to check a string's presence in a file:
bool aviasm::in1(string s)
{
ifstream in("optab1.txt",ios::in);//opening the optab
//cout<<"entered in1 func"<<endl;
char c;
string x,y;
while((c=in.get())!=EOF)
{
in.putback(c);
in>>x;
in>>y;
if(x==s)
return true;
}
return false;
}
it is sure that the string being searched lies in the first column of the optab1.txt and in total there are two columns in the optab1.txt for every row.
Now the problem is that no matter what string is being passed as the parameter s to the function always returns false. Can you tell me why this happens?
What a hack! Why not use standard C++ string and file reading functions:
You can replace
substrby more advanced string searching functions if the search string isn’t required to be at the beginning of a line. Thesubstrversion is the most readable, but it makes a copy of the substring. Theequalversion compares the two strings in-place (but requires the additional size check). Thesearchversion finds the substring anywhere, not just at the beginning of the line (but at a price).