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

输出:

相关推荐
c4fx13 分钟前
Delphi5利用DLL实现窗体的重用
开发语言·delphi·dll
鸽芷咕37 分钟前
【Python报错已解决】ModuleNotFoundError: No module named ‘paddle‘
开发语言·python·机器学习·bug·paddle
Jhxbdks1 小时前
C语言中的一些小知识(二)
c语言·开发语言·笔记
java6666688881 小时前
如何在Java中实现高效的对象映射:Dozer与MapStruct的比较与优化
java·开发语言
Violet永存1 小时前
源码分析:LinkedList
java·开发语言
代码雕刻家1 小时前
数据结构-3.1.栈的基本概念
c语言·开发语言·数据结构
Fan_web1 小时前
JavaScript高级——闭包应用-自定义js模块
开发语言·前端·javascript·css·html
梦想科研社1 小时前
【无人机设计与控制】四旋翼无人机俯仰姿态保持模糊PID控制(带说明报告)
开发语言·算法·数学建模·matlab·无人机
风等雨归期1 小时前
【python】【绘制小程序】动态爱心绘制
开发语言·python·小程序
千穹凌帝1 小时前
SpinalHDL之结构(二)
开发语言·前端·fpga开发