fscanf和fprintf

Posted 芬乐

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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    [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");
    
}

 

以上是关于fscanf和fprintf的主要内容,如果未能解决你的问题,请参考以下文章

fscanf函数和fprintf函数fgets函数和fputs函数fread函数和fwrite函数

fscanf()函数详解&&fprintf()

C语言fscanf/fprintf函数(格式化读写文件)的用法(%[]和%n说明符)

linux 操作系统下能用fprintf() 及fscanf()等函数吗?它们与c语言中的用法一样吗?

请教fprintf函数的用法

fscanf读入文件时错误的原因?