wjpforever的ChinaUnix博客
wjpforever
全部博文(1)
2016年(1)
分类: C/C++
2016-11-18 09:09:17
原文地址:C语言对文件的操作 作者:luojingqing
#include < stdio.h > main() {FILE *fp; char ch,filename[10]; scanf("%s",filename); if((fp=fopen(filename,"w"))==NULL) {printf("cannot open file\n"); exit(0); } ch=getchar(); while(ch!='#') { fputc(ch,fp),putchar(ch); ch=getchar(); } fclose(fp); }
file1.c? (输入磁盘文件名) computer and c#? (输入一个字符串) computer and c
#include < stdio.h > main() {FILE *in,*out; char ch,infile[10],outfile[10]; printf("Enter the infile name:\n"); scanf("%s",infile); printf("Enter the outfile name:\n"); scanf("%s",outfile); if((in=fopen(infile,"r"))==NULL) {printf("cannot open infile\n"); exit(0); } if((out=fopen(outfile,"w"))==NULL) {printf("cannot open outfile\n"); exit(0); } while(!feof(in))fputc(fgetc(in),out); fclose(in); fclose(out); }
Enter the infile name: file1.c?(输入原有磁盘文件名) Enter the outfile name: file2.c?(输入新复制的磁盘文件名)
#include < stdio.h > struct student_type { char name[10]; int num; int age; char sex; }stud[10] main() {int i; FILE *fp; if((fp=fopen("stud_dat","rb"))==NULL; { printf("cannot open file\n"); exit(0); } for(i=0;i<10;i+=2) { fseek(fp,i*sizeof(struct student_type),0); fread(&stud[i],sizeof(struct student_type),1,fp); printf("%s%d%d%c\n",stud[i].name,stud[i].num, stud[i].age,stud[i].sex); } fclose(fp); }
上一篇:没有了
下一篇:没有了
登录 注册