-1

I have char str[5000]; that has nothing in it.

I then have toCharArray(fp1, str);

void toCharArray(FILE *fp1, char *str) {
 while(!feof(fp1)) {
    fscanf(fp1, "%c", *(str++));
 }

The program crashes with no error messages on the fscanf line. I am attempting to iterate through the array using the *(str++) statement and assigning values from the file to each element. I can go *(str++) = 'G'; *(str++) = 'H'; to my heart's content (and printf("%c", *(--str)); would return the correct values) but I can not use fscanf to seemingly do the same thing.

b1lly
  • 113
  • 4
  • 7
    [while(feof) is always wrong](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – n. m. could be an AI Mar 13 '17 at 07:01
  • 2
    `fscanf` with `%c` wants a pointer as parameter – LPs Mar 13 '17 at 07:02
  • 3
    Your compiler should have warned you about incorrect fscanf arguments. If it didn't, crank up your warning level. If it did, go back and read the warning, and set up your compiler such that it treats warnings as errors. – n. m. could be an AI Mar 13 '17 at 07:03
  • 3
    `while(EOF!=fscanf(fp1, "%c", str)) { str++; } *str = 0;` – BLUEPIXY Mar 13 '17 at 07:06
  • 4
    5000 is high, but it could be not high enough. It would be better to pass that value as a parameter to the function and then read only that number of chars (minus one) at max. – Bob__ Mar 13 '17 at 07:08
  • 1
    Show how you call `toCharArray` and show the declaration of the variables you pass to `toCharArray`. There may be also problems _there_. – Jabberwocky Mar 13 '17 at 07:51
  • @b1lly Do you want to extract the hole content of your file on a string ? – P.A Mar 13 '17 at 09:03
  • 1
    Why not just call fread? – stark Mar 13 '17 at 11:23
  • Do Not use `feof()` to control a loop. It does not do what so many seem to think it does. – user3629249 Mar 15 '17 at 12:38
  • when calling `fscanf()`, always check the returned value to assure the operation was successful. Note any returned value other than the count of input/comversion specifiers in the format parameter is an error, In the current scenario, an returned value other than 1 is an error – user3629249 Mar 15 '17 at 12:40

1 Answers1

1

You have to consider few points in code

  • Error check the arguments.
  • In fscanf(), "%c needs a pointer to character.
  • while-feof-file-always-wrong.
  • You said your array size is 5000, so while() loop should not run more than that.

    void toCharArray(FILE *fp1, char *str) {
         int i = 0;
         if(fp1 == NULL || str == NULL){
            printf("Invalid arguments..\n");
            return;
         }
         while((i<5000)&&(EOF!=fscanf(fp1, "%c", &str[i]))) {
             i++;
         }
    }
    
Community
  • 1
  • 1
jjm
  • 431
  • 3
  • 19
  • 3
    Although improved, problems: 1) the `i<5000` test is too late, should be first. Suggest `(i<5000) && (fscanf(fp1, "%c", &str[i]) ==1)` 2) the calling code of `toCharArray()` lacks any indication of how much of `str` was populated by the call. Suggest `size_t i; ..... return i;` with function signature change. 3) Checking the arguments is useful, but not a "have to". per similar functions in the C standard library. – chux - Reinstate Monica Mar 13 '17 at 14:34