C语言文件操作

Python056

C语言文件操作,第1张

C语言标准库提供了一系列文件I/O函数用于文件操作,比如fopen()用于打开文件、fread()、fwrite()用于读写文件、fseek()用于设置操作位置等等,一般C语言教程上都有文件I/O一章,细致内容,可以找本教科书学习一下。下面是一个示例代码实现了,将一个磁盘文件中的信息复制到另一个磁盘文件中。

#include < stdio.h >

int 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")

     return 0

    }

 if((out=fopen(outfile,"w"))==NULL)

    {printf("cannot open outfile\n")

     return 0

    }

 while(!feof(in))fputc(fgetc(in),out)

 fclose(in)

 fclose(out)

 return 0

}

标准输入输出应加头文件其预处理命令为

#include<stdio.h>(是C的编译器的话可以不加)

printf(格式控制,输出列表)如:

pritnf("%d,%c\n",a,b)

函数原型 int printf(char *format,arg,……)

scanf(格式控制,输出列表)如:

scanf("%d,%c\n",&a,&b)

函数原型 int scanf(char *format,arg,……)

其他常用的标准输入输函数如getchar,gets,putchar,puts等在这里就不具体说明了

请参看C库函数(C语言的书上都有)