接下来重点介绍几个常用的文件操作函数
一.fopen fclose
.首先是fopen,它用来打开一个文件
FILE *fopen( const char *filename, const char *mode );
其原型如上,返回一个操作文件的指针,参数是文件名和打开要进行的操作方式,如果打开失败就会返回NULL
int fclose( FILE *stream );
fclose其参数是返回的文件流指针,其作用关闭文件,一般情况两个的使用分不开
用法举例:
#include<stdio.h>
#include<Windows.h>
int main()
{
FILE *pf = fopen("hehe.txt","w");//打开一个文件
if (pf == NULL)//判断是否打开成功
{
perror("打开文件:");
exit(EXIT_FAILURE);
}
fclose(pf);//关闭文件
system("pause");
return 0;
}
这段代码的作用是打开一个hehe.txt文件,w的意思是写入的方式打开文件如果文件不存在则建立文件,因为我没有进行写入操作,所以只创建了一个文件。
如图:
"r" 以只读方式打开文件
"w" 以只写方式打开文件
"a" 以追加方式打开文件
"r+" 以读/写方式打开文件,如无文件出错
"w+" 以读/写方式打开文件,如无文件生成新文件
二.fgetc,fputc
这两个可以使标准输入输出操作(从键盘输入,从屏幕输出,也可以进行文件写入读取)
1.标准输入输出
举例:
#include<stdio.h>
int main()
{
int ch = fgetc(stdin);//从键盘输入一个字符
fputc(ch, stdout);//从屏幕输出一个字符
system("pause");
return 0;
}
2.对于文件的操作
写入
int main()
{
FILE *pf = fopen("hehe.txt","w");//打开一个文件//注意这里用的是w
if (pf == NULL)
{
perror("打开文件:");
exit(EXIT_FAILURE);
}
fputc('a', pf);//将字符'a'放到hehe.txt文件中
fputc('b', pf);//将字符'b'放到hehe.txt文件中
fputc('c', pf);//将字符'c'放到hehe.txt文件中
fputc('d', pf);//将字符'd'放到hehe.txt文件中
fclose(pf);
system("pause");
return 0;
}
程序正常运行,写入文件成功
2.读取:
int main()
{
FILE *pf = fopen("hehe.txt", "r");//注意这里w变成了r,r是读取文件内容的意思
if (pf == NULL)
{
perror("打开文件:");
exit(EXIT_FAILURE);
}
putchar(fgetc(pf));//从hehe.txt文件中读取字符'a'并用putchar()输出到屏幕
putchar(fgetc(pf));//从hehe.txt文件中读取字符'b'并用putchar()输出到屏幕
putchar(fgetc(pf));//从hehe.txt文件中读取字符'c'并用putchar()输出到屏幕
putchar(fgetc(pf));//从hehe.txt文件中读取字符'd'并用putchar()输出到屏幕
fclose(pf);
system("pause");
return 0;
}
二.fputs fgets
这两个的作用是对字符串的操作
写入字符串,和字符fputc操作一样
int main()
{
FILE *pf = fopen("hehe.txt", "w");//打开一个文件
if (pf == NULL)
{
perror("打开文件:");
exit(EXIT_FAILURE);
}
fputs("hello,world", pf);//向hehe.txt写入hello,world
fclose(pf);
system("pause");
return 0;
}
读取:
char *fgets( char *string, int n, FILE *stream );
参数是将字符串读取后存入的地址,读取的数量,文件指针
#include<stdio.h>
#include<Windows.h>
int main()
{
char arr[100];
FILE *pf = fopen("hehe.txt", "r");//打开一个文件
if (pf == NULL)
{
perror("打开文件:");
exit(EXIT_FAILURE);
}
fgets(arr,100, pf);//向hehe.txt读取hello,world存到arr中
fputs(arr, stdout);//输出到屏幕上
fclose(pf);
system("pause");
return 0;
}
三.fprintf fcanf
1.fprintf,fscanf作为标准输入输出流用法
char arr[20] = "hello,shangluo";
int age = 21;
fprintf(stdout, "%s %d", arr, age);
system("pause");
return 0;
char arr[20] = {0};
int age =0;
fscanf(stdin, "%s %d", arr, &age);
printf("%s %d", arr, age);
system("pause");
return 0;
2.对于文件的操作
fprintf将数据放入文件中
FILE *pf =fopen("hehe.txt", "w");
char arr[] = "hello,world";
int age = 100;
if (pf == NULL)
{
perror("use fopen");
exit(EXIT_FAILURE);
}
fprintf(pf,"%s %d\n", arr, age);
fclose(pf);
system("pause");
return 0;
fscanf从文件中读取
FILE *pf =fopen("hehe.txt", "r");
char arr[20];
int age;
if (pf == NULL)
{
perror("use fopen");
exit(EXIT_FAILURE);
}
fscanf(pf,"%s %d\n", arr, &age);//读取文件内容存储到arr和age中
printf("%s %d\n", arr, age);//打印在屏幕上
fclose(pf);
system("pause");
return 0;
四.sprintf sscanf
对内存块的操作
sprintf
把结构体中数据存入数组
#include<stdio.h>
typedef struct S
{
char name[15];
int age;
double score;
}S;
int main()
{
char buf[100];
S s = { "zhangsan", 20, 99.8 };
sprintf(buf, "%s %d %.lf", s.name, s.age, s.score);
printf("%s", buf);
system("pause");
return 0;
}
sscanf把数组中数据存入结构体
int main()
{
char buf[100];
S s = { "zhangsan", 20, 99.8 };
S stu = { 0 };
sprintf(buf, "%s %d %.2f", s.name,s.age, s.score);//存入数组
sscanf(buf, "%s %d %lf", stu.name, &(stu.age), &(stu.score));//从数组读取数据
printf("name:%s age:%d score:%lf",stu.name,stu.age,stu.score);//输出在屏幕
//printf("%s", buf);
system("pause");
return 0;
}