2

My program is supposed to differenciate given measurements into cycles and then it to a file.

The input file looks like this:

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
...

What it should look like in the end:

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
...

So puts all first measurements of the cycles into the first line, the second measurements in the second line and so on.

The Problem I now have is that the velocity increases with every fourth cylce and so the measurements per cycle decrease. So when saving it to the file it gives me a "Segmentation fault", propably because the struct is empty.
How can I check if a struct is empty or full? Or are there other mistakes in my code?

#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;
}

Hope my code makes sense and there aren't too many other mistakes in it

jdi
  • 39
  • 1
  • 4
  • 4
    Don't use your own null-pointer variant, use `NULL` like it is supposed to be used. – Some programmer dude Jan 11 '17 at 11:05
  • 2
    I also recommend you read [this discussion about casting the result from `malloc`](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). – Some programmer dude Jan 11 '17 at 11:06
  • 3
    As for your problem, probably a null or uninitialized pointer. Use a debugger to find out *where* the crash happens, and what the values of all involved variables are. – Some programmer dude Jan 11 '17 at 11:07
  • If you're setting data longer down the road from the allocation, I always find it worth time `memset`-ing the struct, to avoid undefined behaviour that might happen from uninitialized values within the struct if you happen to use them without setting their values. – Ludricio Jan 11 '17 at 11:10
  • @LudvigRydahl: `memset` is only useful for integers, because those are guaranteed to have an encoding od all-bits-zero for `0`. Floating point and pointers are not. – too honest for this site Jan 11 '17 at 12:37

0 Answers0