Hi again!
this code if i rebot the PC work, if i press F9 again does not work.
Someone can help me plese?
Code:
#include <stdio.h>
#include <string.h>
struct dados
{
int largura;
int velocidade;
char texto[50];
};
void escreve(struct dados val);
void ler(struct dados *val);
int main()
{
struct dados entdados;
scanf("%[^\n]%*c", entdados.texto);
entdados.largura=10;
entdados.velocidade=20;
printf("%d %s %d\n", entdados.largura, entdados.texto, entdados.velocidade);
//Write text in the file
escreve(entdados);
//read text in the file
ler(&entdados);
printf("%s\n",entdados.texto);
printf("%d\n",entdados.largura);
printf("%d\n",entdados.velocidade);
return 0;
};
void ler(struct dados *val) // receive as a pointer
{
FILE* fin = fopen("c:\\castanha1.txt", "r"); // open in binary mode
if (!fin)
{
printf("Unable to open file!");
return 1;
}
fread(&val, sizeof(*val), 1, fin);
fclose(fin);
printf("%d %s %d\n", val->largura, val->texto, val->velocidade);
}
void escreve(struct dados val)
{
FILE *ficheiro=fopen("c:\\castanha1.txt","w");
if (!ficheiro)
{
printf("Unable to open file!");
return 1;
}
fwrite(&val, sizeof(val), 1, ficheiro);
fclose(ficheiro);
}
Content of the file after recording:
Error after read: (Scanf plus first printf. The print after read does not appear )