zoukankan      html  css  js  c++  java
  • fscanf和fprintf

    fscanf和fprintf

    fscanf的字符串是在键盘的缓冲区,fprintf是在显示器的缓冲区。

    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    mndadmin@yahoo.com.cn    xijaxi16ytja6t7ibcrj
    void main2()
    {
    
        char str[256] = { 0 };
        {
            int i = 1;
            char email[100] = "mndadmin@yahoo.com.cn";
            char password[100] = "xijaxi16ytja6t7ibcrj";
    
            sprintf(str, "%d %s %s", i, email, password);
            fprintf(stdout, "
    %s", str);
        }
        {
            int j;
            char emailx[100] = { 0 };
            char passmd5[100] = { 0 };
            sscanf(str, "%d%s%s", &j, emailx, passmd5);
            fprintf(stdout, "
    j=%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
    ", i, info1.user, info1.password);//组合
        }
        fclose(pfr);
        fclose(pfw);
        system("pause");
    }
    
    void main3x()
    {
        //int num = fprintf(stdout, "helloword%s","1234");
        //printf("
    %d", num);//fprintf返回值就是写入成功字符的个数
        FILE *pf = fopen("C:\x.txt", "r");
        int num = fprintf(pf, "helloword%s", "1234");//写入失败返回-1
        printf("
    %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("
    %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("
    %d", num);
    
        system("pause");
        
    }

    eg3:

    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    #include <stdlib.h>
    
    
    typedef struct info
    {
        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
    ", i, info1.user, info1.password);//组合
        }
        fclose(pfr);
        fclose(pfw);
        system("pause");
    }
    
    void main3x()
    {
        //int num = fprintf(stdout, "helloword%s","1234");
        //printf("
    %d", num);//fprintf返回值就是写入成功字符的个数
        FILE *pf = fopen("C:\x.txt", "r");
        int num = fprintf(pf, "helloword%s", "1234");//写入失败返回-1
        printf("
    %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("
    %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("
    %d", num);
    
        system("pause");
        
    }
  • 相关阅读:
    集群、分布式、负载均衡区别与联系
    Android--加载大分辨率图片到内存
    Android--MP3播放器MediaPlayer
    Android--SoundPool
    Android--MediaPlayer高级
    Android--调用系统照相机拍照与摄像
    Android--SurfaceView播放视频
    Android--使用VideoView播放视频
    Android--使用Camera拍照
    Vue组件选项props
  • 原文地址:https://www.cnblogs.com/sjxbg/p/5883754.html
Copyright © 2011-2022 走看看