【发布时间】:2021-11-02 14:09:32
【问题描述】:
我正在尝试打开和读取 csv 文件 当我尝试执行此操作时,插入此问题的窗口会显示此内容。 我该如何解决这个问题或我做错了什么?
我尝试了很多事情,但无法弄清楚我做错了什么。我正在 devc++ 中执行此操作,目前正在寻找答案
输出应该是:
Starting time Fri Aug 06 13:06:40 2010
Ending time Thu Apr 10 13:09:09 2014
Temperature Sensor 1 Average: -459.7ºF
Temperature Sensor 2 Average: 440.3ºF
Temperature Sensor 3 Average: 77.2ºF
这是我打开的文件:
Test File #1 35 lines inc. header
1281100000,0,2047,1221
1281200000,0,2047,1221
1321300000,0,2047,1221
1331400000,0,2047,1221
1341703600,0,2047,1221
1351707899,0,2047,1221
1361703600,0,2047,1221
1371200000,0,2047,1221
1371300000,0,2047,1221
1387400000,0,2047,1221
1387703600,0,2047,1221
1388707899,0,2047,1221
1389703600,0,2047,1221
1390200000,0,2047,1221
1390300000,0,2047,1221
1390400000,0,2047,1221
1390703600,0,2047,1221
1390707899,0,2047,1221
1391703600,0,2047,1221
1392200000,0,2047,1221
1392300000,0,2047,1221
1392400000,0,2047,1221
1392703600,0,2047,1221
1392707899,0,2047,1221
1393703600,0,2047,1221
1394200000,0,2047,1221
1394300000,0,2047,1221
1394400000,0,2047,1221
1394703600,0,2047,1221
1394707899,0,2047,1221
1395703600,0,2047,1221
1395713600,0,2047,1221
1396707899,0,2047,1221
1397135349,0,2047,1221
#include <stdio.h>
#include <windows.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
// For Red, Blue And Yellow Color
#define RED "\x1B[31m"
#define BLU "\x1B[34m"
#define YEL "\x1B[33m"
// Color Reset
#define RESET "\033[0m"
// Print Long Date In Human Readable Format
void printDate(unsigned long date) {
time_t seconds = date;
struct tm* tm = localtime(&date);
char months[][4] = {
"JAN", "FEB", "MAR", "APR", "MAY", "JUN",
"JUL", "AUG", "SEP", "OCT", "NOV", "DEC"
};
// Print Fomatted Date
printf("%02d-%s-%d %02d:%02d:%02d", tm->tm_mday, months[tm->tm_mon], tm->tm_year + 1900,
tm->tm_hour, tm->tm_min, tm->tm_sec);
}
// Convert Reading To Fahrenheight
double readingToFahrenheit(double reading) {
double mv = ((reading / 2047) * 5000);
double kelvin = mv / 10;
return (kelvin - 273.15) * (9.0/5.0) + 32;
}
// Print Colored Temperature Output
void printColoredOutput(double temp) {
if (temp < 50.0) {
printf("%s%.2f F%s", BLU, temp, RESET);
} else if (temp > 90.0) {
printf("%s%.2f F%s", RED, temp, RESET);
} else {
printf("%s%.2f F%s", YEL, temp, RESET);
}
}
// Main
int main(void) {
char filePath[256];
// Read File Name From The User
printf("Enter The File Path: ");
fgets(filePath, sizeof(filePath) - 1, stdin);
// Remove New Line From The End Of File Path
filePath[strlen(filePath) - 1] = '\0';
FILE * fp;
fp = fopen(filePath, "r");
if (fp == NULL) {
printf("Error: %s Cannot Be Opened!\n", filePath);
return -1;
} else {
// Process File
unsigned long date;
int temp1, temp2, temp3;
int lineCount = 0;
double sumTemp1, sumTemp2, sumTemp3;
// First Line
if ((fscanf(fp, "%lu,%d,%d,%d", &date, &temp1, &temp2, &temp3)) == 4) {
printf("Starting Time: ");
printDate(date);
sumTemp1 = temp1;
sumTemp2 = temp2;
sumTemp3 = temp3;
lineCount++;
}
// Rest Of The Lines
while ((fscanf(fp, "%lu,%d,%d,%d", &date, &temp1, &temp2, &temp3)) == 4) {
sumTemp1 += temp1;
sumTemp2 += temp2;
sumTemp3 += temp3;
lineCount++;
}
// Display Result
printf("\nEnding Time: ");
printDate(date);
// Compute Average
double temp1Avg = sumTemp1 / lineCount;
double temp2Avg = sumTemp2 / lineCount;
double temp3Avg = sumTemp3 / lineCount;
// Print Average
printf("\nTemperature Sensor 1 Average: ");
printColoredOutput(readingToFahrenheit(temp1Avg));
printf("\nTemperature Sensor 2 Average: ");
printColoredOutput(readingToFahrenheit(temp2Avg));
printf("\nTemperature Sensor 3 Average: ");
printColoredOutput(readingToFahrenheit(temp3Avg));
printf("\n");
}
return 0;
}
【问题讨论】:
-
您忘记提及您想要得到什么,即输出错误的方式。您是指为文本输出着色的转义码吗?
-
输出应该是:
-
开始时间 Fri Aug 06 13:06:40 2010 结束时间 Thu Apr 10 13:09:09 2014 温度传感器 1 平均值:-459.7ºF 温度传感器 2 平均值:440.3ºF 温度传感器 3 平均值: 77.2ºF
-
不要发布文字图片,而是将文字作为文字发布。程序输出为文本。
-
你应该添加你打开的文件的内容。然后我们可以检查为什么
fscanf不返回值 4。
标签: c