# Thread: Reading in a file and printing out a graph from the data

1. ## Reading in a file and printing out a graph from the data

I'm provided with a file full of data on a car trip, here's an example:

Code:
```10 13
1.131232 0.000183
2.872323 0.000312
4.233291 0.000422
5.823823 0.000367
6.464139 0.000316
6.554324 0.000275
6.001023 0.000245
5.293232 0.0
7.012323 0.000612
7.013323 0.000583```
The 10 is the number of minutes the trip lasted (it will always be a multiple of 5 and less than 30), and the 13 is the radius of the tires. The following numbers on the left represent the revolutions the tires made in a given second, and the right numbers are the gas in gallons used in that second.

Basically we have to ask the user for the file the data is storred in and compile a graph from it like so:

Code:
```What file stores the car data?
sample.txt

95
90
85
80
75
70
65
60
55
50
45
40
35
30        *****
25        ***** *****
20        ***** *****
15        ***** *****
10  ***** ***** *****
5   ***** ***** *****
0   ***** ***** *****
-----------------------------------
00-05 05-10 10-15 15-20 20-25 25-30```
To be honest I'm a bit behind and I'm still having trouble printing out graphs and reading in files. This is what i've got so far:

Code:
```#include <stdio.h>

int main() {

FILE *ifp;
double revs, gas;

printf("What file stores the car data?\n");
scanf("%d", &file);

//Open file
ifp = fopen(file, "r");
We also have to include a function after the main function and I really lack that knowledge. I'd appreciate it if anyone could get me a direction to go in...

2. I'm a little confused with what you want to graph? All you said was:
Basically we have to ask the user for the file the data is storred in and compile a graph from it like so:
And then you have an example graph but don't explain what is represented by each axis. Is the x axis trip duration? Or is it revolutions per second for a given trip length? Is the y axis amount of petrol used?

Btw, am I the only one thinking that gallons is a bit of a silly unit of volume to measure 'amount of petrol consumed per revolution'? Wouldn't it make more sense to use a smaller unit of volume for this problem. Oh well, never mind.

You could then try doing something like this:
Code:
```do
{
ret = fscanf(ifp, "%f %f", &value1, &value2);
/* Maybe store value1 and value2 into an array for later processing */
} while( ret != EOF );```
Until I know more about the graph you are trying to print (i.e. which variable against which variable), it is hard to give you much more advice.

Edit:

In a given sample file will there be more than one data set?

3. The x axis is the five minute intervals based on the amount of time the trip lasts, and the y axis is the mpg you get for that interval.

Here's a link to a test file:
https://webcourses.ucf.edu/webct/urw.../7724986325101

4. You must have a classmate who posted here:
http://cboard.cprogramming.com/c-pro...tml#post978448

Since he never got around to stating fully what the data was, I can't say if his math was correct, or not.

In any case, I would change your FILE *ifp to the more conventional *fp. if(p) is a common syntax in C, and having a file pointer that starts with an i is just confusing. Must have been in the prof's notes, because he had the same odd name for it.

Drawing the graph is no problem. Fill the array with your calculated data, and it's the logic shown in that thread, is (maybe), good to go. I don't want to say that it's been tested, because it has not been, AFAIK.

Code:
```Date from milesPG[]:
10.033465
32.441399
28.829947
===================

45|
40|
35|
30|       *****
25|       ***** *****
20|       ***** *****
15|       ***** *****
10| ***** ***** *****
5| ***** ***** *****
0| ***** ***** *****
-----------------------------------
00-05 05-10 10-15 15-20 20-25 25-30
Press any key to continue . . .```
He limited his scale to 45, but there's no reason not to extend it higher, in your version.

Code:
```#include <stdio.h>

int main() {

FILE *ifp;
double revs, gas;

printf("What file stores the car data?\n");
scanf("%d", &file);

//Open file
ifp = fopen(file, "r");

}```
I would: delete "int file", that should be your char array for the filename.
add: char file[40]; double alltheBloodyGasUsedSoFar; int interval;
Code:
```while("endless loop here") {
n=fscanf(fp, "%f", "%f", interval, gas);
if(n>1) {
alltheBloodyGasUsedSoFar += gas;

}
else  //end of file has been reached, n=EOF(usually -1)
break;
}```
fclose(fp); just above the last getchar() of pause line.
getchar() or system("pause"); to just before the,
return 0; at the very end of main.

check if the file was opened OK or not (is fp==NULL). If so, print an error message and return 1, ending the program.

change: scanf("%d", &file); to scanf("%s", file); //(no ampersand)
ifp to fp as mentioned earlier

That should get you going nicely.

Be careful that this is your own work - simple copying with a tweak here or there would be plagerism.