zoukankan      html  css  js  c++  java
  • c语言中国打开并读取文件

    1、

    #include <stdio.h>
    
    int main(void)
    {
        FILE *fp;  //打开文件前必须要定义FILE*型指针变量 
        
        int lines = 0;
        char name[128];
        double height, weight;
        double hsum = 0, wsum = 0;
        
        if((fp = fopen("hw.dat","r")) == NULL)
            printf("afile does not exist!
    ");
        else
        {
            while(fscanf(fp, "%s%lf%lf", name, &height, &weight) == 3) // fscanf函数用于读取文件,fscanf函数和scanf函数相似,多了一个参数,即流(指针)。
            //fscanf函数的返回值为成功赋值的项目 
            {
                printf("%-8s%8.3f%8.3f
    ", name, height, weight);
                lines++;
                hsum += height;
                wsum += weight;
            }
            puts("
    ============================
    ");
            printf("average: %8.3f%8.3f
    ", hsum/lines, wsum/lines);
            fclose(fp);
        }
        return 0;
    }

    c语言中读取文件的函数为fscanf函数,fscanf函数与scanf函数相似,多了一个参数,即流(指针), fscanf函数的返回值为成功赋值的项目数。

  • 相关阅读:
    windows下安装python模块
    红包demo
    如何查看python 的api
    vscode 与 python 的约会
    默认构造函数
    关于重载
    转类型转换
    asm-offset.h 生成
    debian 7 安装
    emacs 定制进缩风格
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/14856639.html
Copyright © 2011-2022 走看看