you need an ifstream object to open the text file, and a string to receive the strings. Both of these are standard c++ classes in and header files. There are hundreds, if not thousands, of examples you can follow to write your program so I am not going to repeat them here. Basically you need to (1) read a line using getline function then (2) compare it to the string you are looing for using the == comparison operator.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
You didn't look hard enough. Didn't you run across the getline() method?
WaltP
Posting Sage w/ dash of thyme
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
Maybe this will help. Note the title...
WaltP
Posting Sage w/ dash of thyme
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
in that case can anyone point me to where there is a very detailed tutorial on how to use ifstream ?
I guess I was too subtle. Post your code as the link describes. Very often if we see the code we can figure out what to tell you. So far we're only guessing.
WaltP
Posting Sage w/ dash of thyme
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
line 6: do you have a program on your computer called command.exe ? If not then that line of code will do nothing other than get an error message from the operating system that "command not found".
lines 8 and 10: that loop doesn't work right. You need to combine them like this:
while( file_op.getline(str,5000) )
{
// compare the strings here to see if this line is the one you are looking for
}
line 13: you can not compare two c-style character arrays like that. Usestrcpy() to do that
if( strcpy(errorline, str) == 0)
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
The code you posted a week ago is obviously not the same code you are using today. So post code (I can't see your monitor very well) and zip up the data file and post it too. stramp is case sensitive so maybe the string that's in the data file is not the same case as the string you want to compare. To do a case-insensive compare you can convert both strings to either upper or lower case before calling strcmp(). Some compilers have a case-insensitive compare function, such as stricmp() in Microsoft compilers and compareNoCase() (or something like that) in some other compilers.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
whats confusing is that when i create the output.txt file manually and not by having it created using the system("mycommand>output.exe") function and then type in the the same output i would get from mycommand.exe , the strcmp(error,str) would then detect the matching lines properly!
That indicates the text generated by the system() function is not the same as what you type manually, could be as simple as a space, tab character, or capitalization. Load both files up into Notepad and compare them side-by-side to see what are the differences.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
although they look identical in notepad , i believe like you said, a space or tab character is added somewhere on each line by the system() function.
i tried removing spaces from the line created by the system() function and then using strcmp() but it still didn't work :/
Then that obviously wasn't the problem. What I'd do now is display every character entered in hex just before comparing and, as AD suggests, see if there are differences you can't see.
You also might want to post part of the file.
WaltP
Posting Sage w/ dash of thyme
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
Maybe you need to change:-
cout<<"\nerror found!\n";cin.ignore();
to
cout<<"\nMatch!\n";cin.ignore();
Or alternatively change:-
if(strcmp(error,str)==0
to
if(strcmp(error,str)==1
The choice is yours.
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439