#include <stdio.h>
#include <stdlib.h>
#define Len 31
#define size 4
struct student
{
char name[10];
int num;
int age;
char addr[15];
}stu[size];
void save()
{
FILE *fp;
int i;
if(!(fp=fopen("stu_list","wb")))
{
printf("can not open the file \n");
return;
}
for(i=0;i<size;i++)
{
if(fwrite(&stu[i],sizeof(struct student),1,fp)!=1)
{
printf("write file err!\n");
fclose(fp);
}
}
}
//将文件都出来并显示出来
void Load()
{
FILE *fp;
if(!(fp=fopen("stu_list","r")))
{
printf("can not open the file\n");
return;
}
int i;
for(i=0;i<size;i++)
{
fread(&stu[i],sizeof(struct student),1,fp);
printf("%s %d %d %s\n",stu[i].name,stu[i].num,stu[i].age,stu[i].addr);
}
fclose(fp);
}
int main()
{
FILE *fp;
int i=1;//用于定位第i个结构
if(!(fp=fopen("stu_list", "r")))
{
printf("can not open the file");
exit(1);
}
rewind(fp);//将指针置于文件头
fseek(fp,i*sizeof(struct student),0);
fread(&stu[0],sizeof(struct student),1,fp);
printf("name \t num \t age \t address\n");
printf("%s \t %d \t %d \t %s \n",stu[0].name,
stu[0].num,stu[0].age,stu[0].addr);
system("pause");
// int i;
//
// printf("please input the studnt's name num,age and address!\n");
// for(i=0;i<size;i++)
// {
// scanf("%s %d %d %s",stu[i].name,&stu[i].num,&stu[i].age,stu[i].addr);
// }
//
// save();
// Load();
//
// return 0;
// FILE *fp;
// char buffer[Len];
//
// if(!(fp=fopen("f:\\shankai.txt", "rt")))
// {
// printf("can not open the file");
// exit(1);
// }
// fgets(buffer,Len,fp);
// printf("%s\n",buffer);
//
// fclose(fp);
// /************************* 图种生成器 *************************/
// FILE *f_pic,*f_rar,*f_finish;
// char pic_name[20],rar_name[20],finish_name[20];
// char ch;
//
// printf("请输入需要合成的图片和其他文件的名称!\n");
// printf("图片: ");
// scanf("%s",pic_name);
// printf("文件: ");
// scanf("%s",rar_name);
// printf("生成为: ");
// scanf("%s",finish_name);
//
// if(!(f_pic=fopen(pic_name,"rb")))
// {
// printf(" can not open the pic file %s",pic_name);
// return;
// }
// if(!(f_rar=fopen(rar_name,"rb")))
// {
// printf(" can not open the rar file %s",rar_name);
// return;
// }
// if(!(f_finish=fopen(finish_name,"wb")))
// {
// printf(" can not open the finish file %s",finish_name);
// return;
// }
//
// while(!feof(f_pic))
// {
// ch=fgetc(f_pic);
// fputc(ch,f_finish);
// }
// fclose(f_pic);
//
// while(!feof(f_rar))
// {
// ch=fgetc(f_rar);
// fputc(ch,f_finish);
// }
// fclose(f_rar);
// fclose(f_finish);
//
// system("pause");
// FILE *fp;
// char ch,filename[20];
//
// printf("please input the file name you want to write:\n");
// scanf("%s",filename);
//
// if(!(fp=fopen(filename,"rt+")))
// {
// printf("Can not open the file!\n");
// exit(0);//终止程序
// }
//
// while(ch!=EOF)
// {
// ch=fgetc(fp);
// putchar(ch);
// }
//
// fclose(fp);
//
// return 0;
// FILE *fp;
// char ch,filename[20];
//
// printf("please input the file name you want to write:\n");
// scanf("%s",filename);
//
// if(!(fp=fopen(filename,"wt+")))
// {
// printf("Can not open the file!\n");
// exit(0);//终止程序
// }
//
// printf("please input the sentences you want to write:\n");
// //ch=getchar();
// ch=getchar();//为什么会要两次呢? 会多出一个空行
// while(ch!=EOF)
// {
// fputc(ch,fp);
// ch=getchar();
// }
//
// fclose(fp);
//
// return 0;
// FILE *fp;
// if(!(fp=fopen("f:\\shankai.txt","w")))
// {
// printf("can not open file f:\\shankai.txt!!!");
// system("pause");
// }
// else
// {
// printf("Open success!\n");
// }
}
c语言之文件操作
最新推荐文章于 2024-03-27 11:46:03 发布