0

I use VS 2010 as my IDE and this code works fine until the line where fgets is called as a puts argument. It writes down the numbers in the file fine but it also prints some annoying gibberish. Maybe I am missing a \0 somewhere, dunno. Other people tried it on other compilers like mingw or gcc and it works fine.

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int *a, n, i;
    char str[512];
    FILE *f;
    printf("Insert array size: ");
    scanf("%d", &n);
    if(n <= 0)
    {
        printf("%d is not an allowed value!\n", n);
        return 1;
    }
    a = (int*)malloc(n * sizeof(int));
    if(a == NULL)
        return 2;
    putchar('\n');
    f = fopen("myarray.txt", "r+");
    if(f == NULL)
        return 3;
    for(i = 0; i < n; ++i)
    {
        printf("Insert %d. element of the array: ", i + 1);
        scanf("%d", &a[i]);
        fprintf(f, "%d ", a[i]);
    }
    putchar('\n');
    puts(fgets(str, 512, f));
    free(a);
    fclose(f);
    return 0;
}
Venom
  • 1,107
  • 4
  • 21
  • 46
  • [Don't cast the result of malloc in C](http://stackoverflow.com/q/605845/1202636). – effeffe Dec 22 '12 at 00:04
  • I thought type conversion in c is explicit? Is it not so? – Venom Dec 22 '12 at 00:13
  • Have you read the accepted answer of the linked question? – effeffe Dec 22 '12 at 00:26
  • So this is valid only when I am casting a void *, otherwise it's fine? Just realized I mixed it up with C++, damn. – Venom Dec 22 '12 at 00:34
  • Almost, conversion to _and from_ `void *` to any object pointer is implicit and safe, otherwise you have to add a cast (and pay attention to do a safe cast). – effeffe Dec 22 '12 at 00:37

1 Answers1

0

At the point in your code where you call fgets your file pointer should be at the end of the file.

Thus, fgets should be returning you the NULL pointer, since it hits EOF before reading any characters.

Thus, you're passing the NULL pointer to puts.

Dancrumb
  • 26,597
  • 10
  • 74
  • 130
  • So rewind(f) should resolve it I guess if I call it before fgets? – Venom Dec 22 '12 at 00:07
  • Really, you should probably check `ferror` after `fgets` and before `puts` or you could end up with a really hard to debug problems. – Dancrumb Dec 22 '12 at 00:08
  • It's just a school assignment so it is not necessary imho. It seems that rewind is working, thank you. – Venom Dec 22 '12 at 00:10
  • Not enough reputation of my own, sorry. I need 15 rep points. Or are you talking about the yes/no question at the bottom? Excuse me, I am new around here. – Venom Dec 22 '12 at 00:17
  • You only need 15 rep points to upvote something. To accept it, you just click on the check mark that appears under the voting arrows – Dancrumb Dec 22 '12 at 00:26
  • Just did, sorry it took me so long. – Venom Dec 22 '12 at 00:32
  • Even if dereferencing a `NULL` pointer is undefined behavior and everything could happen, isn't it strange that the program doesn't crash? Doing this resulted in some kind of memory error on almost every system I tried since the null pointer usually points to an area that is always invalid. – effeffe Dec 22 '12 at 00:41
  • Why would the area be invalid - you mean not allocated or already in use by an other process? That would imply a run-time access violation I guess, but it just didn't happen, not once. – Venom Dec 22 '12 at 00:54