Thread: Is this a problem with strcmp?

  1. #1
    Registered User
    Join Date
    Oct 2011
    Location
    Ottawa
    Posts
    17

    Is this a problem with strcmp?

    Hello again so soon,

    Still writing for a file based database, but now I'm trying to search for key strings to retrieve specific lines of data.

    My problem is that even though I can see that my two strings for comparison are identical when I print them, the strcmp function refuses to acknowledge them as such.

    Have I used it wrong? Or perhaps made another error in my code?

    Code:
    char ViewInfo()
    {
        FILE *fptr;
    
    
        fptr=fopen("E://StudentFile.txt", "r");
    
    
        char StudentSearch[9], FileString[9], Line[130], Name[20], n[10];
    
    
        if (fptr == NULL)
            printf("Unable to open file");
    
    
        else
        {
            printf("File opened\n");
            char StudentSearch[10], FileString[10], Line[130];
    
    
            fputs("Please enter the student number of the student whose information you would like to view: ", stdout);
    
    
            char c=getchar();
            while (c != '\n')
                c=getchar();
            fflush(stdout);
    
    
            if (fgets(StudentSearch, 9, stdin) != NULL && strlen(StudentSearch)==8)
            {
    
    
                printf("Valid input is %s", StudentSearch);
    
    
    
    
                do
                {
                    fscanf (fptr, "%s", FileString);
                    printf("The file says %s\n", FileString);
    
    
                    scanf("%s", n);
    
    
                }
                while(strcmp(StudentSearch, FileString) != 0);
    
    
                printf("Yup");
                fgets(Line, 130, fptr);
                printf("%s\n", Line);
    
    
            }
            else
                printf("Invalid Input. Student number must be 7 digits long");
    
    
    
    
        }
        fclose(fptr);
    Any tips appreciated, thanks!

  2. #2
    Registered User
    Join Date
    Oct 2011
    Location
    Ottawa
    Posts
    17
    line 46 " scanf("%s", n); " was just to prevent the program crashing in an endless loop and so I could see the progress of the file pointer.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quick guess one has an linefeed on the end (fgets) and the other does not.

    Tim S.

  4. #4
    Registered User
    Join Date
    Oct 2011
    Location
    Ottawa
    Posts
    17
    What might be a way to fix that? I read somewhere about a Replace function that worked like this:

    StudentSearch.Replace("\n\r","");

    but this makes absolutely no sense to me and it didn't work anyway.

    Also StudentSearch=StudentSearch.Trim(' ', '\n', '\r'); didn't work.
    Last edited by daynorb; 11-28-2011 at 10:19 PM.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > What might be a way to fix that?
    How about the one shown in your previous thread?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Oct 2011
    Location
    Ottawa
    Posts
    17
    Are you referring to this?
    Code:
    char *newline = strchr(FileString, ' ');
                    if ( newline != NULL )
                    {
                        *newline = '\0';
                        printf("\nNewline Removed");
                    }
    If so, what character should I be looking for? I'm still not familiar enough with c to really understand when to use what, so I've already tried ' ', '\n', '\r', and '\n\r' to no avail.

  7. #7
    Registered User
    Join Date
    Oct 2011
    Location
    Ottawa
    Posts
    17
    Thanks for the help!
    The issue was indeed the linefeed in the gets() value, which was removed using the above method.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with strcmp
    By byebyebyezzz in forum C++ Programming
    Replies: 8
    Last Post: 06-27-2011, 10:20 PM
  2. strcmp problem in c
    By Prodiga1 in forum C Programming
    Replies: 2
    Last Post: 11-23-2010, 12:13 AM
  3. strcmp problem, whats the problem, i cant figure it out!
    By AvaGodess in forum C Programming
    Replies: 14
    Last Post: 10-18-2008, 06:45 PM
  4. strcmp problem
    By cstudent in forum C Programming
    Replies: 3
    Last Post: 04-15-2008, 09:48 AM
  5. problem with strcmp
    By maditsi in forum C Programming
    Replies: 1
    Last Post: 12-07-2001, 12:38 AM

Tags for this Thread