Reading an array of a file

Asked

Viewed 400 times

4

I am wanting to read from a file that has an array inside, the amount of rows and columns and printable on the screen, but my program does not appear anything, it simply stops and does not print the number of rows(counterL) and columns(counterC).

The program is just below:

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

/*
A funcao abaixo tem como utilidade a alocacao dinamica de uma matriz
*/

int** alocaMatriz(int linha, int coluna)
{
    int i, j;
    int** matriz = NULL;

    for(i = 0; i < linha; i++)
    {
        matriz = (int**) malloc(linha * sizeof(int*));
        for(j = 0; j < coluna; j++)
        {
            matriz[i] = (int*) malloc(coluna * sizeof(int));
        }
    }

    return matriz;
}

/*
A funcao abaixo tem como utilidade a liberacao de memoria de uma matriz alocada
dinamicamente
*/

void liberaMatrizes()
{

}

int main (void)
{

    int aux, counterL = 0, counterC = 0;
    int** matrizRetorno = NULL;
    char* nomeArquivo = NULL;
    size_t tamanho = 0;
    FILE * ponteiro;


    getline(&nomeArquivo, &tamanho, stdin);

    nomeArquivo[strlen(nomeArquivo) - 1] = '\0';

    ponteiro = fopen(nomeArquivo, "r");

    while(fscanf(ponteiro, "%d", &aux) != '\n')
    {
        counterC++;
        if(fscanf(ponteiro, "%d", &aux) == '\n')
            counterL++;
    }

    printf("%d\n%d\n", counterL, counterC);

    matrizRetorno = alocaMatriz(counterL, counterC);

    fclose(ponteiro);

    liberaMatrizes();

    return 0;
}

2 answers

1

Verified results

I compiled your program (after mine amendments) and I’ll tell you it doesn’t work. The function you should use is fgets. See this.

Solution

To read a file using fgets we loop, it returns NULL when there are no characters to read or EOF. The second argument from fgetsis the number of characters to be read, usually the size of the string you passed. The third is the pointer to the file. The function stops reading the line when it encounters a \n, or \0 or the end of the file EOF.

So how do we do the math?

The number of rows will be the number of lines read and the number of columns the maximum number of characters read in a row. In this case I chose 20, but you can change.

    char lido[20];
   while(fgets(lido, 20, ponteiro))
   {
       if(counterC < (strlen(lido)-1)) //Pois inclui o \n e o \0
   {
       counterC = (strlen(lido)-1);

     }
     counterL++;


    }

To add

The function getline sets the terminator character automatically.

getline() reads an entire line from stream, storing the address of the buffer containing the text into *lineptr. The buffer is null- terminated and includes the newline Character, if one was found.

Basically puts the terminator character and a \n, if this is found.

Compilation result

Note that I used a file containing the following:

ab
cv
ed

This was the output( test is the name of the executable after compiled ):

C:\Users\NAMS\Desktop>teste
3
2
  • But I do not what the length of rows and columns of my file, the person inserts the txt file and my program counts

  • I don’t know if you read the second link I sent you. Basically if the scanf fails you can leave your pointer in an unknown location bringing future problems. In the case of the size of the rows and columns can always create a string with a size like 4098 or 10240, I doubt that someone write so much in a row and if there should be a troll.

0

    while(fscanf(ponteiro, "%d", &aux) != '\n')
    {
        counterC++;
        if(fscanf(ponteiro, "%d", &aux) == '\n')
            counterL++;
    }

This is an infinite cycle. The value returned by the first fscanf() NEVER will equal '\n'.

Each member of the function family scanf returns the number of assignments made (which may be 0) or EOF in case of error. In your case, as you have only one possible assignment the value returned will be one of the three { EOF, 0, 1 }

I suppose what you want to do is read two numbers from the top of the file without worrying about blanks or line breaks.
Try it like this

   if (fscanf(ponteiro, "%d%d", &counterC, &counterL) != 2) /* erro */;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.