-3

I'm trying to open and read csv file When I try to do this the window inserted in this question displays this. How can I fix this or what am I doing wrong?

I have tried muiltple things and cannot figure out what I am doing wrong. I am doing this in devc++ and am looking for answers at this point

The output should be:

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

This is the file I opened:

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

Gerhardh
  • 11,688
  • 4
  • 17
  • 39
jimbo
  • 1
  • 3
  • You forgot to mention what you want to get, i.e. in what way the output is wrong. Do you mean the escape codes for colouring the text output? – Gerhardh Nov 02 '21 at 14:11
  • output should be: – jimbo Nov 02 '21 at 14:26
  • 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 – jimbo Nov 02 '21 at 14:26
  • 1
    Don't post pictures of text but post text as text. Program output is text. – Jabberwocky Nov 02 '21 at 14:28
  • The fact that `Starting Time: ` isn't seen in the output suggests the first `fscanf` call on the file isn't succeeding. That would result in `sumTemp1`, `sumTemp2` and `sumTemp3` being used uninitialized by subsequent code. – G.M. Nov 02 '21 at 14:31
  • 1
    You should add the content of the file you opened. Then you we can check why the `fscanf` doesn't return value 4. – Gerhardh Nov 02 '21 at 14:32
  • Please don't format your expected output as bullet list. You don't print bullets. Generally nothing in your post is suitable for a bullet list at all. – Gerhardh Nov 02 '21 at 14:33
  • Minor: `reading / 2047` is suspicious. I suspect the right magic number is 2048. What temperature sensor are you using? – chux - Reinstate Monica Nov 02 '21 at 14:38
  • how could I fix the fscanf – jimbo Nov 02 '21 at 14:39
  • Your question now states `Test File #1 35 lines inc. header`: what header? That might explain why the first `fscanf` fails. – G.M. Nov 02 '21 at 14:52
  • Your formatting was not clear. Is "Test File #1... " part of the file or just your description? – Gerhardh Nov 02 '21 at 15:16
  • its part of the file – jimbo Nov 02 '21 at 15:18

2 Answers2

0

The fscanf and if works fine. You have just a problem between first prints and fgets. You need to flush output before fgets :

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

Also in date conversion do :

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

Result :

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
$ 

There are some explanations on so.

Ptit Xav
  • 3,006
  • 2
  • 6
  • 15
  • that didn't work – jimbo Nov 02 '21 at 16:37
  • I added the result output with the fflush – Ptit Xav Nov 02 '21 at 16:58
  • I am still lost I tried doing that and got the same result I've been getting – jimbo Nov 02 '21 at 17:04
  • I saw that you work devc++ . I used clang which is c compiler. I also removed the include window.h that I do not have. – Ptit Xav Nov 02 '21 at 17:19
  • I forgot I changed declaration of tm with seconds variable. – Ptit Xav Nov 02 '21 at 17:36
  • can you show me? – jimbo Nov 02 '21 at 17:50
  • I put it in the answer. That may be the issue. I did this because of a warning about wrong pointer type during compilation. – Ptit Xav Nov 02 '21 at 17:56
  • for some reason I'm still not getting the start time – jimbo Nov 02 '21 at 18:14
  • I put the full code which works for me – Ptit Xav Nov 02 '21 at 18:53
  • @jimbo according to your last comment below the question the line holding some text is first line of your input file. How should that match your first `scanf`: `"%lu,%d,%d,%d"`? `%lu` is not a good choice to read `"Test File"`. When you read the "first line" you actually want to skip the first line and read the second line. – Gerhardh Nov 04 '21 at 07:12
  • @Gerhardh how could I fix this – jimbo Nov 04 '21 at 12:20
  • @jimbo Really? SO is not a replacement for doing own research: A search for "skip line with fscanf" results in [How to skip a line when fscanning a text file?](https://stackoverflow.com/questions/2799612/how-to-skip-a-line-when-fscanning-a-text-file) withing 5 seconds. – Gerhardh Nov 04 '21 at 12:27
0

The full code with my 3 modifications:

    #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;
    }
Ptit Xav
  • 3,006
  • 2
  • 6
  • 15