patriques
patriques

Reputation: 5201

How to read number of chars from line in file

Hi i have this program that reads from a file with chars with the format

#00
000
000

But when I read the lines with fgetc until line break, but when i print out the number of chars they count as 4 but should be 3 (0-3). Why does this happend?

#include <stdio.h>
#include <stdlib.h>

int
main(void)
{
    //int row, col; BEFORE.
    int row=0, col=0; //UPDATE
    /* check controls that read columns are the same size  */
    int check=0, i;
    FILE * file;
    char c;
    char **matrix;
    matrix = malloc(sizeof *matrix * 4);
    for (i = 0; i < 4; i++)
        matrix[i] = malloc(sizeof *matrix[i] * 4);
    file=fopen("map.map", "r");
    while ((c = fgetc(file)) != EOF)
    {
        matrix[row][col]=c;
        if (matrix[row][col]=='\n')
        {
            if(row==0)
               check=col;
            printf("%d\n", check);
            row++;
            if(check!=col)
               return -1;
            check=col;
            col=0;
        }
        else 
            col++;
     }
     fclose(file);
     printf("%d \n", check);
     return 0;
}

UPDATE

I have debugged the program and discovered that the fgetc that reads the file with the chars

#00
000
000

is in the beginning reading a '\0' and then starting to read '#00...' So to fix the the problem one has to delete this char from the buffer. Then like commented the end of col reads '/r' and later '/n' (in system: Mac OS X Lion 10.7.4) so one has to have that in mind.

NEW VERSION

#include <stdio.h>
#include <stdlib.h>

int
main(void)
{
    int row=0, col=0;
    /* check controls that read columns are the same size  */
    int linelen=0, i;
    FILE * file;
    char c;
    char matrix[3][3];
    file=fopen("map.map", "r");
    /* have to read the first char because its a '\0' in the beginning */
    c=fgetc(file);
    if(c!=EOF)
    {
        while ((c=fgetc(file)) != EOF)
        {
            /* to jump over '\r' thats from old systems */
            if(c!='\r' || c!='\n')
            {
                matrix[row][col]=c;
                col++;
            }
            if(c=='\n')
            {
                /* first time definition of line length */
                if(row==0)
                    linelen=col;
                printf("%d\n", linelen);
                row++;
                if(linelen!=col)
                    return -1;
                col=0;
            }
        }
    }
    else
        printf("El archivo esta vacio\n");
    fclose(file);
    printf("%d\n", linelen);
    printf("%d, %d\n", row, col);
    return 0;
}

When I debug this program then it says Im accessing bad memory.

Bug report:

.....
.....
Breakpoint 1, main () at mapaprearmado.c:25
25          while ((c=fgetc(file)) != EOF)
(gdb) print c 
$25 = 13 '\r'
(gdb) print row
$26 = 1
(gdb) print col
$27 = 4
(gdb) step
.....
.....
(gdb) step
Cannot access memory at address 0x0
0x0000000100000714 in start ()

What am I not getting...

Upvotes: 0

Views: 318

Answers (1)

jheriko
jheriko

Reputation: 3113

If this is a windows style text file the problem could be as simple as the \r preceding the \n

There is a historical thing where mac uses \r *nix uses \n and windows uses \r\n to try and be as cross compatible as possible. So the line ending is actually two characters. try changing:

if (matrix[row][col]=='\n')

for

if ((matrix[row][col]=='\r') || (matrix[row][col]=='\n'))

this should work for all platforms - although you will need to skip any \r or \n that come after the line break, and detecting multiple line breaks if you need to will need a bit of thought...

Upvotes: 3

Related Questions