• fscanf函数用法及注意事项


    /*FSCANF.C:This program writes formatted data to afile.It then uses fscanf to read the various databackfromthefile.*/
    #include <stdio.h>
    FILE *stream;
    int main(void)
    {
    long l;
    float fp;
    char s[81];
    char c;
    stream=fopen("fscanf.out","w+");
    if(stream==NULL)
    printf("The file fscanf.out was not opened ");
    else
    {
    fprintf(stream,"%s%ld%f%c","a-string",
    65000,3.14159,'x');
    /*Set pointer to beginning of file:*/
    fseek(stream,0L,SEEK_SET);
    /*Readdatabackfromfile:*/
    fscanf(stream,"%s",s);
    fscanf(stream,"%ld",&l);
    fscanf(stream,"%f",&fp);
    fscanf(stream,"%c",&c);
    /*Output data read:*/
    printf("%s ",s);
    printf("%ld ",l);
    printf("%f ",fp);
    printf("%c ",c);
    fclose(stream);
    }
    }
     
     

    注意事项:

     fscanf(FILE * stream ,constchar*format, [argument...] );

    如果argument为char* str时
    str是一个指向字符串数组的指针,用来拷贝读取到的字符串
    所以, 可以是 char s[128]
    也可以是 char* s = (char *)malloc(128)

    但不可以是 char* s; s没有指向有效的内存空间




    FILE*fp;
     
    char a[10];
     
    int b;
     
    double c;
     
    fscanf(fp,"%s%d%lf",a,&b,&c)




    printf("%g",4.5);//4.5
    printf("%f",4.5);//4.500000
    printf("%e",400.5);//4.5e+2
  • 相关阅读:
    WPF listbox 实现动态滚轮下拉定位
    VS的安装和入门使用
    pyqt5学习之菜单栏,工具栏,状态栏
    pyqt5学习之QSpinBox
    pyqt5环境安装
    pyqt5学习之QKeySequeueEdit
    pyqt5学习之QPainTextEditer
    pyqt5学习之QTextEditer
    pyqt5学习之QABstractScrollArea
    pyqt5学习之QFrame
  • 原文地址:https://www.cnblogs.com/mybabyyh/p/4202254.html
Copyright © 2020-2023  润新知