0

So I'm new in C programming and I am having trouble using my text file to input to my program.This is a program that inputs a text file to the program. But after it compiles, it doesn't input the data. Can someone please help me find out what is wrong? Thank you!

Here is the text file: masks.txt

4

32 67 56 88 172 210 316 400 288 321

47 69 78 100 121 155 467 560 199 278

25 61 72 102 160 225 319 399 171 212

25 41 82 102 160 225 309 388 271 293

CODE:

#include <stdio.h>
#include <limits.h>
#define BUFFERSIZE 100

int main(int argc, char *argv[])
{
    FILE *fp;
    char ch;

    fp = fopen("masks.txt", "r");
    if (fp == NULL)
    {
        printf("Error. File not found!\n");
        return (-1);
    }

    char buffer[BUFFERSIZE];
    fgets(buffer, BUFFERSIZE, stdin);

    int n, count = 0, days = 1;
    float sum = 0, total = 0, min = INT_MAX, max = INT_MIN;

    float lowest = INT_MAX, highest = INT_MIN;

    scanf("%d", &n);
    printf("Project #4\n");
    printf("Number of days: ", n);

    printf("\n\n");

    printf("\t\t t1\t t2\t t3\t t4\t t5\t Aver\t Min\t Max\n");
    printf("\t\t-----\t-----\t-----\t-----\t-----\t-----\t-----\t-----\n");

    while (!feof(fp))
    {
        if (count % 5 == 0)
        {
            printf("day #", days, ":\t\t");
            days++;
        }
    
        int withMasks, totalPeople;
        scanf("%d", withMasks);
        scanf("%d", totalPeople);
     
        float percentage = (withMasks / totalPeople) * 100;
    
        if (percentage > max)
        {
            max = percentage;
        }
        else if (percentage < min)
        {
            min = percentage;
        }
        else if (percentage > highest)
        {
            highest = percentage;
        }
        else (percentage < lowest);
        {
            lowest = percentage;
        }
    
        sum += percentage;
        printf("%0.1f%%\t", percentage);
        count++;
    
        if (count % 5 == 0)
        {
            printf("%0.1f%%\t", (sum / 5));
            printf("%0.1f%%\t", min);
            printf("%0.1f%%\t", max);
        
            total += (sum / 5);
        
            sum = 0;
            min = INT_MAX, max = INT_MIN;
        
            printf("\n");
        }   
     }

     printf("\n\nOverall average: %0.1f%%", (total / n));
     printf("\nLowest overall: %0.1f%%", lowest);
     printf("\nHighest overall: %0.1f%%", highest);

     fclose(fp);
     return 0;
qral
  • 1
  • 2
  • `scanf("%d", &n);` The `scanf` function is for reading from stdin/terminal. Use `fscanf` with the `fp` to read from the file (at the moment you don't call any function that reads from the file). Or my preference: `fgets` followed by `sscanf`. – kaylum Oct 05 '21 at 04:42
  • Or are you actually trying to redirect the file as stdin? If that is the case you should not be opening the file in the code. Instead read from stdin and then use `<` to redirect the file. For example: `./my_program < masks.txt` – kaylum Oct 05 '21 at 04:46
  • You are never using `fp` for any data input, except in one place that [happens to be wrong](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong). – n. m. could be an AI Oct 05 '21 at 05:38
  • got it! Thanks for the help! – qral Oct 18 '21 at 01:21

0 Answers0