文件
day1
概念+相关函数
1.特殊文件流:
程序执行后,操作系统会默认帮程序打开3个流
stdin:标准输入流
stdout:标准输出流
stderr:标准出错流
2.流的输入输出:
1.缓存类型
1.全缓存 4k
缓存区满刷新缓存
刷新条件:
1.缓存区满刷新
2.程序结束刷新
3.fflush刷新
与文件建立的流为全缓存
2.行缓存 1k
遇到\n刷新缓存
刷新条件:
1.遇到\n刷新缓存
2.缓存区存满刷新
3.程序结束刷新
4.fflush刷新
与终端建立的流为行缓存 stdin stdout
3.不缓存 0k stderr
直接刷新
人机交互、界面控制、出错
3.函数接口:
fopen:打开文件
#include <stdio.h>
int main(int argc, const char *argv[])
{
FILE *fp = NULL;
fp = fopen("file.txt", "a");
if (NULL == fp)
{
perror("fail to fopen");
return -1;
}
fclose(fp);
return 0;
}
1.fclose
int fclose(FILE *fp);
功能:
关闭文件流
参数:
fp:文件流指针
返回值:
关闭失败返回EBADF
#include <stdio.h>
int main(int argc, const char *argv[])
{
fclose(stdout);
fclose(stdin);
getchar();
printf("hello world!\n");
return 0;
}
2.setvbuf
int setvbuf(FILE *stream, char *buf, int mode, size_t size);
功能:
改变一个已经打开流的缓存类型
参数:
stream:文件流指针
buf:缓存存放位置空间首地址
mode:
_IONBF 不缓存
_IOFBF 全缓存
_IOLBF 行缓存
size:缓存空间大小
返回值:
成功返回0
失败返回非0
#include <stdio.h>
int main(int argc, const char *argv[])
{
char TmpBuff[4096] = {
0};
// setvbuf(stdout, NULL, _IONBF, 0);
setvbuf(stdout, TmpBuff, _IOFBF, 4096);
setvbuf(stdout, TmpBuff, _IOLBF, 1024);
printf("hello world\n");
while (1)
{
}
return 0;
}
3.fputc
int fputc(int c, FILE *stream);
功能:
向流中写入一个字符
参数:
c:要写入的字符
stream:文件流指针
返回值:
成功返回写入字符的ASCII码值
失败返回EOF
练习:向文件中写入一个字符串"hello world"
#include <stdio.h>
int main(int argc, const char *argv[])
{
FILE *fp = NULL;
char ch = 'A';
fp = fopen("file.txt", "w");
if (NULL == fp)
{
perror("fail to fopen");
return -1;
}
fputc('a', fp);
fputc(100, fp);
fputc(ch, fp);
fclose(fp);
return 0;
}
#include <stdio.h>
int main(int argc, const char *argv[])
{
FILE *fp = NULL;
char Str[32] = {
"hello world"};
char *pTmp = NULL;
fp = fopen("file.txt", "w");
if (NULL == fp)
{
perror("fail to fopen");
return -1;
}
pTmp = Str;
while (*pTmp != '\0')
{
fputc(*pTmp, fp);
pTmp++;
}
fclose(fp);
return 0;
}
4.fgetc
int fgetc(FILE *stream);
功能:
从流中读取一个字符
参数:
stream:文件流指针
返回值:
成功返回读到字符的ASCII码值
失败返回EOF
读到文件末尾返回EOF
练习:利用fgetc将文件中所有内容打印在终端屏幕上
练习:利用fgetc和fputc实现文件内容的拷贝
./a.out srcfile dstfile
ch = getchar() == ch = fgetc(stdin)
putchar(ch) == fputc(ch, stdout)
#include <stdio.h>
int main(int argc, const char *argv[])
{
FILE *fp = NULL;
char ch = 0;
fp = fopen("file.txt", "r");
if (NULL == fp)
{
perror("fail to fopen");
return -1;
}
ch = fgetc(fp);
printf("ch = %c\n", ch);
ch = fgetc(fp);
printf("ch = %c\n", ch);
ch = fgetc(fp);
printf("ch = %c\n", ch);
fclose(fp);
return 0;
}
#include <stdio.h>
int main(int argc, const char *argv[])
{
FILE *fp = NULL;
char Tmp = 0;
char Str[1024] = {
0};
char *pTmp = NULL;
fp = fopen("file.txt", "r");
if (NULL == fp)
{
perror("fail to fopen");
return -1;
}
pTmp = Str;
while (1)
{
Tmp = fgetc(fp);
if (EOF == Tmp)
{
break;
}
*pTmp = Tmp;
pTmp++;
}
printf("%s", Str);
fclose(fp);
return 0;
}
5.fputs
int fputs(const char *s, FILE *stream);
功能:
向流中写入一个字符串
参数:
s:字符串空间首地址
stream:文件流指针
返回值:
成功返回非负整数
失败返回EOF
#include <stdio.h>
int main(int argc, const char *argv[])
{
FILE *fp = NULL;
char Tmp = 0;
if (argc != 2)
{
fprintf(stderr, "Usage:./a.out filename!\n");
return -1;
}
fp = fopen(argv[1], "r");
if (NULL == fp)
{
perror("fail to fopen");
return -1;
}
while ((Tmp = fgetc(fp)) != EOF)
{
putchar(Tmp);
}
fclose(fp);
return 0;
}
**cp.c**
#include <stdio.h>
int main(int argc, const char *argv[])
{
FILE *fSrc = NULL;
FILE *fDst = NULL;
char Tmp = 0;
if (argc < 3)
{
fprintf(stderr, "Usage:./a.out srcfile dstfile!\n");
return -1;
}
fSrc = fopen(argv[1], "r");
if (NULL == fSrc)
{
perror("fail to fopen");
return -1;
}
fDst = fopen(argv[2], "w");
if (NULL == fDst)
{
perror("fail to fopen");
return -1;
}
while ((Tmp = fgetc(fSrc)) != EOF)
{
fputc(Tmp, fDst);
}
fclose(fSrc);
fclose(fDst);
return 0;
}
#include <stdio.h>
int main(int argc, const char *argv[])
{
FILE *fp = NULL;
char TmpBuff[32] = "how are you";
fp = fopen("file.txt", "w");
if (NULL == fp)
{
perror("fail to fopen");
return -1;
}
fputs("hello world", fp);
fputs(TmpBuff, fp);
fclose(fp);
return 0;
}
6.fgets
char *fgets(char *s, int size, FILE *stream);
功能:
从流读取一个字符串
参数:
s:存放字符串空间首地址
size:读取字符个数
stream:文件流指针
返回值:
成功返回读到字符串空间首地址
失败返回NULL
读到文件末尾返回NULL
练习:利用fgets实现cat的功能
./a.out filename
利用fgets和fputs实现文件内容的拷贝
./a.out srcfile dstfile
#include <stdio.h>
int main(int argc, const char *argv[])
{
FILE *fp = NULL;
char TmpBuff[4096] = {
0};
char *pRet = NULL;
if (argc < 2)
{
fprintf(stderr, "Usage:./a.out filename!\n");
return -1;
}
fp = fopen(argv[1], "r");
if (NULL == fp)
{
perror("fail to fopen");
return -1;
}
while (fgets(TmpBuff, sizeof(TmpBuff), fp) != NULL)
{
printf("%s", TmpBuff);
}
fclose(fp);
return 0;
}
fgets gets
fgets不会去掉从终端接收的\n字符
gets会去掉从终端接收的\n字符
fgets比gets更加安全
fputs puts
puts会多打印一个\n字符
fputs不会多打印字符
fgetc fputc ASCII码文件
fgets fputs ASCII码文件
fread fwrite 二进制文件
#include <stdio.h>
int main(int argc, const char *argv[])
{
FILE *fp = NULL;
char TmpBuff[10] = {
0};
fp = fopen("file.txt", "r");
if (NULL == fp)
{
perror("fail to fopen");
return -1;
}
fgets(TmpBuff, sizeof(TmpBuff), fp);
printf("TmpBuff = %s\n", TmpBuff);
fgets(TmpBuff, sizeof(TmpBuff), fp);
printf("TmpBuff = %s\n", TmpBuff);
fgets(TmpBuff, sizeof(TmpBuff), fp);
printf("TmpBuff = %s\n", TmpBuff);
fclose(fp);
return 0;
}
7.fwrite
size_t fwrite(const void *ptr, size_t size, size_t nmemb,
FILE *stream);
功能:
向流中写入ptr指针指向空间的nmemb个对象,每个对象size个字节
参数:
ptr:写入数据空间的首地址
size:写入对象的字节数
nmemb:写入对象的个数
stream:文件流指针
返回值:
成功返回实际写入对象的个数
失败返回0
#include <stdio.h>
struct student
{
char name[32];
char sex;
int age;
int score;
};
int main(int argc, const char *argv[])
{
struct student s = {
"zhangsan", 'm', 19, 100};
struct student a = {
"lisi", 'f', 18, 90};
struct student stu[2] = {
{
"wanger", 'm', 17, 100},
{
"zhaowu", 'f', 16, 80},
};
FILE *fp = NULL;
fp = fopen("file.txt", "w");
if (NULL == fp)
{
perror("fail to fopen");
return -1;
}
fwrite(&s, sizeof(s), 1, fp);
fwrite(&a, sizeof(s), 1, fp);
fwrite(stu, sizeof(struct student), 2, fp);
fclose(fp);
return 0;
}
8.fread
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
功能:
从流中读取nmemb个对象,每个对象size个字节,放入ptr指向的空间中
参数:
ptr:存放数据空间首地址
size:对象的大小
nmemb:对象个数
stream:文件流指针
返回值:
成 功返回实际读到对象的个数
失败返回0
读到文件末尾返回0
#include <stdio.h>
struct student
{
char name[32];
char sex;
int age;
int score;
};
int ShowStuInfo(struct student tmp)
{
printf("姓名:%s\n", tmp.name);
printf("性别:%c\n", tmp.sex);
printf("年龄:%d\n", tmp.age);
printf("成绩:%d\n", tmp.score);
return 0;
}
int main(int argc, const char *argv[])
{
FILE *fp = NULL;
struct student a;
struct student b;
struct student stu[2];
size_t Ret;
fp = fopen("file.txt", "r");
if (NULL == fp)
{
perror("fail to fopen");
return -1;
}
while (fread(&a, sizeof(a), 1, fp) != 0)
{
ShowStuInfo(a);
}
#if 0
Ret = fread(&a, sizeof(a), 1, fp);
printf("Ret = %d\n", Ret);
ShowStuInfo(a);
Ret = fread(&b, sizeof(b), 1, fp);
printf("Ret = %d\n", Ret);
ShowStuInfo(b);
Ret = fread(stu, sizeof(struct student), 2, fp);
printf("Ret = %d\n", Ret