4
I’m trying to make a simple program to read data from a text file, store it in a struct, and print that data on the screen. The code I have is this::
#include <stdio.h>
#define max 70
typedef struct
{
int BI;
float altura;
int peso;
}PESSOA;
int main()
{
PESSOA data[max];
FILE *f;
int N=0;
f=fopen("dados2.txt", "r");
if(f==NULL)
{
printf("Não foi possível abrir o arquivo.\n");
return 0;
}
while(feof(f)!=0)
{
fscanf(f, "%d", &data[N].BI);
fscanf(f, "%f", &data[N].altura);
fscanf(f, "%d", &data[N].peso);
printf("%d \n", data[N].BI);
printf("%.2f \n", data[N].altura);
printf("%d \n", data[N].peso);
printf("\n");
N++;
}
fclose(f);
return 0;
}
The text file has the following format (repeated 65 times):
BI
Altura
Peso
Example:
9914241
1.55
99
1171804
1.80
63
1796075
1.64
85
The problem is that when I run the program nothing appears on the screen. Can someone tell me what I’m doing wrong?