Reading an array from a file

4

I want 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 start the number of lines (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] = '
#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] = '%pre%';

    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;
}
'; 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; }
    
asked by anonymous 28.06.2015 / 03:59

2 answers

1

Verified results

I've compiled your program (after my changes ) and I'll tell you it does not work. The function you should use is fgets . See this .

Solution

To read a file using the fgets loop, it returns NULL when there are no characters to read or% with%. The second argument of EOF is the number of characters to be read, usually the size of the string that has passed. The third is the pointer to the file. The function to read the line when it finds a fgets , or \n or the end of the EOF file.

So how do we do the accounts?

The number of lines will be the number of rows read and the number of rows 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 
ab
cv
ed
{ counterC = (strlen(lido)-1); } counterL++; }

To add

The getline function automatically places the terminator character.

  

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 it is found.

Compilation result

Notice that I used a file that contained the following:

C:\Users\NAMS\Desktop>teste
3
2

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

    char lido[20];
   while(fgets(lido, 20, ponteiro))
   {
       if(counterC < (strlen(lido)-1)) //Pois inclui o \n e o 
ab
cv
ed
{ counterC = (strlen(lido)-1); } counterL++; }
    
28.06.2015 / 13:01
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% with% NEVER will be equal to fscanf() .

Each member of the '\n' function family returns the number of assignments made (which can be scanf ) or 0 in case of error. In your case, since you have only one possible assignment, the value returned will be one of the three EOF

I suppose what you want to do is read two numbers from the beginning of the file without worrying about whitespace or line breaks. Try it out

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