/** * 写文件 .dat file. * */ void writingDatFile() { int i; FILE *outFile; float price[] = { 39.5,3.22,1.03 }; string *descrip; //char descrip[] = { "苹果","香焦","䔧"}; //,"\0" //char* str_arr[] = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" }; string descrip2[] = { "苹果","香焦","䔧" }; descrip = descrip2; outFile = fopen("prices.dat", "w"); if (outFile == NULL) { printf("文件不存在!"); ofstream fout(outFile); //创建文件 } 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; string* 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); }
标签:price,dat,outFile,file,descrip,inFile From: https://www.cnblogs.com/geovindu/p/17740828.html