Thread: why is the last line not being red

  1. #1
    Registered User
    Join Date
    Apr 2010
    Location
    Vancouver
    Posts
    132

    why is the last line not being red

    I red the first line of the file and it gave the rows (rows) and columns (cols) using fscanf(). There is a global variable called fpin1, that's the file I'm reading from. The last line is chopped off.
    Code:
    void readImg(char Image1[MAXROW][MAXCOL],int rows, int cols)
    {
        printf("rows: %d cols: %d", rows, cols);
        int i;
        for(i = 0; i < rows; i++)//if the condition is <= it works as expected
        {
            fgets(Image1[i], MAXCOL, fpin1);
            printf("%s", Image1[i]);
        }
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by c_weed
    I red the first line of the file and it gave the rows (rows) and columns (cols) using fscanf().
    Chances are, that left the newline character unprocessed. Therefore, your first read of the actual data consumed that newline instead of reading the data that you wanted.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Are the values of rows and cols the true number of rows and columns or are they the index values of the last row and column ...

    If they are index values than <= is appropriate. Just be sure to make your arrays[rows+1][cols+1] in size, in this case.

  4. #4
    Registered User
    Join Date
    Apr 2010
    Location
    Vancouver
    Posts
    132
    @LasserLight your right! This should do it
    Code:
    fgets(NULL, 1, fpin1);
    @CommonTatter what do you mean by index?

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by c_weed View Post
    @LasserLight your right! This should do it
    Code:
    fgets(NULL, 1, fpin1);
    Are you trying to make your program crash?


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    @OP:
    fgets(). The internet really is a wonderful thing.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by c_weed View Post
    I red the first line of the file and it gave the rows (rows) and columns (cols) using fscanf(). There is a global variable called fpin1, that's the file I'm reading from. The last line is chopped off.
    Code:
    void readImg(char Image1[MAXROW][MAXCOL],int rows, int cols)
    {
        printf("rows: %d cols: %d", rows, cols);
        int i;
        for(i = 0; i < rows; i++)//if the condition is <= it works as expected
        {
            fgets(Image1[i], MAXCOL, fpin1);
            printf("%s", Image1[i]);
        }
    }
    There. I fixed it. Now the last line is being red.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Oct 2011
    Location
    india
    Posts
    18
    fgets() cannot be used here.the use of fgets is demonstrated below.Consider the following code segment
    Code:
     while(feof()!=Null)
      {
        fgets(array,25,filepointer);
        printf("%s",array);
     }
    then the first 25 characters in the file pointed by filepointer is copied to array.
    now think whats wrong with your code

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by vikasvijayan View Post
    fgets() cannot be used here.
    You can't really make that statement, since you don't actually know what his file looks like.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by vikasvijayan
    fgets() cannot be used here
    On the contrary, it can. Whether it is the best approach is another matter, but so far it could well be.

    Quote Originally Posted by vikasvijayan
    the use of fgets is demonstrated below.
    Badly demonstrated. One thing missing from c_weed's code is a check of the return value of fgets. Your example does no better, and in fact does worse because your call of feof is wrong both in that it is missing its argument and in that controlling the loop with feof like this is a wrong approach.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by c_weed View Post
    @CommonTatter what do you mean by index?
    A common error made by programmers is to confuse the "number of elements" (size) with the "number of the element" (index)

    For example... if you define an array[10]... you get valid indexes number 0 through 9... for a total of 10 elements. However the last element's index is 9, not 10

    This can cause confusion in loops where you are tracking the last used index since it will never reach the actual size value of the array... Thus if you read data and the count stops at 9 ... you've actually read 10 elements... and need an array of 10 to store it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 21
    Last Post: 08-07-2011, 09:55 PM
  2. Read text file line by line and write lines to other files
    By magische_vogel in forum C Programming
    Replies: 10
    Last Post: 01-23-2011, 10:51 AM
  3. Replies: 7
    Last Post: 12-13-2010, 02:13 PM
  4. Read strings line by line until Ctrl+C is pressed
    By r00t in forum C Programming
    Replies: 25
    Last Post: 11-17-2009, 04:18 AM
  5. Replies: 1
    Last Post: 05-20-2006, 11:17 PM