from my main program to a function. I will first describe the problem then put my code after it. Thanks in Advance for your help!!
Problem Description:
I use sscanf to get data from each line I read using fgets (158 lines total). Each line has 5 fields separated by white space. A typical line looks like:
1 0 0 1 XZ_632 (This would be the first line in the file)
.
.
.
158 0 3866 1 YY_314 (This would be the last line in the file)
The problem is that the last (5th) field that I get from each of the 158 lines using sscanf is not getting passed properly from main to the function. When I look at all 158 array elements inside the function for the 5th value it's always the last value in the last line. In this case it would be element 5 from line number 158 (YY_314). It should be the last element from eah line. In other words I get "YY_314" all 158 times.
Here is my code:
#include <ctype.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "train.h"
#define MAXCHARS 80
int i;
int ip;
int lp;
int m;
int n;
int np;
FILE *infp;
char line1[MAXCHARS];
char t1[80], t2[80], t3[80], t4[80],
t5[80];
int main()
{
struct CAR_STRUCT pCar;
infp = fopen("Data.txt", "r");
if(infp == NULL) {
fprintf(stderr, "can't open %s\n", "Data.txt");
exit(EXIT_FAILURE);
}
for (m=1; m<159; m++) {
if(fgets(line1, MAXCHARS, infp) != NULL) {
sscanf (line1, "%s %s %s %s %s",
t1, t2, t3, t4, t5);
pCar.Index = atoi(t1);
pCar.Matrix_Pos[m][1] = atoi(t2);
pCar.Matrix_Pos[m][2] = atoi(t3);
pCar.Matrix_Pos[m][3] = atoi(t4);
pCar.Track[m] = t5;
}
}
// Done reading input file at this point
fclose(infp);
Load_Matrices(&pCar);
return(0);
}
void Load_Matrices(CARTYPE *pCar)
{
for (m=1; m<159; m++){
printf(" Track_Id = %s\n", pCar->Track[m]);
}
}
<****HEADER FILE****>
typedef struct CAR_STRUCT {
int Index;
int Track[158];
int *pCar;
int Matrix_Pos[158][11];
} CARTYPE;
void Load_Matrices(CARTYPE *pCar);



LinkBack URL
About LinkBacks




