c: dat file

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

#include "include/geovindu.h"
#include 
#include 
#include 
//#include  C++
//#include 


/**
 * 写文件 .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);
 
}

调用:
 

    printf("hello world,geovindu\n");
    writingDatFile();
    readDatFile();

输出:

c: dat file_第1张图片

你可能感兴趣的:(C,C++,c语言,开发语言)