Question

FILE *t;
t = fopen(argv[8], "r");
fgets(tmp, 2048, t);

What I am reading at each line is something like this "l 23 23" a letter and some numbers (int or float) depending on weather "l" is "a" "b" or "c".

I've tried but couldn't compare tmp[0] with a letter.

tmp[0] =="t"

I know for sure "t" is present in the file but it always gives false.

How do I compare it and also extract the numbers that follow?

PS: I know how many number and what type to expect at each line depending on the value of tmp[0].

Était-ce utile?

La solution

If you want to compare the first character in tmp with the character 'x', then use:

if (tmp[0] == 'x')
{
    ...
}

If you want to compare the entire string in tmp with the string "xyz", then use:

if (strcmp(tmp,"xyz") == 0)
{
    ...
}

Behind the scene:

  • When you use a double-quoted string of characters in your code, the compiler replaces it with a pointer to the beginning of the memory space in which the string will reside during runtime.
  • So by tmp[0] == "t", you are actually attempting to compare a single character (typically 8 bits of data) with a memory address (typically 32 or 64 bits of data, depending on your system).

Autres conseils

If you want to compare one character like that, use 't', not "t" (single-quotes, not double).

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top