【问题标题】:Can someone tell why this code doesn't work有人能说出为什么这段代码不起作用
【发布时间】: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;
        }

the window when I try to run the program

【问题讨论】:

  • 您忘记提及您想要得到什么,即输出错误的方式。您是指为文本输出着色的转义码吗?
  • 输出应该是:
  • 开始时间 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


【解决方案1】:

fscanf 和 if 工作正常。你在第一次打印和 fgets 之间有一个问题。您需要在 fgets 之前刷新输出:

printf("Enter The File Path: ");
fflush(stdout);
fgets(filePath, sizeof(filePath) - 1, stdin);

也在日期转换中做:

   // Print Long Date In Human Readable Format
    void printDate(unsigned long date) {
        time_t seconds = date;
        struct tm* tm = localtime(& seconds);

结果:

Enter The File Path: headf
Starting Time: 06-AUG-2010 13:06:40
Ending Time: 10-APR-2014 13:09:09
Temperature Sensor 1 Average: -459.67 F
Temperature Sensor 2 Average: 440.33 F
Temperature Sensor 3 Average: 77.16 F
$ 

关于so有一些解释。

【讨论】:

  • 那行不通
  • 我用 fflush 添加了结果输出
  • 我仍然迷路我尝试这样做并得到了相同的结果
  • 我看到你在 devc++ 工作。我使用了c编译器clang。我还删除了我没有的包含 window.h。
  • 我忘了我用秒变量更改了 tm 的声明。
【解决方案2】:

经过我的 3 处修改的完整代码:

    #include <stdio.h>
    //#include <windows.h> // CHANGE 1
    #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;
       // CHANGE 2 :
        struct tm* tm = localtime(&seconds);
        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: ");
       // CHANGE 3 :
    fflush(stdout);
        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;
    }

【讨论】:

    猜你喜欢
    • 2022-12-16
    • 1970-01-01
    • 2021-06-23
    • 2010-09-18
    相关资源
    最近更新 更多