Hi, I have written a function called read_input and it is for writing a table of data from a file and categorizing and arranging them into an array of structs.. A sample of 1 line of the data is shown below:
Quote:
1.1.1.1.2 = 1.2372e-05 23 4
The struct is written as:
What I am intended to put into one of the index of the array is :Quote:
struct database_input {
int category_A;
int category_B;
double category_C;
int category_D;
};
My full written code is shown below:Quote:
category_A = 11112
category_B = 23
category_C = 0.000012372
category_D = 4
Like what I had commented inside the code, I had a segmentation fault error when the code was run. This error was found (and has been later confirmed by a debugger) to be the fault of the strcat line, which I have been commented above. Can anybody please tell me how to fix this problem and tell me about other problems found in this program?Code:void read_input( FILE *ifp, struct database_input *data)
{
int i=0;
char number = 0;
char *goto_A = NULL;
char *category_C_f = NULL;
double goto_C;
char *goto_B;
while( i<99999 && feof(ifp) == 0)
{
while( number != ' ')
{
fscanf( ifp, &number);
if (number != '.')
strcat(goto_A, &number); //all of the strcat are making the segment faultation
}
data[i].category_A = (int)*goto_A;
ifp = ifp+2;
number = 0;
while( number != 'e')
{
fscanf( ifp,&number);
strcat(category_C_f, &number);
}
goto_C = (double)*category_C_f;
data[i].category_C = goto_C/10000;
ifp = ifp+5;
number = 0;
while( number != ' ')
{
fscanf( ifp, &number);
if (number != '.')
strcat(goto_B, &number);
}
data[i].category_B = (int)*goto_B;
ifp = ifp+1;
number = 0;
fscanf( ifp, &number);
data[i].category_D = (int)number;
number = 0;
while( number != '\n')
{
ifp++;
}
i++;
}
}
Thanks