一、打开关闭文件
只打开文件 再关闭文件
#include <stdio.h>
#include<stdlib.h>
main()
{
FILE *fp;
fp = fopen("data.dat","r"); //打开文件
if(fp == NULL)
{
printf("Cannot open this file!\n ");
exit(0); //头文件需要有 stdlib.h
}
fclose(fp); //关闭文件
}
二、调用fgetc和fputc函数进行输入(文件→程序)和输出(程序→文件)
fputc(程序→文件)在键盘上输入字符通过程序输出到文件上。本例中通过 ‘@’字符作为结束标志。
#include <stdio.h>
#include<stdlib.h>
main()
{
FILE *fpout;
char ch;
fpout = fopen("data.dat","w"); //只写打开文件
if(fpout == NULL)
{
printf("Cannot open this file!\n ");
exit(0); //头文件需要有 stdlib.h
}
ch = getchar();
while(ch != '@') //把从键盘上输入的文本按原样输出到data.dat中用@作为键盘结束标志
{ fputc(ch,fpout); ch = getchar(); }
fclose(fpout); //关闭文件
}
输入(文件→程序)fgetc(文件→程序) 将文件中的字符从开始显示到屏幕上。
#include <stdio.h>
#include<stdlib.h>
main()
{
FILE *fpin;
char ch;
fpin = fopen("data.dat","r"); //只读方式打开文件
if(fpin == NULL)
{
printf("Cannot open this file!\n ");
exit(0);
}
ch = fgetc(fpin);
while(ch != EOF) //把一个已存在磁盘上的文件data.dat中的文本原样输出到终端屏幕上。
{ putchar(ch); ch = fgetc(fpin); }
fclose(fpin); //关闭文件
}
三、fscanf(文件指针,"格式控制字符串",输入项表)函数(文件→程序) 和 fprintf(文件指针,"格式控制字符串",输出项表)(程序→文件)
fscanf 函数 格式化输入 将文件中的数据按程序中的格式输入到内存变量中。
#include <stdio.h>
#include<stdlib.h>
int main()
{
FILE *fp;
int a,b,c;
fp = fopen("data.dat","r"); //只读方式打开文件
if(fp == NULL)
{
printf("Cannot open this file!\n ");
exit(0);
}
fscanf(fp,"%d%d%d",&a,&b,&c); //data文件中已有数据
printf("a = %d b = %d c = %d\n",a,b,c);
fclose(fp); //关闭文件
return 1;
}
fprintf函数 将内存变量中得数据格式化输出到文件中
#include <stdio.h>
#include<stdlib.h>
int main()
{
FILE *fp;
int a,b,c;
fp = fopen("data.dat","r+"); //读写方式打开文件
if(fp == NULL)
{
printf("Cannot open this file!\n ");
exit(0);
}
a = 100; b = 200; c = 300;
fprintf(fp,"%d %d %d",a,b,c);
fclose(fp); //关闭文件
return 1;
}
四、fgets(str,n,fp)函数(文件→程序) 和 fputs(str,fp)函数(程序→文件)
fgets函数 从文件中文件指针fp位置向后读取n-1个字符数据到以str为起始地址的内存变量中。str可以是数组指针、数组名、指针、字符串常量。
#include <stdio.h>
#include<stdlib.h>
int main()
{
FILE *fp;
char str[50];
fp = fopen("data.dat","r+"); //读写方式打开文件
if(fp == NULL)
{
printf("Cannot open this file!\n ");
exit(0);
}
fgets(str,13,fp);
puts(str);
fclose(fp); //关闭文件
return 1;
}
fputs函数 将数据输出到文件中。
#include <stdio.h>
#include<stdlib.h>
int main()
{
FILE *fp;
char str[50]={"hello world! I Love You!"};
str[12] = '\n';
fp = fopen("data.dat","r+"); //读写方式打开文件
if(fp == NULL)
{
printf("Cannot open this file!\n ");
exit(0);
}
fputs(str,fp);
fclose(fp); //关闭文件
return 1;
}
五、fread(buffer,size,count,fp)函数(文件→程序) 和 fwrite(buffer,size,count,fp)函数(程序→文件)
buffer是数据块的指针,对fread来说,它是内存块的首地址,输入的数据存在该内存块中;对于fwrite来说,它是准备输出的数据的起始地址。size表示每个数据块的字节数。count用来指定每读、写一次,输入或输出数据块的个数(每个数据块具有size字节)。
fwrite函数
#include <stdio.h>
#include<stdlib.h>
int main()
{
FILE *fp;
char str[50]={"hello world! I Love You sheia ?!"};
str[12] = '\n';
fp = fopen("data.dat","rb+"); //读写方式打开文件
if(fp == NULL)
{
printf("Cannot open this file!\n ");
exit(0);
}
fwrite(str,sizeof(str),1,fp);
fclose(fp); //关闭文件
return 1;
}
fread函数
#include <stdio.h>
#include<stdlib.h>
int main()
{
FILE *fp;
char str[50]={"0"};
fp = fopen("data.dat","r+"); //读写方式打开文件
if(fp == NULL)
{
printf("Cannot open this file!\n ");
exit(0);
}
fread(str,49,1,fp);
str[49]='\0';
puts(str);
fclose(fp); //关闭文件
return 1;
}
六、文件定位函数
fseek(pf,offset,origin)
fseek 函数用来移动文件位置指针到指定的位置上,接着读写的操作将从此位置开始。
pf是文件指针;offset是以字节为单位的位移量,为长整形数;origin是起始点 用以指定位移量是从哪个位置为基准点。
标识符 | 数字 | 代表的起始点 |
SEEK_SET | 0 | 文件开始 |
SEEK_END | 2 | 文件末尾 |
SEEK_CUR | 1 | 文件当前位置 |
对于二进制文件位移量为正数时表示位置指针向尾部移动,当为负数时,则相反。
对于文本文件位移量必须是0
ftell()函数用以获得当前文件指针的位置(相对于文本开头的字节数)
long t;
t = ftell(fp);
出错时 函数返回-1。
rewind()函数又称“反绕”函数。该函数没有返回值。其作用是是文件中的位置指针返回到文件开头。其调用格式如下
rewind(fp);