0

I am new with C, I'm having a problem with this code, I am trying to print when I reach a specific part of a text file, but it doesn't print anything. Here is the code:

while(!feof(f)){
    fgets(temp, 150, f);
    if(temp=='****'){
        printf("%s\n", temp);
    }
}
Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
  • 5
    Several things: 1) Please see [Why is `while ( !feof (file) )` always wrong?](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) 2) you can't compare strings with `==` and 3) `'****'` isn't a string anyway, and 4) you need to remove the trailing newline. Please see [Removing trailing newline character from fgets() input](https://stackoverflow.com/questions/2693776/removing-trailing-newline-character-from-fgets-input/28462221#28462221) – Weather Vane Oct 07 '20 at 15:58
  • Are you able to compile that code without warnings? – William Pursell Oct 07 '20 at 15:59

2 Answers2

1

In order to compare two string, you have to use strcmp function:

if(strcmp(temp,"****")==0 ) { ....

otherwise you will just comparing two pointers

Felice Pollano
  • 32,832
  • 9
  • 75
  • 115
  • If anything, this is only a partial answer and does not address the real problems behind what OP is trying to do. – Marco Bonelli Oct 07 '20 at 16:01
  • @Marco nope,it's the answer – pm100 Oct 07 '20 at 16:05
  • 2
    @pm100 no, the code is still wrong even using `strcmp` and this answer does not address the real problem. You cannot just read chunks of 150 bytes and then compare the initial part of those with `strcmp`. This is not how searching a string in a file works. – Marco Bonelli Oct 07 '20 at 16:06
  • Re “does not compile”: While `'****'` is not what OP needed or wanted, it does compile. C 2018 6.4.4.4 permits character constants to contain multiple characters, and 6.4.4.4 10 says the value is implementation-defined. – Eric Postpischil Oct 07 '20 at 16:30
  • @MarcoBonelli fgets(temp, 150, f); does not read chunks of 150 bytes, it reads line by line each line up to 150 chars – Felice Pollano Nov 26 '20 at 17:04
0

I believe what you really want to use is strstr()

This returns a pointer to the first occurrence of a string inside another string.

https://www.tutorialspoint.com/c_standard_library/c_function_strstr.htm

//while there are still lines to read , print if desired line has been read

while(fgets(temp, 150, f)) 
{
  //if beggining mark is found print
   if(strstr(temp, "****")!=NULL) printf("%s\n" , temp)
}

Note: 150 should be replaced by a MAXIMUM_LINE_SIZE variable. This variable should be populated with the maximum line size from the file that you are reading from. If a line is longer than this max size you could miss the desired string if "****" is over 150 characters into the line.

mitchell
  • 298
  • 1
  • 11
  • This is definitely better than OP's original solution, but if the string is at the end of a chunk you will not detect it (e.g. `"A"*148 + "****"`). – Marco Bonelli Oct 07 '20 at 18:20
  • Yes I forgot to mention that thanks @MarcoBonelli , I am going to make a quick edit. – mitchell Oct 07 '20 at 18:24