Thread: Help with fgets and strcpy - returning 3!

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    2

    Unhappy Help with fgets and strcpy - returning 3!

    Hello all,

    I have spent the better part of today trying to figure this out.

    I've asked elsewhere and searched around a lot but I cannot figure it out.

    I have searched around on here as well as other sites for potential explanations for this, and I believe it is something to do with the \n or the \0 that is added when using fgets. Maybe or maybe not.
    At the moment I have a list of words (one per line) in a text file that I read into an array, I use the following code to read the file into an array:

    Code:
    int i = 0; 
       while(fgets(buffer,sizeof buffer, fileIn) != NULL) { 
       list[i] = malloc(strlen(buffer) + 1); 
       strcpy(list[i], buffer);
        i++;
    }
      

    This works as intended, reading the file into the array.

    I then ask the user to input a word... Also using fgets.

    Code:
        printf("Please enter the word to compare.\n"); 
       fgets(compword, sizeof(compword),stdin);
    

    I then compare the two and print the result of the comparison...


    Code:
        int result = strcmp(list[0], compword);    printf("Result: %d\n", result);
    

    However the result, I am using the first word which is 'test' is 3... Before I was using a scanf to take the user input, and the result would be 13, so I thought I would try using the same way as the file is read in, and the result of the comparison is now 3.

    I know that fgets adds the \n to the end of strings, which is what I think caused scanf to compare and return 13, but now I don't know what could be the difference? In the text document they are just the words, no extra spaces etc.

    Can anyone help with this? I'm really not sure anymore what could be causing it. If you can't is there any way I could do it so that they do equal the same thing? :/

    Also, I am on a mac computer using the terminal.
    I have also tried it in xcode now and the result is the same.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    I do not really know what you mean by this "I am using the first word which is 'test' is 3".
    I know however that strcmp returns
    Code:
    Returns an integral value indicating the relationship between the strings:
    A zero value indicates that both strings are equal.
    A value greater than zero indicates that the first character that does not match has a greater value in str1 than in str2; And a value less than zero indicates the opposite.
    source : strcmp
    In your case str1 is the list and str2 is the compword.

    Also, notice, that we usually are interested for the sign of the result, rather than its actual value. As a result, if the return value of strcmp is 3 or 13, it doesn't really matter, since its a positive return value.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    2
    Quote Originally Posted by std10093 View Post
    I do not really know what you mean by this "I am using the first word which is 'test' is 3".
    I know however that strcmp returns
    Code:
    Returns an integral value indicating the relationship between the strings:
    A zero value indicates that both strings are equal.
    A value greater than zero indicates that the first character that does not match has a greater value in str1 than in str2; And a value less than zero indicates the opposite.
    source : strcmp
    In your case str1 is the list and str2 is the compword.

    Also, notice, that we usually are interested for the sign of the result, rather than its actual value. As a result, if the return value of strcmp is 3 or 13, it doesn't really matter, since its a positive return value.

    Thanks, I meant that the word I am comparing in this case is 'test' which is the first word I read in from the text file.

    I need to be able to compare them and get a correct return (0) for strings I know are equal but the program does not think so. As I said above, I know fgets adds \n's and \0's on the end so I thought it might be that, so I switched from using scanf to fgets for the user input, the end result however is still only 3 meaning the strings are in some way unequal.

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    I have an idea. Try to debug it yourself. It will feel goooooood

    Right before the line of code that you use strcmp, use a loop to print every character of both words, to see by yourself what is happening. strcmp compares the words, by comparing the characters of them.

    Example
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
            char* str1 = "teest";
            char* str2 = "test";
    
            int i, length;
    
            /* Will put +1 in the for loop to print the null terminator too   */
            /* Notice, that I use |the currenct character| to see exactly what*/
            /* every character is. Helps with spaces!                         */
            for(i = 0, length = strlen(str1)+1 ; i < length ; i++)
                    printf("|%c|\n", str1[i]);
            for(i = 0, length = strlen(str2)+1 ; i < length ; i++)
                    printf("|%c|\n", str2[i]);
    
            printf("Strings are%s equal\n", strcmp(str1, str2) ? " not" : "");
    
            return 0;
    }
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    It does sound like '\n' causing the problem

    Add this for debugging
    Code:
    printf("list[0]=<%s> \ncompword=<%s>",list[0], compword);
    See if there is a new line at the end
    Fact - Beethoven wrote his first symphony in C

  6. #6
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Your file is using CR (0x0D) for a newline and your terminal is using LF (0x0A). That's why scanf produced a difference of 13 = 0x0D - 0, and fgets a difference of 3 = 0x0D - 0x0A.

    Write a function to trim all '\n' and '\r' from the end of a string, and pass both through before comparing.

  7. #7
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    If that is the case, you can't go past Nominal Animal's suggestion from this thread

    http://cboard.cprogramming.com/c-pro...le-thread.html

    It quickly removes \n and \r and anything else you want
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Returning an integer ... not returning; weird error
    By Imanuel in forum C++ Programming
    Replies: 19
    Last Post: 09-25-2011, 01:30 PM
  2. Replies: 5
    Last Post: 09-06-2011, 02:59 PM
  3. Replies: 1
    Last Post: 06-19-2010, 07:42 AM
  4. Recursion: base case returning 1, function returning 0
    By yougene in forum C Programming
    Replies: 5
    Last Post: 09-07-2007, 05:38 PM
  5. fgets, fflush, strcpy...
    By blanny in forum C Programming
    Replies: 4
    Last Post: 05-11-2004, 04:14 PM

Tags for this Thread