#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
int fprintf(FILE * stream, const char * format, ...);
功能:根据参数format字符串来转换并格式化数据,然后将结果输出到stream指定的文件中,指定出现字符串结束符 '\0' 为止。
参数:
stream:已经打开的文件
format:字符串格式,用法和printf()一样
返回值:
成功:实际写入文件的字符个数
失败:-1
int fscanf(FILE * stream, const char * format, ...);
功能:从stream指定的文件读取字符串,并根据参数format字符串来转换并格式化数据。
参数:
stream:已经打开的文件
format:字符串格式,用法和scanf()一样
返回值:
成功:实际从文件中读取的字符个数
失败: - 1
fscanf 遇到空格或者遇到换行结束
*/
const char *fileName = "./a.txt";
void test()
{
FILE *fp = fopen(fileName, "w");
if (fp == NULL)
{
return;
}
fprintf(fp, "hello world!\n%s", "hello Obama!\n");
fclose(fp);
fp = NULL;
fp = fopen(fileName, "r");
if (fp == NULL)
{
return;
}
char buf[1024] = { 0 };
while (!feof(fp))
{
fscanf(fp, "%s", buf);
if (feof(fp))//只有读完之后fp指针才会移动
{
break;
}
printf("%s", buf);//输出helloworld!helloObama! 没有换行和空格
memset(buf, 0, 1024);
}
fclose(fp);
}
void main()
{
test();
system("pause");
}