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();

输出:

相关推荐
故事和你913 小时前
洛谷-【图论2-1】树6
开发语言·数据结构·c++·算法·深度优先·动态规划·图论
被AI抢饭碗的人3 小时前
C++过渡Python
开发语言·python
不知名的老吴3 小时前
在C++中不用宏怎么打日志的使用建议
开发语言·c++·算法
jieyucx3 小时前
Go 语言进阶:结构体指针、new 关键字与匿名结构体/成员详解
开发语言·后端·golang·结构体
wjs20243 小时前
jEasyUI 添加复选框指南
开发语言
迪霸LZTXDY3 小时前
U-NET模型训练--图像标注脚本工具
开发语言·python
码界筑梦坊3 小时前
119-基于Python的各类企业排行数据可视化分析系统
开发语言·python·信息可视化·数据分析·毕业设计·echarts·fastapi
习明然3 小时前
记录下解决Python在windows 2008 Server 无法启动
开发语言·windows·python
凯瑟琳.奥古斯特3 小时前
IP组播跨子网传输核心技术解析
java·开发语言·网络·网络协议·职场和发展
xyq20243 小时前
Razor VB 循环
开发语言