69.fprintf fscanf

  • fprintf
    1 //从读文件中提取字符串到info1.user和info1.password中
    2 fscanf(pfr, "%s%s", info1.user, info1.password);

     

  • fscanf
    1 //格式化写入到文件中
    2 fprintf(pfw, "%d %s %s\n", i, info1.user, info1.password);

     

  • printf 和 scanf是特例
    1 //从字符串中取出,键盘缓冲区
    2 fscanf(stdin, "a=%s", str);
    3 //int string映射到一 个字符串 显示器缓冲区
    4 fprintf(stdout, "%s", str);

     

完整代码:

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include <stdio.h>
 3 #include <stdlib.h>
 4 
 5 //创建结构体
 6 typedef struct info7k7k
 7 {
 8     char user[50];
 9     char password[50];
10 
11 }INOF,* PINOF;
12 
13 void main2x()
14 {
15     //以读的方式打开文件
16     FILE *pfr = fopen("7k7kOK.txt", "r");
17     //以写的方式打开文件
18     FILE *pfw = fopen("7k7kOKwithid.txt", "w");
19     //编号
20     int i = 0;
21     //如果没到文件末尾
22     while (!feof(pfr))
23     {
24         i++;
25         //创建结构体
26         INOF info1;
27         //从读文件中提取字符串到info1.user和info1.password中
28         fscanf(pfr, "%s%s", info1.user, info1.password);
29         //格式化写入到文件中
30         fprintf(pfw, "%d %s %s\n", i, info1.user, info1.password);
31     }
32     //关闭文件
33     fclose(pfr);
34     fclose(pfw);
35     system("pause");
36 }
37 
38 void main3x()
39 {
40     //int num = fprintf(stdout, "helloword%s","1234");
41     //printf("\n%d", num);//fprintf返回值就是写入成功字符的个数
42     FILE *pf = fopen("C:\\x.txt", "r");
43     int num = fprintf(pf, "helloword%s", "1234");//写入失败返回-1
44     printf("\n%d", num);
45     system("pause");
46 }
47 
48 void main()
49 {
50     //char str[128] = { 0 };
51     //int numa;
52     //int  numb;
53     //int num = fscanf(stdin, "%s%d%d",str,&numa,&numb);
54     ////返回值是扫描到几个数据,失败返回-1
55     //printf("\n%d", num);
56     FILE *pf = fopen("C:\\x.txt", "w");
57     char str[128] = { 0 };
58     int numa;
59     int  numb;
60     int num = fscanf(pf, "%s%d%d",str,&numa,&numb);
61     printf("\n%d", num);
62 
63     system("pause");
64     
65 }

 

posted @ 2018-02-19 14:42  喵小喵~  阅读(201)  评论(0编辑  收藏  举报