点击左上方蓝色“一口Linux”,选择“设为星标”
第一时间看干货文章 ☞【干货】嵌入式驱动工程师学习路线 ☞【干货】Linux嵌入式知识点-思维导图-免费获取 ☞【就业】一个可以写到简历的基于Linux物联网综合项目 ☞【就业】找工作简历模版
FILE *fopen(char *pname,char *mode)
//定义一个名叫fp文件指针
FILE *fp;
//判断按读方式打开一个名叫test的文件是否失败
if((fp=fopen("test","r")) == NULL)//打开操作不成功
{
printf("The file can not be opened.\n");
exit(1);//结束程序的执行
}
int fclose(FILE *fp);
int n=fclose(fp);
int fgetc(FILE *fp);
//程序名为:display.c
//执行时可用:display filename1 形式的命令行运行。显示文件filename1中的内容。例如,执行命令行display display.c将在屏幕上显示display的原代码。
//File display program.
void main(int argc,char *argv[]) //命令行参数
{
int ch;//定义文件类型指针
FILE *fp;//判断命令行是否正确
if(argc!=2)
{
printf("Error format,Usage: display filename1\n");
return; //键入了错误的命令行,结束程序的执行
}
//按读方式打开由argv[1]指出的文件
if((fp=fopen(argv[1],"r"))==NULL)
{
printf("The file <%s> can not be opened.\n",argv[1]);//打开操作不成功
return;//结束程序的执行
}
//成功打开了argv[1]所指文件
ch=fgetc(fp); //从fp所指文件的当前指针位置读取一个字符
while(ch!=EOF) //判断刚读取的字符是否是文件结束符
{
putchar(ch); //若不是结束符,将它输出到屏幕上显示
ch=fgetc(fp); //继续从fp所指文件中读取下一个字符
} //完成将fp所指文件的内容输出到屏幕上显示
fclose(fp); //关闭fp所指文件
}
int fputc(int ch,FILE *fp)
//程序名为:copyfile.c
//执行时可用:copyfile filename1 filename2形式的命令行运行,将文件filename1中的内容复制到文件filename2中去。
//file copy program.
void main(int argc,char *argv[]) //命令行参数
{
int ch;
FILE *in,*out; //定义in和out两个文件类型指针
if(argc!=3) //判断命令行是否正确
{
printf("Error in format,Usage: copyfile filename1 filename2\n");
return; //命令行错,结束程序的执行
}
//按读方式打开由argv[1]指出的文件
if((in=fopen(argv[1],"r"))==NULL)
{
printf("The file <%s> can not be opened.\n",argv[1]);
return; //打开失败,结束程序的执行
}
//成功打开了argv[1]所指文件,再
//按写方式打开由argv[2]指出的文件
if((out=fopen(argv[2],"w"))==NULL)
{
printf("The file %s can not be opened.\n",argv[2]);
return; //打开失败,结束程序的执行
}
//成功打开了argv[2]所指文件
ch=fgetc(in); //从in所指文件的当前指针位置读取一个字符
while(ch!=EOF) //判断刚读取的字符是否是文件结束符
{
fputc(ch,out); //若不是结束符,将它写入out所指文件
ch=fgetc(in); //继续从in所指文件中读取下一个字符
} //完成将in所指文件的内容写入(复制)到out所指文件中
fclose(in); //关闭in所指文件
fclose(out); //关闭out所指文件
}
//程序名为:dumpf.c
//执行时可用:dumpf filename1 形式的命令行运行。
// File dump program.
void main(int argc,char *argv[])
{
char str[9];
int ch,count,i;
FILE *fp;
if(argc!=2)
{
printf("Error format,Usage: dumpf filename\n");
return;
}
if((fp=fopen(argv[1],"r"))==NULL)
{
printf("The file %s can not be opened.\n",argv[1]);
return;
}
count=0;
do{
i=0;
//按八进制输出第一列,作为一行八个字节的首地址
printf("%06o: ",count*8);
do{
// 从打开的文件中读取一个字符
ch=fgetc(fp);
// 按十进制方式输出这个字符的ASCII码
printf("%4d",ch);
// 如果是不可示字符就用"#"字符代替
if(ch<' '||ch>'~') str[i]='#';
// 如果是可示字符,就将它存入数组str以便形成字符串
else str[i]=ch;
// 保证每一行输出八个字符
if(++i==8) break;
}while(ch!=EOF); // 遇到文件尾标志,结束读文件操作
str[i]='\0'; // 在数组str加字符串结束标志
for(;i<8;i++) printf(" "); // 一行不足八个字符用空格填充
printf(" %s\n",str); // 输出字符串
count++; // 准备输出下一行
}while(ch!=EOF); // 直到文件结束
fclose(fp); // 关闭fp所指文件
}
char *fgets(char *str,int n,FILE *fp)
int fputs(char *str,FILE *fp)
//程序名:linkfile.c
//执行时可用:linkfile filename1 filename2形式的命令行运行,将文件filename2的内容附加在文件filename1之后。
// file linked program.
void main(int argc,char *argv[])
{
char buffer[SIZE];
FILE *fp1,*fp2;
if(argc!=3)
{
printf("Usage: linkfile filename1 filename2\n");
return;
}
// 按追加方式打开argv[1] 所指文件
if((fp1=fopen(argv[1],"a"))==NULL)
{
printf("The file %s can not be opened.\n",argv[1]);
return;
}
if((fp2=fopen(argv[2],"r"))==NULL)
{
printf("The file %s can not be opened.\n",argv[2]);
return;
}
// 读入一行立即写出,直到文件结束
while(fgets(buffer,SIZE,fp1)!=NULL)
printf("%s\n",buffer);
while(fgets(buffer,SIZE,fp2)!=NULL)
fputs(buffer,fp1);
fclose(fp1);
fclose(fp2);
if((fp1=fopen(argv[1],"r"))==NULL)
{
printf("The file %s can not be opened.\n",argv[1]);
return;
}
while(fgets(buffer,SIZE,fp1)!=NULL)
printf("%s\n",buffer);
fclose(fp1);
}
int fprintf(FILE *fp,char *format,arg_list)
//存储文件名:save.txt
//程序代码如下:
// file display program.
void main()
{
char name[10];
int nAge,nClass;
long number;
FILE *fp;
if((fp=fopen("student.txt","w"))==NULL)
{
printf("The file %s can not be opened.\n","student.txt");
return;
}
fscanf(stdin,"%s %d %d %ld",name,&nClass,&nAge,&number);
fprintf(fp,"%s %5d %4d %8ld",name,nClass,nAge,number);
fclose(fp);
if((fp=fopen("student.txt","r"))==NULL)
{
printf("The file %s can not be opened.\n","student.txt");
return;
}
fscanf(fp,"%s %d %d %ld",name,&nClass,&nAge,&number);
printf("name nClass nAge number\n");
fprintf(stdout,"%-10s%-8d%-6d%-8ld\n",name,nClass,nAge,number);
fclose(fp);
}
int fread(void *buffer,unsigned sife,unsigned count,FILE *fp)
int fwrite(void *buffer,unsigned sife,unsigned count,FILE *fp)
struct worker
{ int number;
char name[20];
int age;
};
void main()
{
struct worker wk;
int n;
FILE *in,*out;
if((in=fopen("file1.txt","rb"))==NULL)
{
printf("The file %s can not be opened.\n","file1.txt");
return;
}
if((out=fopen("file2.txt","wb"))==NULL)
{
printf("The file %s can not be opened.\n","file2.txt");
return;
}
while(fread(&wk,sizeof(struct worker),1,in)==1)
fwrite(&wk,sizeof(struct worker),1,out);
fclose(in);
fclose(out);
}
int getw(FILE *fp)
void main(int argc,char *argv[])
{
int i,sum=0;
FILE *fp;
if(argc!=2)
{
printf("Command error,Usage: readfile filename\n");
exit(1);
}
if(!(fp=fopen(argv[1],"rb")))
{
printf("The file %s can not be opened.\n",argv[1]);
exit(1);
}
for(i=1;i<=10;i++) sum+=getw(fp);
printf("The sum is %d\n",sum);
fclose(fp);
}
int putw(int n,FILE *fp)
void main(int argc,char *argv[])
{
int i;
FILE *fp;
if(argc!=2)
{
printf("Command error,Usage: writefile filename\n");
return;
}
if(!(fp=fopen(argv[1],"wb")))
{
printf("The file %s can not be opened.\n",argv[1]);
return;
}
for(i=1;i<=10;i++) printf("%d\n", putw(i,fp));
fclose(fp);
}
int feof(FILE *fp)
void main(int argc,char *argv[])
{
FILE *in,*out;
char ch;
if(argc!=3)
{
printf("Usage: copyfile filename1 filename2\n");
return;
}
if((in=fopen(argv[1],"rb"))==NULL)
{
printf("The file %s can not be opened.\n",argv[1]);
return;
}
if((out=fopen(argv[2],"wb"))==NULL)
{
printf("The file %s can not be opened.\n",argv[2]);
return;
}
while(!feof(in))
{
ch=fgetc(in);
if(ferror(in))
{
printf("read error!\n");
clearerr(in);
}
else
{
fputc(ch,out);
if(ferror(out))
{
printf("write error!\n");
clearerr(out);
}
}
}
fclose(in);
fclose(out);
}
int ferror(FILE *fp)
void clearerr(FILE *fp)
void main(int argc,char *argv[])
{
FILE *in,*out;
char ch;
if(argc!=3)
{
printf("Usage: copyfile filename1 filename2\n");
return;
}
if((in=fopen(argv[1],"rb"))==NULL)
{
printf("The file %s can not be opened.\n",argv[1]);
return;
}
if((out=fopen(argv[2],"wb"))==NULL)
{
printf("The file %s can not be opened.\n",argv[2]);
return;
}
while(!feof(in))
{
ch=fgetc(in);
if(ferror(in))
{
printf("read error!\n");
clearerr(in);
}
else
{
fputc(ch,out);
if(ferror(out))
{
printf("write error!\n");
clearerr(out);
}
}
}
fclose(in);
fclose(out);
}
long ftell(FILE *fp)
void rewind(FILE *fp)
void main()
{
FILE *in,*out;
in=fopen("filename1","r");
out=fopen("filename2","w");
while(!feof(in)) fputc(fgetc(in),out);
rewind(out);
while(!feof(in)) putchar(fgetc(in));
fclose(in);
fclose(out);
}
int fseek(FILE *fp,long offset,int base)
struct std_type
{
int num;
char name[20];
int age;
char class;
}stud;
int cstufile()
{
int i;
FILE *fp;
if((fp=fopen("stufile","wb"))==NULL)
{
printf("The file can't be opened for write.\n");
return 0;
}
for(i=1;i<=100;i++)
{
stud.num=i;
strcpy(stud.name,"aaaa");
stud.age=17;
stud.class='8';
fwrite(&stud,sizeof(struct std_type),1,fp);
}
fclose(fp);
return 1;
}
void main()
{
int n;
FILE *fp;
if(cstufile()==0) return;
if((fp=fopen("stufile","rb"))==NULL)
{
printf("The file can not be opened.\n");
return;
}
for(n=0;n<100;n+=2)
{
fseek(fp,n*sizeof(struct std_type),SEEK_SET);
fread(&stud,sizeof(struct std_type),1,fp);
printf("%10d%20s%10d%4c\n",stud.num,stud.name,stud.age,stud.class);
}
fclose(fp);
}
void exit(int status)
int feof(FILE *fp)
end
一口Linux
关注,回复【1024】海量Linux资料赠送
精彩文章合集
文章推荐