c语言实现文件的读写操作:
#include<>
#include<>
#define N 5
typedef struct {
int number;//学号
char name[30];//姓名
char major[30];//专业
} Student_info;
int main(){
//写文件
// FILE *fp=fopen("D:/","w");
// if(fp==NULL){
// printf("打开文件失败!");
// exit(0);
// }
// for(int i=0;i<10;i++){
// fputs("Hello file!",fp);
// }
// fclose(fp);
//读文件
// FILE *fp1=fopen("D:/","r");
// if(fp1==NULL){
// printf("打开文件失败!");
// exit(0);
// }
// fseek(fp1,15,SEEK_SET);
// char c=fgetc(fp1);
// printf("字符%c",c);
// fclose(fp1);
// //以"写"方式打开文件
// FILE *fp=fopen("D:/","w");
// if(fp==NULL) {
// printf("打开文件失败!");
// exit(0);
// }
// Student_info student[N]; //用数组保存N个学生信息
// for(int i=0;i<N;i++) { //生成 N个学生信息
// student [i].number=i;
// student [i].name[0]='?';
// student[1].major[0]='?';
// printf("\n");
// }
// //将N个学生的信息写入文件
// fwrite(student,sizeof(Student_info),N,fp);
// fclose(fp);
//以"读"方式打开文件
FILE *fp1=fopen("D:/","r");
if(fp1==NULL) {
printf("打开文件失败!");
exit(0);
}
//文件读写指针指向编号为5的学生记录
fseek(fp1,5*sizeof(Student_info),SEEK_SET);
Student_info stu; //用数组保存N个学生信息
//将文件读出1条记录,记录大小为sizeof(Student_info)
fread(&stu,sizeof(Student_info),1,fp1);
printf("学生编号:%d\n",);
fclose(fp1);
}