Thread: Reading char matrix from file

  1. #1
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286

    Unhappy Reading char matrix from file

    Why does this program not work as expected? (Read the file test.txt and output it to stdout as written in the file)

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <assert.h>
    
    #define  GRIDWIDTH   10
    #define  GRIDHEIGHT  10
    
    FILE *fin;
    
    //#define POINTERS
    
    #ifdef POINTERS
    char *grid[GRIDHEIGHT];
    #else
    char grid[GRIDHEIGHT][GRIDWIDTH];
    #endif
    
    int main(int numargs, char **firstarg) {
    	fin  = fopen("test.txt",  "r");
    	int i, j;
    	int fx, fy, fd = 0;
    	int cx, cy, cd = 0;
    	for (i = 0; i < GRIDHEIGHT; ++i) {
            #ifdef POINTERS
            grid[i] = malloc(GRIDWIDTH);
            #endif
            #if 1
            for (j = 0; j < GRIDWIDTH; ++j) {
                grid[i][j] = getc(fin);
            }
            #else
            if (fread(grid[i], 1, GRIDWIDTH, fin) != GRIDWIDTH) {
                printf("Could not read all chars on the line\n");
                system("pause");
            }
            #endif
            fseek(fin, 1, SEEK_CUR);
        }
        for (i = 0; i < GRIDHEIGHT; ++i) {
            #if 1
            for (j = 0; j < GRIDWIDTH; ++j) {
                putc(grid[i][j], stdout);
            }
            #else
            if (fwrite(grid[i], 1, GRIDWIDTH, stdout) != GRIDWIDTH) {
                printf("Could not write all chars on the line\n");
                system("pause");
            }
            #endif
            printf("\n");
        }
        system("pause");
    }
    And here's test.txt:

    Code:
    *...*.....
    ......*...
    ...*...*..
    ..........
    ...*.F....
    *.....*...
    ...*......
    ..C......*
    ...*.*....
    .*.*......
    What happens is that it writes the first line correctly, but then the following lines all comes after one extra new line too much; instead they miss their last char. So the print to stdout looks like this:

    Code:
    *...*.....
    
    ......*..
    
    ...*...*.
    
    .........
    
    ...*.F...
    
    *.....*..
    
    ...*.....
    
    ..C......
    
    ...*.*...
    
    .*.*.....
    Why? I have tried different implementations as well, as you can see from the preprocessing code, but it doesn't matter.
    Last edited by TriKri; 07-17-2006 at 04:58 PM.

  2. #2
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    What's happening is it's interpreting the newline in the file as a part of the grid. You could simply do getc(fin); in order to skip the newline chracter after you've filled each row.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Use fgets() to read each line of text (including any trailing space or newline
    Use a loop to copy valid data from that line to the corresponding row of your board.
    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.

  4. #4
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286

    Lightbulb

    Quote Originally Posted by Brian
    What's happening is it's interpreting the newline in the file as a part of the grid. You could simply do getc(fin); in order to skip the newline chracter after you've filled each row.
    Aha, I understand what is wrong now. New line is two characters? But it is still strange that it just slips one character after when it has read the first line. After the second line it keeps laying one character behind. Fishy. Does it compensate for something?

  5. #5
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    Quote Originally Posted by TriKri
    Aha, I understand what is wrong now. New line is two characters? But it is still strange that it just slips one character after when it has read the first line. After the second line it keeps laying one character behind. Fishy. Does it compensate for something?
    Newline is one character in ascii mode and in binary mode it depends on the platform you're running.

  6. #6
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    OMG. Here we go again.

    What the heck is wrong with _read, _open, and _write? Why use text files here?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  4. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  5. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM