我的程序应该将给定的测量值分解为循环,然后将其分解为文件。
输入文件如下所示:
reading | force | displacement | velocity
1 | 64.69 | 49.67 | 0.002
2 | 64.54 | 49.66 | 0.002
3 | 64.39 | 49.66 | 0.003
...
最终应该是什么样子:
reading1 | force1 | displacement1 | velocity1 | reading2 | force2 | ...
1 | 64.69 | 49.67 | 0.002 | 871 | -15.13 | 33.47 | 0.019
2 | 64.54 | 49.66 | 0.002 | 872 | -15.74 | 33.44 | 0.019
3 | 64.39 | 49.66 | 0.003 | 873 | -17.75 | 33.41 | 0.02
...
因此,将所有周期的第一次测量放入第一行,第二次测量放在第二行,依此类推。
我现在面临的问题是速度会随着每四个周期而增加,因此每个周期的测量结果会减少。
因此,当将其保存到文件时,它会给我一个“分段错误”,因为结构是空的。
如何检查结构是空的还是满的?或者我的代码中是否还有其他错误?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NIL (struct zyklus *)0
struct zyklus {
double reading;
double force;
double displacement;
double velocity;
struct zyklus * p_next;
};
//structured list on struct zyklus
struct zyklus * newElem(double read, double forc, double displace, double velocit){
struct zyklus * p_help;
p_help=(struct zyklus *)malloc(sizeof(struct zyklus));
p_help->reading=read;
p_help->force=forc;
p_help->displacement=displace;
p_help->velocity=velocit;
p_help->p_next=NIL;
return p_help;
}
int main(){
//set pointers on structure
struct zyklus * p_start;
struct zyklus * p_act;
struct zyklus * p_act_array[28];
//Open Input and output files and initialise some variables
FILE *testdata;
FILE *speichern=fopen("output.txt","w");
testdata=fopen("TESTDATA.txt","rt");
char line[40];
double summe;
double zwischenarray[20]={0};
char *token;
int j=0,i=0,k=0,l=0,anzahl=0;
double zyklenanfang[40]={0};
//Go through measurements by line, seperate the data by the comma, write it into "zwischenarray" and save it in the structured list. Also remember the Beginning of the different cycles in "zyklenanfang"
while (fgets(line, sizeof(line),testdata)){
i=0;
token=strtok(line,",");
while(token!=NULL){
if(atof(token)!=0){
zwischenarray[i]=atof(token);
}
token=strtok(NULL,",");
i++;
}
if(j==0 && zwischenarray[3]>0){
p_start=p_act=newElem(zwischenarray[0],zwischenarray[1],zwischenarray[2],zwischenarray[3]);
j=1;
}else if(zwischenarray[3]>0){
if(k==0){
k=1;
zyklenanfang[l]=zwischenarray[0];
l++;
}
p_act->p_next=newElem(zwischenarray[0],zwischenarray[1],zwischenarray[2],zwischenarray[3]);
p_act=p_act->p_next;
}else{
k=0;
}
}
/*Output*/
int f=0;
//All pointers in array are set to a start pointer
while (f<=28){
p_act_array[f]=p_start;
printf("Array NR. %i Anfang: %lf",f, zyklenanfang[f]);
f++;
}
//All pointers are increased until they are in the beginning of the next cycle
int m=0;
while (m<28){
while(p_act_array[m]->reading<zyklenanfang[m]){
p_act_array[m]=p_act_array[m]->p_next;
}
m++;
}
int n=0;
int q=0;
printf("\noutside");
//Save while in first cycle
while((p_act_array[0]->reading)<zyklenanfang[1]){
printf("gehthalbrein\n");
printf("gehtrein\n");
if (p_act_array[n]!=NULL){//Trying to filter for empty struct(doesn't work)
fprintf(speichern,"%lf,%lf,%lf,",p_act_array[n]->reading,p_act_array[n]->displacement,p_act_array[n]->force);
}
n++;//Save first measurement of every cycle
if(n==28){//When every cycle has been saved
n=0;
fprintf(speichern,"\n");//make new line
while (n<=28){
p_act_array[n]=p_act_array[n]->p_next;//and increase every pointer
n++;
}
q++;
n=0;//set n to first pointer again
}
}
printf("\noutside2\n");
return 0;
}
希望我的代码有意义并且其中没有太多其他错误