C program does not correctly read a . txt file with integer numbers

Asked

Viewed 820 times

0

I’m with this C code where the goal is to read 25 whole numbers of a text file and store them in a 5 by 5 array. The problem is that when running on MS-DOS, the program prints only junk from memory. I believe it’s the fgets(...). Is there a similar command for integers? Can anyone help in the solution?

Here is an excerpt from the code referring to the problem:

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

int main()
{

int matriz_A[5][5];
int matriz_B[3][2];
int matriz_C[3][2];
int m, n;

printf("\n Abaixo temos a matriz A \n\n\n");

FILE *matrizA;
matrizA = fopen("matriz_A.txt", "r");

if (matrizA == NULL)
{
    printf("\nNão foi possivel abrir o arquivo. \n");
    exit(0);
}

while (fgets(matriz_A, 25, matrizA) != NULL);
{
    for (m = 0; m<5; m++)
    {
        for (n = 0; n<5; n++)
        {
            printf(" %i ", matriz_A[m][n]);
        }

        printf("\n\n");
    }
}

system("pause");
return 0;

}

Exit after run:

inserir a descrição da imagem aqui

  • 1

    The fgets function reads a string of characters until it finds a line end ('n') or reaches the limit of the specified number of characters. If your text file contains numbers you can directly read the numbers with the fscanf function or read the string and then handle the string, for example with the sscanf function.

  • You can use two loops for (m=0; m<5; m++) for (n=0; n<5; n++) fscanf(matrizA, "%d", &matriz_A[m][n]);

2 answers

1

Greetings

You will need to do the process in two steps:

1 - You will have to read the file and enter the data in the matrix;

2 - After entering the data in the matrix ai yes you can read

What is happening is that you are printing BEFORE you have anything inside the matrix, fgets does not work the way you are thinking!

You didn’t post the input file, I’m guessing it’s something like this:

10 20 30 40 50
10 20 30 40 50
10 20 30 40 50
10 20 30 40 50
10 20 30 40 50

My suggestion is the following, first read the entries using something like:

FILE *file = fopen ( "matriz_A.txt", "r" );
if ( file == NULL ) {
  printf("Erro ao ler arquivo.");
}
char s[10];
while ( fgets ( s, sizeof s, file ) != NULL ) {
  printf("%s",s);
}

In place of the printf you will have to do the treatment to insert in the correct position of the matrix! Use an auxiliary variable to go counting!

To convert from string to integer use the atoi function.

Good luck with your exercise.

1

Let’s see...

Consider the file with matrix data arranged as follows:

01 02 03 04 05
06 07 08 09 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25

... Or even as follows:

01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

If you read the file data to the matrix with the following algorithm:

for( m = 0; m < 5; m++ ){

    for( n = 0; n < 5; n++ ) fscanf( matrizA, "%d%*c", &matriz_A[m][n] );

    }

... You’ll see that everything will work!

Explaining

The sub-specifier * used with the specifiers % and c (forming %*c), indicates that it is to ignore any type of data of the specified type that is being read. That is, it will not be stored but ignored. Note that the order matters and * should come right after the specifier %.

Whereas at each reading the position pointer inside the file does not change without the developer telling it, both the spaces and the line and file jumps are ignored by fscanf. The fscanf is nothing more than a scanf for archives.

If you want to know more about specifiers and sub-specifiers on scanf and reading derivatives, read this article from Wikipedia. Alternatively, read this explanation about the fscanf of cplusplus.com. Both texts are in English.

The problems

It is worth observing the reason of the random data presented in its algorithm:

  1. Different types between matrix and file data;
  2. A two-dimensional array (matrix) differs from the type required for fgets (array);
  3. Because of item 2, the storage of the data in the file is wrong and chaotic, taking the place of addresses of the matrix;
  4. Printing data from other parts of memory because of item 3;

Try to think a little about the problems pointed out. If you cannot understand, ask a new question about!

Browser other questions tagged

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