Skip to content

Instantly share code, notes, and snippets.

@Silva97
Created May 12, 2018 03:27
Show Gist options
  • Save Silva97/98763b85837317523d3011922f795707 to your computer and use it in GitHub Desktop.
Save Silva97/98763b85837317523d3011922f795707 to your computer and use it in GitHub Desktop.
/********************
* Exemplo por Luiz Felipe
* https://github.com/Silva97
********************/
#include <stdio.h>
#include <unistd.h>
#define FNAME "exemplo.dat"
typedef struct data {
char name[65];
int age;
} Data;
int main(){
Data user;
FILE *dat;
if(!access(FNAME, F_OK)){
dat = fopen(FNAME, "r");
fread(&user, sizeof user, 1, dat);
printf("Seu nome: %s\n", user.name);
printf("Sua idade: %d\n", user.age);
} else {
fputs("Digite seu nome: ", stdout);
scanf("%64[^\n]s", user.name);
fputs("Digite sua idade: ", stdout);
scanf("%d", &user.age);
dat = fopen(FNAME, "w");
fwrite(&user, sizeof user, 1, dat);
}
fclose(dat);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment