Like Tree1Likes
  • 1 Post By MK27

Problems in file reading

This is a discussion on Problems in file reading within the C Programming forums, part of the General Programming Boards category; I am writing a c program to read individual lines from a file and check if any line(in this case ...

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    4

    Problems in file reading

    I am writing a c program to read individual lines from a file and check if any line(in this case a single word) is equal to hello.

    i am using fgets function.

    The length of the lines in the text file are unknown, thus i use an array variable buff that is of a considerate length.

    But strcmp never returned 0. I thought that fgets was taking in extra space for the remaing length of the array so i transferred the contents of buff into another array called tmp.

    Size of tmp=strlen(buff).

    But i am getting a different out put.
    Here is the code:
    Code:
    #include<stdio.h>
    #include<string.h>
    
    int main()
    {
        FILE *fp;
        char buff[128];
        fp=fopen("fi.txt","r");
        if(fp==NULL)
        {printf("HELLO \n");printf("File does not exist");}
        else{printf("FILE EXISTS \n");
        printf("Debugging stage loc 1==");
        while(fgets(buff,sizeof buff,fp) !=NULL){
        char tmp[strlen(buff)];
        printf("%d tmp length=",(int)strlen(tmp));
        printf("%d buff length=",(int)strlen(buff));    printf("\n");
        strcpy(tmp,buff);
        printf("\n");puts(buff);
        puts(tmp);
        printf("\n");
        printf("%d",strcmp(tmp,"hello"));
        printf("\n");break;
        }}
        return 0;
    }
    And here are the contents of the file:

    hello
    poll

    And here is the output:

    FILE EXISTS
    Debugging stage loc 1==0 tmp length=6 buff length=

    hello

    hello


    1

    Q1) What is the 0 after debugging location 1? Where is it coming
    from?

    Q2) tmp length is alright but why is buff length not being displayed?

    Q3) if both values are hello, then why is strcmp returning 1 and not 0?

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    FYI, strlen() does not include the null terminator, so tmp needs to be strlen(buf) + 1 for a copy. That is important. Also, please learn to indent properly.

    Q1) What is the 0 after debugging location 1? Where is it coming
    from?

    Q2) tmp length is alright but why is buff length not being displayed?
    Because you do not have newlines in your printfs, and you do not actually put the lengths after the =, you put it first:
    Code:
        printf("Debugging stage loc 1==");
     [...]
        printf("%d tmp length=",(int)strlen(tmp));
        printf("%d buff length=",(int)strlen(buff));    printf("\n");
    0 is the length of tmp, which could actually be anything since it is unintialized (but your compiler may use zero'd memory for some reason, it which case it will be consistent, but don't count on that). 6 is the length of buf. strlen() does not give you the size of an array, it gives you the number of characters in a c-string, which is everything upto a '\0'. That may be less or (if you screw up) more than the size of the array.

    Q3) if both values are hello, then why is strcmp returning 1 and not 0?
    Because both values are not "hello". Why is the strlen() of buf 6? "hello" would be 5 -- but I bet it is actually "hello\n", since fgets reads upto and including a newline.
    inu11byte likes this.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    4
    Thank you.

    It has been a long time since i have programmed in c. I was doing java and c++, so i am just out of touch.

    Your reply really helped a lot.

    Than you very much.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File reading problems
    By VR6_Nut in forum C Programming
    Replies: 3
    Last Post: 02-19-2009, 05:55 PM
  2. File-reading problems
    By kinghajj in forum C Programming
    Replies: 29
    Last Post: 03-27-2004, 11:25 PM
  3. Reading from file problems
    By highlands in forum C++ Programming
    Replies: 3
    Last Post: 03-22-2004, 12:50 PM
  4. file reading problems
    By robid1 in forum C Programming
    Replies: 8
    Last Post: 06-26-2003, 08:28 AM
  5. file reading problems again
    By bluebob in forum C Programming
    Replies: 8
    Last Post: 05-01-2002, 05:29 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21