Thread: reading file then if statement not working

  1. #1
    Registered User
    Join Date
    Nov 2010
    Location
    In my house
    Posts
    32

    reading file then if statement not working

    I've been having a little problem with grabbing a word from a file then checking it using an if statement to see if it is true or not.
    when the code reaches the "printf("Line = ...." it prints out the variable, which is the first string then the second string, but when the if statement comes in, even though it the exact same that it is in the data file, it turns out to be false and doesn't execute.

    this is the concept code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main()
    {
    	FILE * in = fopen("DATA.txt", "r");
    	char line[255];
    	if (in == NULL)
    	{
    		printf("The input file was not found, or an error occured while reading the file");
    		return 1;
    	}
    	else
    	{
    		while (!feof(in))
    		{
    			fscanf(in, "%s", line);
    			printf("Line = %s\r\n", line); /* this was here to debug */
    			if (line == "Test:")
    			{
    				fscanf(in, "%s", line);
    				if(line == "True")
    				{
    					printf("This is a test");
    					getchar();
    				}
    				else
    				{
    					printf("This is not a test");
    					getchar();
    				}
    			}
    		}
    	}
    	return 0;
    }
    and the data file is
    Code:
    Test: True

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You need to compare strings with strcmp, or by hand, which is effectively the same thing. You can't compare strings in C with the comparison operator.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User hellork's Avatar
    Join Date
    Nov 2010
    Posts
    39
    I know, it was declared as an array, but the variable, line, oddly enough, is treated by the compiler as though it were a pointer.
    Cprogramming.com FAQ > Pointers And Arrays (intermediate)

    To get around this you could dereference the "pointers" one at a time and compare them, or use strcmp as quzah says, that's what strcmp does.

    The string literal, "True" can also be treated as though it were a pointer (because it is)...
    Code:
    if *line == *"T" && *(line+1) == *"r" && *(line+2) == *("True" + 2) ...
    if line[0] == 'T' && line[1] == 'r' && 2[line] == 2["True"] ...

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Also, don't use feof to control your loop: Cprogramming.com FAQ > Why it's bad to use feof() to control a loop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  4. Replies: 1
    Last Post: 08-31-2004, 04:07 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM