0

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!

3
  • 1
    Please make a sscce Answer. There's too much code here. sscce.org Commented Jun 12, 2014 at 12:36
  • 3
    If you want to store double elements why are you declaring array1 as char * and not double *? Commented Jun 12, 2014 at 12:38
  • when I declare array1 with double *array1; And try to read with array1[lin]=ptr; I get a strange number as well (-134258688)?! Don't know where this comes from Commented Jun 12, 2014 at 14:02

1 Answer 1

1

On this line:

array1[lin] = ptr;

The compiler kind of tells it all: array1[lin] is a char and ptr is a char*.

Sign up to request clarification or add additional context in comments.

3 Comments

okay, thank you. and how do I solve this? I thought I declared array1 as array of char* ?!
char* array1[MAX_ELEMENTS] or char** array1 and them malloc.
Oh I just found out - unfortunately it did not work all good. When I print out e.g. array1[lin] in the moment I assign it to ptr then it works -BUT when I than do a for loop and want to print out everything that is in array[1] with for(i=0,i<lins,i++) printf("%s",array[i]); then nothing is printed out (just an empty line?!)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.