#include<stdio.h>
#include<conio.h>
#include<string.h>
// pisanje i citnaje strukture iz fajla
struct s_person{
char name[20];
char surname[20];
int age;
};
void person_input(struct s_person *person);
void person_output(struct s_person *person);
void person_fWrite(FILE *fp,struct s_person *person);
void person_fRead(FILE *fp,struct s_person *person);
struct s_person person;
main(){
FILE *fp=fopen("fStruct.txt","w+");
//unos strukture sa tastature
person_input(&person);
// upis u fajl
person_fWrite(fp,&person);
// citanje iz fajla
person_fRead(fp,&person);
// ispis strukture na ekran
person_output(&person);
//
fclose(fp);
getch();
}
void person_input(struct s_person *person){
printf("person.name=");scanf("%s",&person->name);
printf("person.surname=");scanf("%s",&person->surname);
printf("person.age=");scanf("%d",&person->age);
}
void person_output(struct s_person *person){
printf("person.name=%s",person->name);
printf("person.surname=%s",person->surname);
printf("person.age=%d",person->age);
}
void person_fWrite(FILE *fp,struct s_person *person){
if (fp!=NULL){
fprintf(fp,"%s\n",person->name);
fprintf(fp,"%s\n",person->surname);
fprintf(fp,"%d\n",person->age);
}
else{
printf("Cant read or create file");
}
}
void person_fRead(FILE *fp,struct s_person *person){
if (fp!=NULL){
rewind(fp); // vrati na pocetak fajla pre citnaja
char str[20];
fgets(str,20,fp); strcpy(person->name,str);
fgets(str,20,fp); strcpy(person->surname,str);
fscanf (fp, "%d", &person->age);
}
else{
printf("Cant read or create file");
}
}
#include<conio.h>
#include<string.h>
// pisanje i citnaje strukture iz fajla
struct s_person{
char name[20];
char surname[20];
int age;
};
void person_input(struct s_person *person);
void person_output(struct s_person *person);
void person_fWrite(FILE *fp,struct s_person *person);
void person_fRead(FILE *fp,struct s_person *person);
struct s_person person;
main(){
FILE *fp=fopen("fStruct.txt","w+");
//unos strukture sa tastature
person_input(&person);
// upis u fajl
person_fWrite(fp,&person);
// citanje iz fajla
person_fRead(fp,&person);
// ispis strukture na ekran
person_output(&person);
//
fclose(fp);
getch();
}
void person_input(struct s_person *person){
printf("person.name=");scanf("%s",&person->name);
printf("person.surname=");scanf("%s",&person->surname);
printf("person.age=");scanf("%d",&person->age);
}
void person_output(struct s_person *person){
printf("person.name=%s",person->name);
printf("person.surname=%s",person->surname);
printf("person.age=%d",person->age);
}
void person_fWrite(FILE *fp,struct s_person *person){
if (fp!=NULL){
fprintf(fp,"%s\n",person->name);
fprintf(fp,"%s\n",person->surname);
fprintf(fp,"%d\n",person->age);
}
else{
printf("Cant read or create file");
}
}
void person_fRead(FILE *fp,struct s_person *person){
if (fp!=NULL){
rewind(fp); // vrati na pocetak fajla pre citnaja
char str[20];
fgets(str,20,fp); strcpy(person->name,str);
fgets(str,20,fp); strcpy(person->surname,str);
fscanf (fp, "%d", &person->age);
}
else{
printf("Cant read or create file");
}
}