1.函数原型:
- int fprintf(FILE *fp, const char *format[,argument, ...])
- int fscantf(FILE *fp, const char *format[,address, ...])
2.功能:按格式对问件进行I/O操作
3.返回值:
- 成功,返回I/O的个数,出错或文件尾,返回EOF。
- fprintf()返回值就是写入成功的字符的个数。
- fscanf()返回值是扫描到几个数据,这个字符串不管有多长,要扫描的每一种类型算一个数据,%s算一个,%d算一个。详见eg3.
4.例子:
fprintf(fp, "%d, %6.2f, i,t"); //将i和t按%d,%6.2f格式输出到fp文件
fscanf(fp, "%d,%f", &i,&t)/ //若文件中有3,4.5,则将3送入i,4.5送入t。
eg1:
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include <stdlib.h> void main1() { //printf("hello"); //fprintf(stdout, "hello");//printf只是fprintf特例 char str[100] = { 0 }; //scanf("%s", str); fscanf(stdin, "a=%s", str);//从字符串中取出,键盘缓冲区,输入时,要输入“a=abc”,下面才会打印。 fprintf(stdout, "%s", str);//int string映射到一个字符串 显示器缓冲区 getchar(); getchar(); } //1 [email protected] xijaxi16ytja6t7ibcrj void main2() { char str[256] = { 0 }; { int i = 1; char email[100] = "[email protected]"; char password[100] = "xijaxi16ytja6t7ibcrj"; sprintf(str, "%d %s %s", i, email, password); fprintf(stdout, "\n%s", str); } { int j; char emailx[100] = { 0 }; char passmd5[100] = { 0 }; sscanf(str, "%d%s%s", &j, emailx, passmd5); fprintf(stdout, "\nj=%d eamilx=%s passmd5=%s ", j, emailx, passmd5); } system("pause"); }
eg2:
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> typedef struct info7k7k { char user[50]; char password[50]; }INOF,* PINOF; void main2x() { FILE *pfr = fopen("Z:\\I\\尹成清华终极版C语言视频源码文档20150131\\大数据相关数据\\7k7k2000万_2047\\2000-1\\7k7kOK.txt", "r"); FILE *pfw = fopen("Z:\\I\\尹成清华终极版C语言视频源码文档20150131\\大数据相关数据\\7k7k2000万_2047\\2000-1\\7k7kOKwithid.txt", "w"); int i = 0; while (!feof(pfr)) { i++; INOF info1; fscanf(pfr, "%s%s", info1.user, info1.password);//提取 fprintf(pfw, "%d %s %s\n", i, info1.user, info1.password);//组合 } fclose(pfr); fclose(pfw); system("pause"); } void main3x() { //int num = fprintf(stdout, "helloword%s","1234"); //printf("\n%d", num);//fprintf返回值就是写入成功字符的个数 FILE *pf = fopen("C:\\x.txt", "r"); int num = fprintf(pf, "helloword%s", "1234");//写入失败返回-1 printf("\n%d", num); system("pause"); } void main() { //char str[128] = { 0 }; //int numa; //int numb; //int num = fscanf(stdin, "%s%d%d",str,&numa,&numb); ////返回值是扫描到几个数据,失败返回-1 //printf("\n%d", num); FILE *pf = fopen("C:\\x.txt", "w"); char str[128] = { 0 }; int numa; int numb; int num = fscanf(pf, "%s%d%d",str,&numa,&numb); printf("\n%d", num); system("pause"); }
eg3:
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> typedef struct info7k7k { char user[50]; char password[50]; }INOF,* PINOF; void main2x() { FILE *pfr = fopen("Z:\\I\\尹成清华终极版C语言视频源码文档20150131\\大数据相关数据\\7k7k2000万_2047\\2000-1\\7k7kOK.txt", "r"); FILE *pfw = fopen("Z:\\I\\尹成清华终极版C语言视频源码文档20150131\\大数据相关数据\\7k7k2000万_2047\\2000-1\\7k7kOKwithid.txt", "w"); int i = 0; while (!feof(pfr)) { i++; INOF info1; fscanf(pfr, "%s%s", info1.user, info1.password);//提取 fprintf(pfw, "%d %s %s\n", i, info1.user, info1.password);//组合 } fclose(pfr); fclose(pfw); system("pause"); } void main3x() { //int num = fprintf(stdout, "helloword%s","1234"); //printf("\n%d", num);//fprintf返回值就是写入成功字符的个数 FILE *pf = fopen("C:\\x.txt", "r"); int num = fprintf(pf, "helloword%s", "1234");//写入失败返回-1 printf("\n%d", num); system("pause"); } void main() { //char str[128] = { 0 }; //int numa; //int numb; //int num = fscanf(stdin, "%s%d%d",str,&numa,&numb); ////返回值是扫描到几个数据,失败返回-1 //printf("\n%d", num); FILE *pf = fopen("C:\\x.txt", "w"); char str[128] = { 0 }; int numa; int numb; int num = fscanf(pf, "%s%d%d",str,&numa,&numb); printf("\n%d", num); system("pause"); }
时间: 2024-10-18 09:58:29