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

输出:

相关推荐
最贪吃的虎9 分钟前
【分布式】分布式事务框架 Seata 详解:架构与四种实现模式
java·开发语言·数据库·分布式·后端·架构
毕设源码-赖学姐16 分钟前
【开题答辩全过程】以 基于Java的彩票销售管理系统的设计与实现为例,包含答辩的问题和答案
java·开发语言
molaifeng16 分钟前
Go 并发控制的艺术:深入理解 context.Context
开发语言·后端·golang
水月wwww25 分钟前
【算法分析】指派问题
java·开发语言·算法
2501_9418663725 分钟前
面向接口限流与资源保护策略的互联网系统高并发设计与多语言工程实践分享
开发语言·python
lang2015092829 分钟前
Java信号量Semaphore:并发控制的利器
java·开发语言
程序猿编码33 分钟前
隐形的内核后门:无模块Linux内核植入技术详解
linux·c语言·内核·linux内核·内核植入
lkbhua莱克瓦2444 分钟前
进阶-索引4-使用规则
开发语言·数据库·笔记·sql·mysql·使用规则
未来之窗软件服务1 小时前
幽冥大陆(九十二 ) 封装 PHP HTTP 请求的 —东方仙盟练气期
开发语言·http·php·仙盟创梦ide·东方仙盟