1、fprintf
#define _CRT_SECURE_NO_DEPRECATE
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
FILE* pFile = fopen("C:\\Users\\lucky\\Desktop\\22.txt", "w");
fprintf(pFile, "%d,%s %.lf", 12, "hellomw", 12.34);//%s之后必须是空格,否则fscanf会无法读出str后的数;可以规范浮点数的个数;
fclose(pFile);
system("pause");
return 0;
}
2、fscanf
#define _CRT_SECURE_NO_DEPRECATE
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
FILE* pFile = fopen("C:\\Users\\lucky\\Desktop\\22.txt", "r");
//fprintf(pFile, "%d,%s,%lf", 12, "hellomw", 12.34);
int a = 0;
char str[20] = {0};
double d = 0.0;
fscanf(pFile, "%d,%s %lf", &a, str, &d);//%s之后必须是空格,否则会与后面的数据相结合输出;且浮点数不能规范位数,否则无法读出
fclose(pFile);
system("pause");
return 0;
}
本文介绍了C语言中使用fprintf和fscanf进行文件写入与读取的方法。通过具体示例展示了如何利用这两个函数来处理整数、字符串及浮点数等不同数据类型。
643

被折叠的 条评论
为什么被折叠?



