Home > database >  Can someone tell why this code doesn't work
Can someone tell why this code doesn't work

Time:11-03

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("d-%s-%d d:d:d", 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

CodePudding user response:

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.

CodePudding user response:

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("d-%s-%d d:d:d", 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;
    }
  •  Tags:  
  • c
  • Related