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 for a set of chars a string

Status
Not open for further replies.

JaybOt

Programmer
Apr 18, 2001
101
0
0
GB
Hi all,

I have written some code to open a file and check for a tag (string) within that file.

No matter how it is coded, the result does not appear to be correct. The code should return 'found' if the string was in the file, and 'not found' otherwise, but it does not appear to work correctly. Here is a pesudo snippit...

pos = 0;
tag = argv[2];

infile.open(file);
while(infile.get(Ch))
{ // read and store the data in array 'data'
data[pos] = Ch;
outfile << Ch;
pos++;
}
infile.close();

if(strcmp(data,tag) == 0)
{ // check if 'tag' is in the array
printf(&quot;found\n&quot;);
}
else
{
printf(&quot;not found\n&quot;);
}

Essentially, i need to find a small string within a much larger one (that is the file).

Im sure im looking at this too simply, so if anyone can point me in the right direction it would be much appriciated. Thanks in advance.

Regards,
J@yb0t[afro]

&quot;Always know what you say, but don't always say what you know!&quot;
 
Thanks m8,

Tried that, but get the same result - any substring searched for returns 'found'.

Doesn't strstr() require all the chars of the main string and substring to be of the same CaSe?

This would not work for me as i have no way of knowing what case the main string (file) will be in (mixed).

What i realy need is a way to do a regexp of some kind.

Regards,
j@yb0t[afro]

&quot;Always know what you say, but don't always say what you know!&quot;
 
Sorry mate,

got it working now! Another error was the reason it did not work!

Regards,
J@yb0t[afro]

&quot;Always know what you say, but don't always say what you know!&quot;
 
Re your mixed-case problem, why don't you just convert both the string to be searched and the search phrase to the same case for the purpose of the strstr - if keeping the case is important fpr subsequent processing then just make a copy of it to be converted.

Cheers - Gavin
 
No input string null terminator - that's all...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top