-1

I am trying to code a program showing the minimum, maximum and mean values from an external file.

the file here is:

31.6    22.4    25.9
30.2    22.7    25.5
31.2    22.9    26.1
31.3    23.4    26.4
30.7    23.2    26.2
31.3    23.1    26.4
31.6    23.9    26.4
31.6    24.0    26.9
32.7    24.7    27.5
33.8    24.8    27.7
32.4    25.0    27.6
32.1    24.9    27.6
32.7    25.4    27.9
31.9    25.5    27.6
31.9    25.4    27.8
32.1    25.3    27.8
31.7    25.6    27.8
32.6    25.2    27.7
32.2    24.9    27.5
32.2    24.9    27.7
31.7    25.8    27.7
32.3    25.5    27.9
32.1    24.4    27.3
31.5    24.6    27.2
31.8    24.0    27.0 
32.0    24.4    27.4 
32.4    24.9    27.8
32.1    25.0    27.6

and this is the main code: to access and read the file:

#include <stdio.h>
#include <cstdlib>
void main(void)
{
    FILE *feb, *mar;
    int month, date, count = 0;
    double min, max, mean;
    char *pch;
    char line[256];

    printf("Input Month (2 or 3): ");
    scanf("%d", &month);

    if(month == 2)
    {
        feb = fopen("feb.txt", "r");

        if(!feb)
        {
            printf("404: File Not Found!");
        }

        else
        {
            printf("Input Date (1 ~ 28): ");
            scanf("%d", &date);
        }

        fclose(feb);
    }

    system("pause");
    return 0;
}

The problem is; I can't figure out a way to make it read a specific line and print the values out separately on the output screen.

  • 1
    I recommend you [find a good beginners book](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) and read about *structures* and *arrays*. Once you have an array of structures, it's easy to read all entries from the file into that. And once you have done that finding the right "date" is extremely easy. – Some programmer dude Jan 05 '17 at 13:03
  • For the impatient here has a program that does this. http://stackoverflow.com/questions/3501338/c-read-file-line-by-line – Marichyasana Jan 05 '17 at 13:13
  • For starting out, why not sticking the data into three separate arrays? Then you can try and use an array of structs afterwards. – RoadRunner Jan 05 '17 at 14:40

1 Answers1

3

Suggested approach, with examples:
1. Create a struct. For example:

typedef struct  {  
    double min;  
    double mean;  
    double max;  
} RECORD;  
RECORD *record;  

2. Count Lines in file. There are many ways to do this, see one example here.
Given the sample data you provided (i.e. that it appears to use the newline character \n), here is another example:

int longest=0;
int count = countLines("c:\\records.txt", &longest);

Where countlines is defined:

int countLines(char *file, int *longest)
{
    char line[260]; 
    int len=0, lenKeep=0, count=0;
    FILE *fp = fopen(file, "r");
    if(fp)
    {
        while(fgets(line, 80, fp))
        {
            len = strlen(line);
            if(len > lenKeep) lenKeep = len;
            count++;
        }
        fclose(fp);
    }
    *longest = lenKeep;
    return count;
}

3. Create an array of struct with count elements to contain records of file:

record = calloc(count, sizeof(RECORD));  
if(record)  
{... 

4. Read file line by line (see example in 1. ), and use sscanf or similar to parse line contents into three members of struct array. ( Some examples of using sscanf: here, here and here ). Given your line buffer is line, and i is some value between 0 and count, here is a one line parse example using sscanf:

sscanf(line, "%lf %lf %lf",&record[i].min, &record[i].mean, &record[i].max);  

5. Print selected record by using desired index of members. This code loops through all records, for example:

for(i=0;i<count;count++)
{
    printf("%d: %f %f %f\n", i, record[i].min, record[i].mean, record[i].max);  
}
Community
  • 1
  • 1
ryyker
  • 22,849
  • 3
  • 43
  • 87