0
int compare_chars(const void* a, const void* b)
{
    char* c1 = (char*)a;
    char* c2 = (char*)b;

    return *c1 - *c2;
}

int main(int argc, char* argv)
{
    FILE* file = fopen("c:\\file.txt", "r");
    assert(file != NULL);

    char word[10] = { 0 };

    while (!feof(file))
    {
        fscanf(file, "%s", &word);

        qsort(word, 10, sizeof(char), &compare_chars);

        for (int i = 0; i < 10; i++)
            printf("%c", word[i]);
        printf("\n");
    }

    fclose(file);
}

I get the following message:

Run-Time Check Failure #2 - Stack around the variable 'word' was corrupted.

It happens on a valid file containing only "0123456789" (10 characters).

Why?

Elimination
  • 2,619
  • 4
  • 22
  • 38
  • 1
    char word[] =calloc(11,sizeof(char)); // 10 + 0 terminator – AnthonyLambert Aug 14 '15 at 14:37
  • Thank you all (2 more to go) – Elimination Aug 14 '15 at 14:40
  • Look, unless you are on a memory-restricted embedded platform, don't use buffers smaller than, say, 128 unless you have a very good reason for it. 'char word[10]' is not justifiable when handling text lines. Stop doing that. – Martin James Aug 14 '15 at 15:02
  • I wonder how many dups there are of this? – Martin James Aug 14 '15 at 15:03
  • this line: 'int main(int argc, char* argv)' is not correct, the second parameter is always a pointer to an array of pointers to char strings. the line can be either: 1) 'int main(int argc, char* argv[])' or 2) 'int main(int argc, char** argv)' Also, because argc and argv are not used, the compiler will output two warning messages about unused parameters. Suggest using: 'int main( void )' – user3629249 Aug 15 '15 at 03:59
  • this line: 'fscanf(file, "%s", &word);' has not length limiter on the input, so any 'word' that longer than 9 (to allow for the string terminator '\0') will result in writing past the end of the input buffer which results in undefined behaviour and can lead to a seg fault event. In C, an array name degrades to the address of the first byte of the array, so do not use a '&' in the word parameter – user3629249 Aug 15 '15 at 04:04
  • the call to qsort() must not include the string terminator character, as it will be sorted into the first char of the array, resulting in the printf() outputting nothing. – user3629249 Aug 15 '15 at 04:07
  • this line: 'while (!feof(file))' will not work as expected, mostly because feof() does not return 'true' until after trying to read past the end of the file. suggest using the call to fscanf() statement in the while() parameter – user3629249 Aug 15 '15 at 04:08
  • 'assert' statements are really for debugging. much better to use: if( NULL == file) { perror( "fopen failed"); exit( EXIT_FAILURE ); } – user3629249 Aug 15 '15 at 04:11

3 Answers3

4

Strings in c are null-terminated, so you need to allocate space for the string terminator.

To fit a 10-character long word you need to allocate 11 bytes:

char word[11] = { 0 };
Klas Lindbäck
  • 33,105
  • 5
  • 57
  • 82
4

In fscanf statement-

  fscanf(file, "%s", &word);
                     ^not needed (remove &)

char word[10] = { 0 };

Strings are null terminated. So increase size of array word to include '\0'.

Also while (!feof(file)) is wrong .

EDIT

Instead you can control loop with fscanf-

while(fscanf(file, "%s", word)==1){
// your code
 }

Because upon successful completion, fscanf shall return the number of successfully matched and assigned input items.

Here in your case fscanf will return 1 on success.

Community
  • 1
  • 1
ameyCU
  • 16,489
  • 2
  • 26
  • 41
3

A 10-character string takes 11 chars to represent (1 extra for the null byte at at the end).

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101