1. ## A file reading/relativity problem

Hi, first post so please be gentle! basically I'm trying to write a code to do some relativity calculations (yes it's homework, I'm not gonna lie, but i'm stuck) using data from a file. Basically here's the data file:

# Event ID t1 (ns) t2 (ns) E (MeV)
Event: 1 t1 = 91199.24779 t2 = 91209.25718 E = 3087.557027
Event: 2 t1 = 293746.1094 t2 = 293756.1196 E = 91.7881244
Event: 3 t1 = 78354.68253 t2 = 78364.69326 E = 2153.221593
Event: 4 t1 = 141035.6017 t2 = 141045.6123 E = 393.6490052
Event: 5 t1 = 59721.42846 t2 = 59731.43988 E = -155.6911938
Event: 6 t1 = 84348.06297 t2 = 84358.07247 E = 872.6708937
Event: 7 t1 = 30078.92394 t2 = 30088.93519 E = 2082.250327

and here's my code:

Code:
```/* Aarran Shaw 200313078 */

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

int main(void) {
FILE *file;
char c;
int eventid, count=0, negcount=0, count2=0;
double t1, t2, energy, total=0.0, time=0.0, average, timeaverage, v, beta, gamma, timetravelled;

file=fopen("p3data7.txt","r"); /*file is the input file*/
while ((c=fgetc(file)) != EOF) {
if (c=='#') {
while (c != '\n') {
c=fgetc(file);
}

}

else {
ungetc(c,file); /*puts a character back*/

fscanf(file, "Event: %d t1 =  %lf t2 =  %lf E =  %lf \n", &eventid, &t1, &t2, &energy); /*Turns a line of data into variables*/

if (energy>0) {

count++;
total=total+energy; /*Adds up all the positive energies*/
time=time+t2; /*Adds up all the t2 values associated with the positive energies*/

}

else {
negcount++;
}

}

}

if (count>0) {

printf("Number of events with positive Energy ('Good Events') = %d \n",count);

printf("Number of events with negative Energy ('Bad Events') = %d \n",negcount);

average=total/count;

timeaverage=time/count;

timetravelled=t2-t1;

v=3/((t2*pow(10,-9))-t1*pow(10,-9));

beta=v/299792458;

gamma=1/sqrt(1-(beta*beta));

printf("Average Energy = %.2lf MeV \n",average);

printf("Average of time t2 = %.2lf ns \n",timeaverage);

}

else {
printf("No positive Energies in the data file, cannot perform calculations. \n"); /*This ensures that if count=0, then it wont divide by zero to return NaN*/
}

if (eventid=1) {
printf("Velocity=%lf\n",v);
printf("beta=%lf\n",beta);
printf("gamma=%lf\n",gamma);
printf("time=%lfns\n",timetravelled);
printf("t1=%lf t2=%lf\n",t1,t2);
}

fclose(file);

return 0;
}```
For the moment i'm trying to test out my calculations on the first line (where eventid=1), and when that's correct I'll create a function to make it look a little less messy.
Now, I noticed I got the wrong value for gamma, so check all my calculations, hence the if statement at the bottom, and it turns out, that even though i'm asking for the first line (if eventid=1) It's giving me the times t1 and t2 from the final line, hence the velocity v being wrong (gamma should be around 40.5). Could somebody please help me with this? Is the if statement in the wrong place or something?
Any help will be much appreciated, thanks.

2. you have to put that part
Code:
```	if (eventid=1) {
printf("Velocity=%lf\n",v);
printf("beta=%lf\n",beta);
printf("gamma=%lf\n",gamma);
printf("time=%lfns\n",timetravelled);
printf("t1=%lf t2=%lf\n",t1,t2);
}```
inside the while loop.
BTW you propably want

Code:
`	if (eventid==1) {`
Kurt

3. Thank you very much my friend, I had to move my calculations into that if statement within the while loop as well, but now I'll clean up with a few functions etc etc.
On a side note, now that i have an extra if statement in the while loop, will i have to put an else in there? Or because i'm only testing with one line of the data should I just leave it?

Aarran.

4. Sorry. After looking more closeley at your code I realized that this if statement doesn't make much sense inside the while loop because you're doing the actual calculations after that loop.
If you want to be able to access all the data from the file after reading you will have to store the data in an array of structs
e.g.
Code:
```struct raw_data {
int eventid;
double t1;
double t2;
double energy;
} data[10];```
Kurt