Complementary task for topic: 9
M Nemeth · 2023-08-29 15:21:04.632218'
File Handling: read measurement data
File Handling: read measurement data
There is a measurement file in the following form:
1st line: ID
2nd line: mode
3rd line: channel (1,0,2 form)
4th line: datetime 2024-01-02 21:01:10
5th line: internal parameter
6th line: internal parameter
7th line: number of measurement
8th-?th line: measurement data (5 comma separated tabulated)
?+1th line: 0 (end of one measurement)
There repeats many times
Your task is the collect the channel's first element, and the from all of the data you need the first and third column. You have to save out one table from all of the measurements with 3 columns, 1st is the index/2+input's first column (index is changing by measurement to measurement. 2nd is the input's 1st column. The 3rd is the third column of the input. The output should be ordered according to its second column, when these are equals accoring to the first column! the first two rows should be a name and the number of records in the file.
sample file:
f6388
332
1,0,0
2023-06-22 10:00:21
h3
t654
21
0.5,1.0, 15.50, 52.93,235.21
1.0,5.0, 15.74, 52.93,132.68
1.5,1.0, 15.98, 52.93, 89.79
2.0,1.0, 15.97,127.31,152.98
2.5,1.0, 15.84,120.31,121.38
3.0,1.0, 15.71, 88.31,100.34
3.5,8.0, 15.47,120.59, 84.87
4.0,1.0, 15.02, 14.59, 72.11
5.0,1.0, 14.40,120.59, 55.30
6.0,1.0, 13.24,120.66, 42.39
7.0,1.0, 12.30,120.66, 33.75
8.0,1.0, 11.44,120.66, 27.46
9.0,1.0, 11.08,120.68, 23.65
10.0,1.0, 10.84,120.68, 20.84
11.0,1.0, 10.85,120.68, 18.95
12.0,1.0, 10.82,120.71, 17.33
13.0,1.0, 10.79, 12.71, 15.96
14.5,1.0, 10.93,120.71, 15.01
15.0,1.0, 10.97,120.72, 14.06
16.0,1.0, 11.01,120.72, 13.23
17.0,1.0, 11.32,120.72, 12.09
0
h3243
tz3
2,0,0
2023-06-22 10:00:37
h3
fggr
21
0.5,1.0, 14.06, 47.08,210.88
1.0,1.0, 16.07, 47.08,120.51
1.5,1.0, 16.92, 47.08, 84.55
2.0,1.0, 17.67,108.31,152.41
2.5,1.0, 17.12,108.31,118.09
3.0,1.0, 17.02,108.31, 97.88
3.5,1.0, 16.47,108.46, 81.25
4.0,1.0, 16.16,108.46, 69.78
5.0,1.0, 14.75,108.46, 50.93
6.0,1.0, 13.34,108.48, 38.40
7.0,1.0, 12.06,108.48, 29.76
8.0,1.0, 11.15,108.48, 24.08
9.0,1.0, 10.49,108.57, 20.16
10.0,1.0, 10.38,108.57, 17.95
11.0,1.0, 10.38,108.57, 16.31
12.0,1.0, 10.40,108.54, 14.99
13.0,1.0, 10.48,108.54, 13.93
14.0,1.0, 10.53,108.54, 13.00
15.0,1.0, 10.88,108.56, 12.54
16.0,1.0, 11.23,108.56, 12.14
18.0,1.0, 11.56,108.56, 11.11
0
h313
h3
13,0,0
2023-06-22 10:03:45
rep12
due4
21
0.5,1.0, 27.25, 53.65,465.62
1.0,1.0, 29.27, 53.65,250.03
1.5,1.0, 25.91, 53.65,147.53
2.0,1.0, 22.43,120.57,215.31
2.5,1.0, 18.67,120.57,143.40
3.0,1.0, 16.26,120.57,104.04
3.5,1.0, 13.65,121.06, 75.20
4.0,1.0, 12.62,121.06, 60.81
5.0,1.0, 11.30,121.06, 43.58
6.0,1.0, 10.65,121.22, 34.27
7.0,1.0, 10.50,121.22, 28.95
8.0,1.0, 10.49,121.22, 25.31
9.0,1.0, 10.61,121.25, 22.76
10.0,1.0, 10.79,121.25, 20.84
11.0,1.0, 11.03,121.25, 19.36
12.0,1.0, 11.24,121.06, 18.05
13.0,1.0, 11.42,121.06, 16.94
14.0,1.0, 11.53,121.06, 15.87
15.0,1.0, 11.59,121.10, 14.89
16.0,1.0, 11.64,121.10, 14.03
18.0,1.0, 11.68,121.10, 12.51
0
R3177
rep1
177,0,0
2023-06-22 10:50:57
S3
ut3
3
0.5,1.0, 42.46, 29.27,395.84
1.0,1.0, 34.64, 29.27,161.47
1.5,1.0, 31.25, 29.27, 97.11
0
Make sure there is no lines after the last 0!!!
Hint: This is an absolutely everyday problem for engineers and scientist. To solve it may takes hours!
1st you have to think over what kind of structurce you can use to hold the data
2nd you have to think over how to create an output data structure
3rd think over how to order the data
4th print out and clear the memory.
The following solution is just one direction. There are better solutions as well. You can think what if we create the output format when we read the data. Yes, that would make things much easier, but in real life situation the programmer have to think over the reusability of the code. if we separate the data collection and the process, we can simply modify this program when we need to do other things with the same data structure! (e.g. other mode). Here we arrive to modular programming...
Solution
First you should fill up the functions, if is do not help, see the explanation for the full program:
typedef struct{
int ID;
int size;
double* AM;
double* RO;
}data;
typedef struct{
int size;
data* dat;
}measure;
typedef struct{
char* name[100];
int size;
double* x;
double* AM;
double* RO;
}output;
measure read_from_file(measure h3);
output create_output(measure h3);
void write_to_file(output out);
void Delete(output out, measure h3);
int main()
{
measure H3;
H3.dat=NULL;
H3.size=0;
output out;
H3=read_from_file(H3);
out=create_output(H3);
write_to_file(out);
Delete(out,H3);
return 0;
}
Explanation
#include#include typedef struct{ int ID; int size; double* AM; double* RO; }data; typedef struct{ int size; data* dat; }measure; typedef struct{ char* name[100]; int size; double* x; double* AM; double* RO; }output; measure read_from_file(measure h3){ FILE* fp=fopen("h3.txt","r"); int n1=10;//to step in the loop char tmp[500];//for read dummy lines while(!feof(fp)){ fscanf(fp,"%s",tmp);//dummy line printf("%s\n",tmp); fscanf(fp,"%s",tmp);//dummy line printf("%s\n",tmp); double t1,t2,t4;//dummy variables int ID,a2,t3;//ID and dummy variables fscanf(fp,"%d,%d,%d",&ID,&t3,&a2);//if we do not read them, we have to get rid of the characters somehow printf("%d,%d,%d\n",ID,t3,a2); h3.dat=(data*)realloc(h3.dat,(h3.size+1)*sizeof(data));//create space for one more h3.dat[h3.size].ID=ID;//the new has the ID fscanf(fp,"%s",tmp); //for DATE 1st part printf("%s",tmp); fscanf(fp,"%s",tmp);//DATE 2nd part printf("%s\n",tmp); fscanf(fp,"%s",tmp); printf("%s\n",tmp); fscanf(fp,"%s",tmp); printf("%s\n",tmp); fscanf(fp,"%d",&n1);//read the number of meas. lines printf("%d\n",n1); h3.dat[h3.size].size=n1; h3.dat[h3.size].AM=(double*)malloc(n1*sizeof(double)); h3.dat[h3.size].RO=(double*)malloc(n1*sizeof(double)); for(int i=0; i