I want to read a file line by line. Each line consists of 504 entries. I want to store from each line the first column in an array array1 the second in an array array2 and the third one in an array called array3. The last 500 entries I want to store in an 2D array array4.
For now I tried to do this with pointers but I have trouble to store them right.
vrFile = fopen("file.txt","r");
int size = 1024, pos;
int c, cdbz;
char *buffervr = (char *)malloc(size);
char *bufferdbz = (char *)malloc(size);
int lin=0;
char *array1, *array2, *array3, **array4;
array1=(char *) malloc(5000*sizeof(double));
array2=(char *) malloc(5000*sizeof(double));
array3=(char *) malloc(5000*sizeof(double));
array4 = malloc(5000 * sizeof(double *));
int i;
for(i = 0; i < 5000; i++) {
array4[i] = malloc(nbins * sizeof(double));
}
tdbz = malloc(5000 * sizeof(double *));
for(i = 0; i < 5000; i++) {
tdbz[i] = malloc(500 * sizeof(double));
}
if(vrFile!=NULL) {
do { // read all lines in file
pos = 0;
do{ // read one line
c = fgetc(vrFile);
if(c != EOF) buffervr[pos++] = (char)c;
if(pos >= size - 1) { // increase buffer length - leave room for 0
size *=2;
buffervr = (char*)realloc(buffervr, size);
}
}while(c != EOF && c != '\n');
buffervr[pos] = 0;
// line is now in buffer
//printf("%s",buffervr);
char *ptr;
ptr = strtok(buffervr," \t");
int abs=1;
while(ptr != NULL) {
if(abs==1){
array1[lin] = ptr;
}
else if(abs==2) {
array2[lin] = atof(ptr);
}
else if (abs==3) {
array3[lin] = atof(ptr);
}
else {
array4[lin][abs-4]=atof(ptr);
}
abs++;
ptr = strtok(NULL, " \t");
}
lin++;
} while(c != EOF);
fclose(vrFile);
}
free(buffervr);
My Problem is, that when I print out ptr with printf("%s\n",ptr) I get the right number, e.g. the first entry from the first line is displayed (0.52312) . But when I try assign array1[lin] to ptr I get an warning:
"assingnment makes integer from pointer without a cast"
printf("%i\n",array1[lin])
results in: "96"
Has someone an idea why this happens and how I can manage to read and save the floating-point entries of each line in different arrays?
Thank you!
doubleelements why are you declaringarray1aschar *and notdouble *?