2

I have a text file like this

"input"
height : 227
width : 227
depth : 3

"conv"
num_output : 96
pad : 0
kernel_size : 11
stride : 4
group : 1

"relu"

"pool"
kernel_size : 3
stride : 2

I'm reading it in a loop (this is partial code)

char line[100];

while ((fgets(line, sizeof(line), filePtr))) {
    if (line[0] != "\n") {
        sscanf(line, "%15s : %15s", tmpstr1, tmpstr2);
        printf("%s\n",  tmpstr2);
        printf("line = %s", line);
    } else
        break;
}

But I observed that the if condition always holds true and the output is as below

"input"
227
line = height : 227
227
line = width : 227
3
line = depth : 3
3
line = 
3
line = "conv"
96
line = num_output : 96
0
line = pad : 0
11
line = kernel_size : 11
4
line = stride : 4
1
line = group : 1
1
line = 
1
line = "relu"
1
line = 
1
line = "pool"
3
line = kernel_size : 3
2
line = stride : 2

I have tried comparison with \0 as well but the result doesn't change. Please point me where I'm going wrong.

P.S. : I'm using Ubuntu 16.04 64-bit machine with gcc 5.2.1.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
Harsh Wardhan
  • 2,110
  • 10
  • 36
  • 51
  • 6
    `line[0] != "\n"` --> `line[0] != '\n'` – BLUEPIXY Aug 15 '17 at 19:44
  • 3
    You are comparing `char` with an string literal, you should turn on compiler warnings. `gcc` -pedantic -Werror – kocica Aug 15 '17 at 19:48
  • 1
    @BLUEPIXY Thanks for pointing it out. Totally overlooked that typo. – Harsh Wardhan Aug 15 '17 at 20:01
  • I closed it as a duplicate for 'string comparison' but it can also be regarded as character comparison. The condition `if (line[0] != "\n") {` compares a `char` with a `char *`. It can be fixed by using `if (strcmp(line, "\n") != 0)` or by using `if (line[0] != '\n')` — both work in this context. For general string comparison (longer strings), use `strcmp()`. – Jonathan Leffler Aug 15 '17 at 23:36

4 Answers4

5

Newline is a character, not a string, so change this:

line[0] != "\n"

to this:

line[0] != '\n'

Enable compiler warnings (-Wall flag in GCC) and you should see something like this:

warning: comparison between pointer and integer
warning: comparison with string literal results in unspecified behavior [-Waddress]
gsamaras
  • 71,951
  • 46
  • 188
  • 305
4

You are trying to compare an string literal with char.

With:

if(line[0] != '\n')

It works well.

If you're reading from a file that's been opened in text mode (including stdin), then whatever representation the underlying system uses to mark the end of a line will be translated to a single '\n' character.

You should turn on compiler warnings. For gcc it's -pedantic -Werror.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
kocica
  • 6,412
  • 2
  • 14
  • 35
0

On this site we can see that fgets doesn't change the value of str (line in your case). Because the value does not change, your if test keeps getting evaluated to true.

If you want to check whether fgets found the end of the file, you have to check if fgets returned NULL.

Erik
  • 153
  • 8
0

You are comparing a character to a string: line[0] != "\n" should produce a warning. To detect an empty line, use this instead:

line[0] != '\n'

Note that you should also verify the return value of sscanf():

char line[100];

while (fgets(line, sizeof(line), filePtr) && *line != '\n') {
    if (sscanf(line, "%15s : %15s", tmpstr1, tmpstr2) == 2) {
        printf("%s\n",  tmpstr2);
        printf("line = %s", line);
    } else {
        printf("invalid format: %s", line);
    }
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189