0
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].

3
  • If you want to compare one character, use 't', not "t" (single-quotes, not double). Commented May 3, 2014 at 19:46
  • 3
    TURN ON YOUR COMPILER WARNINGS. No excuse. Turn the damn warnings on and pay attention to them. (And next time read that beginner's tutorial attentively.) Commented May 3, 2014 at 19:48
  • I'm using gcc in a terminal kind of hard to read them, I'm acutally not new at this but forgot the difference between " and ' Commented May 3, 2014 at 19:51

2 Answers 2

1

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).
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.