0

My goal is to read a file, and save each elements in this file into a new array..

rewind(fp); ii = 0; while (!feof(fp)) {
    ii ++;
    fscanf(fp, "%s\n", filename_i);
    fp_i = fopen(filename_i, "r");
    if (fp_i == NULL) {
        fprintf(stderr, "can't open input file %s \n", filename_i);
        exit(1);
    }
    filename_ii[ii] = filename_i;
    printf("%s, %d\n", filename_ii[ii],ii);
    fclose(fp_i);

}


printf("a %s %d\n",filename_ii[9],DataSize[2]);
printf("a %s %d\n",filename_ii[1],DataSize[2]);

In while() function, my output is each elements, but I don't know why the last two printf() returns the same results, i.e, it seems like both filename_ii[1] and filename_ii[9] point the last element in the file. Does anyone have ideas about what's wrong in my code? Thank you~

2 Answers 2

1

You need to use strcpy to copy a string. Change:

filename_ii[ii] = filename_i;             // this just assigns a pointer -
                                          // it doesn't actually copy a string

to:

strcpy(filename_ii[ii], filename_i);      // copy the *contents* of `filename_i`
                                          // to `filename_ii[ii]`

This assumes of course that the filename_ii array has been correctly initialised and is not just an array of dangling char * pointers (not possible to tell from the code as currently posted in the question).

Note that if filename_ii is just an array of uninitialised char * pointers then you can use strdup to handle the memory allocation and copying all in one convenient function call. In which case you would change the line above to:

filename_ii[ii] = strdup(filename_i);     // allocate memory to `filename_ii[ii]` and
                                          // copy the *contents* of `filename_i`
                                          // to `filename_ii[ii]`
Sign up to request clarification or add additional context in comments.

4 Comments

After changing to 'strcpy()', it still doesn't work out. I think maybe I didn't initialize the 'filename_ii' correctly. I used 'char *filename_ii[20]'.
As I said above: This assumes of course that the filename_ii array has been correctly initialised and is not just an array of dangling char * pointers. I've added to my answer above to cover this now.
After Changing to strdup(), it works out. Thank you very much! @Paul R
OK - don't forget to free() the memory when you're done with it.
1

Stop using feof()/fscanf() like that, it's super-brittle and needlessly hard to get right.

Instead:

char line[1024]; /* or whatever makes you feel comfortable */
while(fgets(line, sizeof line, fp) != NULL)
{
  size_t len = strlen(line);
  if(len == 1) /* Ignore blank lines. */
    continue;
  if(line[len - 1] == '\n')
    line[--len] = '\0'; /* Remove linefeed. */
  if(access(line, R_OK) == 0)
    strcpy(filename_ii[ii++], line);
}

This:

  1. Uses fgets() to read in a whole line.
  2. Uses access() to check if the file can open. Note that this kind of checking is always prone to race-conditions.
  3. Uses strcpy() to copy the filename, assuming filename_ii[] is a properly set up array.

4 Comments

Why I get this error "read-only variable is not assignable" for ' line[--len] == '\0';' @unwind
How to set up 'filename_ii[]' properly? I still haven't work out.
@user2760099, see updated code. Should be an assignment (=) not an equality test (==) in that line.
Ah, drop the const when declaring len.

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.