Results 1 to 5 of 5

Thread: [SOLVED] fgets in C seems to act a bit wierd

  1. #1
    Join Date
    Aug 2007
    Location
    Kottawa, Sri Lanka
    Beans
    7,387
    Distro
    Ubuntu 10.04 Lucid Lynx

    [SOLVED] fgets in C seems to act a bit wierd

    I'm using fgets to read the contents of a file line-by-line, this is the code:-
    Code:
    #include<stdio.h>
    #include<string.h>
    
    main(void){
    
      FILE *in , *out;
      char Names[10][17], temp[2];
      int NP;
    
      in = fopen("greedy.in","r");
      out = fopen("greedy.out","w");
    
      NP = atoi(fgets(temp, 2, in));
      int i;
      for (i = 0 ; i < NP +1 ; i++){
      fgets(Names[i], sizeof Names  , in);
    
      /* if (strlen(Names[0])== 1 ){ 
    i = 0;
      } */
      }
    
    }
    Now the problem I get is that although the first fgets works properly, the next fgets code takes the 1st line again and not the 2nd as it should. Is there something wrong in what I am doing above?

    Thanks in advance.
    Think carefully before executing commands containing "rm", especially "sudo rm -rf ", if you require more information concerning this matter, read this.
    I am an experimenter, give me the most stable OS and I can make it unstable in a few hours.

    C == seriously fast == FTW!

  2. #2
    Join Date
    Aug 2007
    Location
    Kottawa, Sri Lanka
    Beans
    7,387
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: fgets in C seems to act a bit wierd

    Ok, I figured it out(I know, it's stupid), it seems that I must use the same range between all uses of fgets, however, something I cannot get is as to why it is like this, can anyone enlighten me on this?

    Thanks!
    Think carefully before executing commands containing "rm", especially "sudo rm -rf ", if you require more information concerning this matter, read this.
    I am an experimenter, give me the most stable OS and I can make it unstable in a few hours.

    C == seriously fast == FTW!

  3. #3
    Join Date
    May 2008
    Location
    UK
    Beans
    1,451
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: fgets in C seems to act a bit wierd

    1 problems i can spot :

    your code is not reading to the end of the line - you only read two characters off the first line - but actually the first line might have more than two characters on it when you include the "\n" that is included by definition at the end of each line. Your code seems to assume that fgets automatically read the whole line, and just give you the first n bytes, and then throws the rest of the line away - that is not the case - it provides the next n bytes - off the same line if there is still characters left.
    Tony - Happy to try to help.
    Unless otherwise stated - all code posted by me is untested. Remember to Mark the Thread as Solved.
    Ubuntu user number # 24044 Projects : TimeWarp - on the fly Backups

  4. #4
    Join Date
    Aug 2007
    Location
    Kottawa, Sri Lanka
    Beans
    7,387
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: fgets in C seems to act a bit wierd

    Quote Originally Posted by Tony Flury View Post
    1 problems i can spot :

    your code is not reading to the end of the line - you only read two characters off the first line - but actually the first line might have more than two characters on it when you include the "\n" that is included by definition at the end of each line. Your code seems to assume that fgets automatically read the whole line, and just give you the first n bytes, and then throws the rest of the line away - that is not the case - it provides the next n bytes - off the same line if there is still characters left.
    Thanks for the explanation, that made me understand my mistake completely.
    Think carefully before executing commands containing "rm", especially "sudo rm -rf ", if you require more information concerning this matter, read this.
    I am an experimenter, give me the most stable OS and I can make it unstable in a few hours.

    C == seriously fast == FTW!

  5. #5
    Join Date
    Apr 2007
    Beans
    2,042

    Re: fgets in C seems to act a bit wierd

    also don't write your code like main (void) write it like this: int main() or int main(int argc, char **argv)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •