파일 입출력 (binary)
#include<stdio.h>
#pragma warning (disable:4996)
typedef struct student
{
char name[10];
int age;
}STU;
void main()
{
//2진(binary)입출력 함수
FILE *fp;
STU stu={"batman",30};
STU buf;
fp=fopen("c.txt","rb"); // b를 붙히면 binary란 뜻을 가진다.
fprintf(fp,"%s %d\n",stu.name,stu.age);
fwrite(&stu,sizeof(STU),1,fp);
fread(&buf,sizeof(STU),1,fp);
fclose(fp);
printf("%s %d\n",buf.name,buf.age); //콘솔창에 batman 30 출력
}
#include<stdio.h>
#include<stdlib.h>
#pragma warning(disable:4996);
struct a
{
char name[10];
int score[4];
float avg;
};
void main()
{
FILE*fp;
struct a aa={"Jombie",1,2,3,6,2.f};
fp=fopen("b.txt","wb");
fwrite(&aa,sizeof(struct a),1,fp);
fclose(fp);
}
#include<stdio.h>
#include<stdlib.h>
#pragma warning(disable:4996);
struct a
{
char name[10];
int score[4];
float avg;
};
void main()
{
FILE*fp;
struct a aa={"Jombie",1,2,3,6,2.f};
struct a bb;
fp=fopen("b.txt","rb");
fread(&bb,sizeof(struct a),1,fp);
fclose(fp);
printf("%s %d %d %d %d %f\n",bb.name,bb.score[0],bb.score[1],bb.score[2],bb.score[3],bb.avg);
}