标准C语言文件操作函数包含:fopen,fwrite,fread,fseek,fputc,fgetc等
1.fopen
FILE *fopen(const char *path, const char *mode);
path:文件名(文件路径)
mode:依某种模式权限打开文件
mode的几种参数
r 打开文本文件,用于读。流被定位于文件的开始。
r+ 打开文本文件,用于读写。流被定位于文件的开始。
w 将文件长度截断为零,或者创建文本文件,用于写。流被定位于文件的开始。
w+ 打开文件,用于读写。如果文件不存在就创建它。流被定位于文件的开始。
a 打开文件,用于追加。如果文件不存在就创建它。流被定位于文件的末尾。
a+ 打开文件,用于追加(在文件尾写)。如果文件不存在就创建它。
a+ 读文件的初始位置是文件的开始,但是输出总是被追加到文件的末尾。
返回值
如果成功执行了 fopen将返回一个指向文件对象 FILE 的指针,失败将返回 NULL。
2.fwrite
size_t fwrite(const void *ptr, size_t size, size_t nmemb,FILE *stream);
pstr:缓冲区,存放写入文件内容
size:写入的每个元素的大小,以字节为单位
nmemb:元素的个数,每个元素的大小为 size 字节
stream:目标文件指针
返回值
如果成功,该函数返回一个 size_t 对象,表示元素的总数,大小与nmemb一致
3.fread
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
pstr:缓冲区,存放读取文件内容
size:写入的每个元素的大小,以字节为单位
nmemb:元素的个数,每个元素的大小为 size 字节
stream:目标文件指针
返回值
如果成功,该函数返回一个 size_t 对象,表示元素的总数,大小与nmemb一致
3.fseek
int fseek(FILE *stream, long offset, int whence);
stream:目标文件指针
offset:相对于whence的偏移量
whence:指定文件指针初始位置
whence具体参数:
SEEK_SET:文件指针开始
SEEK_CUR:文件指针当前位置
SEEK_END:为文件指针尾
返回值
成功执行返回0,出错,返回-1
4.fclose
int fclose(FILE *stream);
stream:目标文件指针
返回值
成功执行返回 0
5.fputc
int fputc(int c, FILE *stream);
c:输入字符
stream:目标文件指针
返回值
成功执行函数返回写入文件的字符的ASCII码值,出错时,返回EOF(-1)
6.fgetc
int fgetc(FILE *stream);
stream:目标文件指针
返回值
成功执行函数返回写入文件的字符的ASCII码值,出错时,返回EOF(-1)
demo1:
1.创建文件
2.写入hello
3.读取hello
4.关闭文件
结果示例:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
void result()
{
FILE *fp;
char *write_buf = "hello";
int fp_write;
int fp_fseek;
char * read_buf = NULL;
int fp_read;
int fp_close;
fp = fopen("/root/pro/file","w+");
if(fp == NULL)
{
printf("file create fail\n");
perror("why");
exit(-1);
}
fp_write = fwrite(write_buf,strlen(write_buf),1,fp);
if(fp_write != 1)
{
printf("write file fail\n");
perror("why");
exit(-1);
}
fp_fseek = fseek(fp,0,SEEK_SET);
if(fp_fseek != 0)
{
printf("fseek file fail\n");
perror("why");
exit(-1);
}
read_buf = (char *)malloc(strlen(write_buf));
fp_read = fread(read_buf,strlen(write_buf),1,fp);
if(fp_read != 1)
{
printf("read file fail %d\n",fp_read);
perror("why");
exit(-1);
}
printf("file context: %s\n",read_buf);
fp_close = fclose(fp);
if(fp_close != 0)
{
printf("close file fail\n");
perror("why");
exit(-1);
}
}
int main()
{
result();
return 0;
}
demo2:
1.创文件
2.单个字符依次写入hello
3.单个字符依次读取hello
结果示例:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void result()
{
FILE *fp;
char *buf = "hello\n";
int n_fputc;
int i;
int len = strlen(buf);
int fp_fseek;
char c_data;
int fp_close;
fp = fopen("/root/pro/file","w+");
if(fp == NULL)
{
printf("create file fail\n");
perror("why");
exit(-1);
}
for(i = 0;i < len;i++)
{
n_fputc = fputc(*buf,fp);
if(n_fputc < 0)
{
printf("%dth input fail\n",i+1);
perror("why");
exit(-1);
}
*buf++;
}
fp_fseek = fseek(fp,0,SEEK_SET);
if(fp_fseek == -1)
{
printf("fseek fail\n");
perror("why");
exit(-1);
}
while(!feof(fp))
{
c_data = fgetc(fp);
printf("%c",c_data);
}
fp_close = fclose(fp);
if(fp_close != 0)
{
printf("close file fail\n");
perror("why");
exit(-1);
}
}
int main()
{
result();
return 0;
}