4

I have the weirdest thing happening, and I'm not quite sure why it's happening. Basically what I need to do is use fgetc to get the contents of a simple ASCII file byte by byte. The weird part is it worked, but then I added a few more characters and all of a sudden it added a newline that wasn't there and read past the end of the file or something. Literally all I did was

do {
    temp = (char*) checked_realloc (temp, n+1);
    e = fgetc(get_next_byte_argument);
    temp[n] = e;
    if (e != EOF)
      n++;
 }
while (e != EOF);

And then to check I just printed each character out

temp_size = strlen(temp)-1;
for(debug_k = 0; debug_k < temp_size; debug_k++){
  printf("%c", temp[debug_k]);
}

And it outputs everything correctly except it added an extra newline that wasn't in the file. Before that, I had

temp_size = strlen(temp);

But then it ended on some unknown byte (that printed gibberish). I tried strlen(temp)-2 just in case and it worked for that particular file, but then I added an extra "a" to the end and it broke again.

I'm honestly stumped. I have no idea why it's doing this.

EDIT: checked_realloc is just realloc but with a quick check to make sure I'm not out of memory. I realize this is not the most efficient way to do this, but I'm more worried about why I seem to be magically reading in extra bytes.

user1777900
  • 975
  • 3
  • 13
  • 27
  • In order for `strlen(temp)` to work, there should be a terminating NUL character after the file content. You didn't show the code which adds this final `\0` -- do you have it? – Anton Kovalenko Jan 18 '13 at 07:31
  • if checked_realloc() is just realloc, try to initialize the memory with 0 before trying to read. – Aniket Inge Jan 18 '13 at 07:38
  • Oooooh...duh. I completely forgot about that. Thanks! By the way, since the very last character for this is EOF, should NULL replace that or just come after? I'll probably figure out through trial and error, but I might as well ask – user1777900 Jan 18 '13 at 07:44
  • @user1777900 replace the `EOF` with NULL. I edited my answer to fix this. – kmkaplan Jan 18 '13 at 07:51
  • `EOF` is not a character -- `EOF` is an integer returned by fgetc when you've reached the end of the file. – Chris Dodd Jun 03 '14 at 21:00
  • also make sure that `e` has type `int` (not `char`). – M.M Jun 03 '14 at 22:46

2 Answers2

1

A safer way to write such operation is:

  1. memset the memory bulk before use with zeros, if you are allocating memory prior to realloc.And every time you realloc, initialize it to zero.
  2. If you are using a memory to access strings or use string functions on that memory always ensure you are terminating that memory with a NULL byte.

do{
    temp = (char*) checked_realloc (temp, n+1);//I guess you are starting n with 0? 
    temp[n]=0;
    e = fgetc(get_next_byte_argument);
    temp[n] = e;
    if (e != EOF)
        n++;
} while (e != EOF);
temp[n]=0;
n=0;

I guess the above code change should fix your issue. You don't need strlen -1 anymore. :)

Cheers.

Ryan Haining
  • 35,360
  • 15
  • 114
  • 174
askmish
  • 6,464
  • 23
  • 42
  • That seems to have done it! There's oddly still a newline that wasn't in my file, but it's consistent. There's not maybe newline maybe gibberish being printed. – user1777900 Jan 18 '13 at 07:52
  • Nope. OP is initing e with fgetc and storing temp[n] with the value of e. If OP gets EOF then n++ wouldn't happen and temp[n] will still contain EOF, before reaching the while condition.After it exits tem[n] will contain EOF always(unless loop exits by some other cause) and temp[n]=0 after the while loop will safely overwrite the EOF contained in temp[n]. :) – askmish Jan 18 '13 at 07:53
0

It sounds like you forgot to null terminate your string. Add temp[n] = 0; just after the while.

kmkaplan
  • 18,655
  • 4
  • 51
  • 65