c: dat file

cpp 复制代码
/**
 * @file geovindu.c
 * @author geovindu (Geovin Du)
 * @brief 
 * @version 0.1
 * @date 2023-10-03
 * 
 * @copyright Copyright (c) 2023
 * 
 */

#include "include/geovindu.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
//#include <iostream> C++
//#include <iomanip>


/**
 * 写文件 .dat file.
 *
 */
void writingDatFile()
{
    int i;
    FILE *outFile;
    float price[] = { 39.5,3.22,1.03 };   
      //char *descrip;
      char *descrip[3] = {{"苹果"},{"香焦"},{"苹果"}};
    //char *descrip[3]={ {"苹果"},{"香焦"},{"䔧"}};
     //char *descrip[]={"home","geovindu","du"};
    
    outFile = fopen("prices.dat","a+");//ios::app(C++追加)  w+  a+(C追加)
    if (outFile == NULL)
    {
        printf("文件不存在!");
        //ofstream fout(outFile); //创建文件
         outFile = fopen("prices.dat", "wb");
    }/**/
    for (i = 0; i < 3; ++i)
    {
        fprintf(outFile, "%-9s  %5.2f\n", descrip[i], price[i]);
    }
    fclose(outFile);
 
}
/**
 * 读文件 .dat file.
 *
 */
void readDatFile()
{
    FILE* inFile;
    float price;
    char *descrip[10];
    inFile = fopen("prices.dat", "r");
    if (inFile == NULL)
    {
        printf("\n 文件不存在");
        exit(0);
    }
    while (fscanf(inFile, "%s %f", descrip, &price) != EOF)
    {
        printf("%-9s %5.2f\n", descrip, price);
    }
    fclose(inFile);
 
}

调用:

cpp 复制代码
    printf("hello world,geovindu\n");
    writingDatFile();
    readDatFile();

输出:

相关推荐
传奇开心果编程19 分钟前
【dioxus0.7基础语法学与练】第8课:RSX 宏 — Dioxus 声明式 UI 语法
开发语言·学习·rust
wuyk5551 小时前
18.Kruskal 算法:用最短的边连成一张网
开发语言·算法·图论
Evand J1 小时前
【MATLAB例程,图像降噪7】高斯加权滑动窗口滤波,用于图像降噪,带质量评价,附代码下载链接
开发语言·图像处理·matlab·滑动窗口·滤波·降噪·图像滤波
十年一梦惊觉醒1 小时前
快手视频发布助手,全自动视频发布带货工具
开发语言·前端·javascript
Java后端的Ai之路1 小时前
一文搞懂 GitHub Actions-CICD
开发语言·大模型·github·cicd·action
码匠许师傅1 小时前
【C++三方组件】开篇总论:为什么需要以及如何选型?
开发语言·c++
cvby1 小时前
C++11--右值引用和移动语义
开发语言·c++
en.en..1 小时前
TCP/UDP 收发流程(网络字节序---函数讲解)
开发语言·php
名字还没想好☜2 小时前
Go 用 json.Decoder 流式解析大 JSON:边读边处理不爆内存
开发语言·后端·golang·go·json
xiangyun612 小时前
【408数据结构 03】线性表与顺序表:C++手写SeqList
开发语言·数据结构·c++