Find the answer to your Linux question:
Results 1 to 4 of 4
Hi. I am trying to read from a file named matrixA.dat that contains a matrix formatted like this: 2 x 3 1 0 2 -1 3 1 I am reading ...
  1. #1
    Just Joined!
    Join Date
    Jan 2011
    Location
    Texas
    Posts
    3

    [SOLVED] Help using fgets() in C

    Hi. I am trying to read from a file named matrixA.dat that contains a matrix formatted like this:
    2 x 3
    1 0 2 -1 3 1

    I am reading the lines in, and this is the source that I have so far:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define DATALIMIT 17
    #define DIMLIMIT 5
    #define NAMELIMIT 40
    
    /* Data line limit is 17 to account for the case that all six
     * values are negative.
     */
    
    int main()
    {
      /* Declare variables */
      int rowA, colA, rowB, colB;
      char matA[NAMELIMIT], matB[NAMELIMIT];
      char dimA[DIMLIMIT+1], dimB[DIMLIMIT+1], dataA[DATALIMIT+1], dataB[DATALIMIT+1];
      FILE *fd;
      
      /* Define filenames */
      strcpy(matA,"matrixA.dat");
      strcpy(matB,"matrixB.dat");
    
      /* Open matrixA.dat file */
      fd = fopen(matA, "r");
      if (fd == NULL)
        perror("Error opening file.");
    
      /* Read dimensions and data */
      fgets(dimA,DIMLIMIT+1,fd);
      fgets(dataA,DATALIMIT+1,fd);
    
      printf("%s\n%s\n",dimA,dataA);
    
      return 0;
    }
    The program will do various other things with the matrices, but I am testing whether I can read in matrix A, and then I will continue. When I run the code I get this output:
    $ ./a.out
    2 x 3


    $
    It looks like a \n was stored in dataA, so when I print it all I get is a newline. Why didn't it grab the 1 0 2 -1 3 1 line? Thanks!

  2. #2
    Just Joined!
    Join Date
    Jan 2011
    Location
    Texas
    Posts
    3
    UPDATE: I ended up just dumping the characters one by one into a buffer until fgetc()=='\n' (namely, end of line). Altough this works, it looks really sloppy with all the looping. I still would appreciate any help with using fgets(). Thanks!

  3. #3
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    Congratulations! You've discovered some of the fun difficulties of I/O!

    Your problem here is related to the sizes of everything that you're using.

    fgets() will read up to ONE FEWER than the number of characters that you tell it. This is because it needs space to store the NUL byte at the end of the string. So you are telling it that your buffer is six characters long. So it reads up to five characters:

    "2 x 3"

    It does NOT read the newline at the end. It then returns.

    Your next fgets() requests up to 17 characters. So fgets() looks at the stream. Oh! The next character is a newline. Well, that's the end of the line, so it returns after putting the newline in the buffer.

    The short way around this is to use more reasonable buffer sizes. You have no idea how long your lines might be, so use something large (BUFSIZ is a constant and is usually a good size). Memory is cheap these days, so having a big buffer doesn't hurt anyone. You can also ensure that after reading a line, the last character in the buffer before the NUL byte is a '\n', which will make sure that you read a whole line.

    Good luck with this!
    DISTRO=Arch
    Registered Linux User #388732

  4. #4
    Just Joined!
    Join Date
    Jan 2011
    Location
    Texas
    Posts
    3
    That did the trick. Thanks a bunch!

Posting Permissions

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