Hi all,
I'm completely new to C but have dabbled with Bash scripting before. I've been trying to get my head around pointers etc.

I have a small routine to check for the existence of a string in a file and print to screen, I claim no originality it's a mish mash of what's on net. i hope it's a start of a larger program eventually so I want it to be simple.

Now, it did work, just once and was correct, matching the string in a file but since then it refuses to work.

I'm puzzled as to why. If anyone can point me in right direction.

Here's the code I have :-

#include <string.h>

#include <stdio.h>

#include <stdlib.h>	
FILE *fp;	 
 
int main() {
	
char SearchText[]="#!/bin/ash"; /* Search String. */
char MainText[80];
  

 if ((fp = fopen("/etc/rc.d/rc.local","r")) == NULL) { return; }
          
 while (feof(fp) == 0)
 
 {  fgets( MainText,80,fp );
 
  
 if (strcmp(MainText,SearchText) == 0 )
		printf("strings are equal\n");
	else 
		printf("strings are different\n");

	
  printf("%s", MainText); /* See what MainText actually is */
 }
 
 fclose(fp);
 
 
return(0);
 


 
}

Edited 6 Years Ago by fizzle: n/a

Can you paste the content of the input file ?

Also I do not understand the rationale behind the number 80 ?Can some one explain why the size of the temp buffer is 80

First of all, you have taken the search string wrong...
It will be

char SearchText[]="#!/bin/sh"; // not "#!/bin/ash"...right?"

...a silly mistake !!!
Second thing is, still it wont work as fgets is taking \n together with the string you are reading as one line from the file.
So, your MainText is containing "#!/bin/sh\n" and so it wont match...
You can try changing your SearchText to "#!/bin/sh\n", and it will work...
To be sure to what I told, print both the strings and check it...

printf("\n####%s####%s####\n", SearchText, MainText); /* See what both the string contains */

Otherwise you can also try changing the fgets function to fread or fscanf for the purpose...

Hope that helps,

Assuming you're really wanting to find files that have a line beginning with that string, and not with any other characters in front of your SearcText, you might want to consider using strncmp, instead of strcmp. That will also cure your problem with any trailing text (such as a newline or a comment).

Correctly told Dervish...
If correct position of the text in the line of the file is known (in this case, it a script file which is definitely going to start with that search string), we can very well solve the problem with string functions like strndup or can use strncmp for the comparison with strlen(SearchText) as the size.

Cheers,

Back again after a power cut stopped play!


kings_mitra,

No it's ash, as this script was written for ash.

Your line to print both search texts produces just " #####!/bin/ash "

However thanks for the result regarding fgets returning the line including the \n , once I added that to the search text it worked. Odd how it worked once only, must have been a fluke.

I've since hit another snag when i attempted to run through the 10 line script a line at a time.

I changed the search text to the 2nd line of the script which was:

[space]modprobe fbcon

When i entered the line for search it would not find a match again. What do I need to enter instead of a space? I tried \0

Sorry, I didn't post the input file, here it is.

#!/bin/ash
modprobe fbcon

exec /root/.startmount/mount-drives.sh

if [ -x /etc/rc.d/rc.firewall ]; then
/etc/rc.d/rc.firewall start
fi


Argh, board format removes spaces, modprobe fbcon has one in front and /etc....firewall start... has two in front.

I presume it wouldn't find a match for the 2nd firewall line either as it begins with spaces.

Edited 6 Years Ago by fizzle: n/a

Hmmm, something odd again. I entered two spaces for the firewall line in the search text and it didn't work, then I closed everything, waited, then tried again and it works !!

I think the PC cache is not writing whilst I'm changing and testing.

Your line to print both search texts produces just " #####!/bin/ash "

Not possible right... If you have printed both the texts then both should come.
Now, if you have printed the MainText first then the SearchText will be definitely printed at the next line due to the \n at the end of MainText.
Check it...

Anyway, coming back to the point, if you know the full line that has to be matched (let it be with space in front or anything else), then that same string (with space in front) can be taken as the SearchText and it will work.
Now, if it is a part of the line (i.e, may be present at any point in the file), then you need to change your program and will have to search character wise.

What do I need to enter instead of a space? I tried \0

If it the first case you can check with changing the string with " modprobe fbcon". Check it...

OK guys problems solved.

The search works and returns OK.

I'm just using a text editor and compiling at command line.

I watched the disk write when i compiled before running, the write wasn't taking place as the disk cache held it. I was too quick testing from the directory file, if I waited or made another action it was written to disk and it ran OK.

Edited 6 Years Ago by fizzle: n/a

This question has already been answered. Start a new discussion instead.