0

I'm writing a program in C where I need to read a file in and print out every string that is at least 4 characters long. I am having a problem allocating memory to use. Strings can arbitrarily long. I am trying to malloc a buffer to the size of the file, and then free it at the end but I am clearly missing something important. Here is my code:

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

int main(int argc, char **argv) {
  FILE *f = fopen(argv[1], "r");
  char *buffer = malloc(8 * sizeof(f));
  char ch;
  int i = 0;

  while(fread(&ch, 1, 1, f) != 0) {
    if((ch >= 32 && ch <= 126) || ch == 9) {
      buffer[i++] = ch;
    }
    else {
      buffer[i] = '\0';
      if (i >= 4) {
        printf("%s\n", buffer);
      }
      i = 0;
    }
  }

  free(buffer);
  fclose(f);
  return 0;
}

Running this with a file correctly outputs all strings (1 per line) and then gives me this error message.

*** glibc detected *** ./mystrings: free(): invalid next size (fast): 0x0000000000601250 ***
======= Backtrace: =========
/lib64/libc.so.6[0x34ff676166]
/lib64/libc.so.6[0x34ff678ca3]
./mystrings[0x4006e9]
/lib64/libc.so.6(__libc_start_main+0xfd)[0x34ff61ed1d]
./mystrings[0x400569]
======= Memory map: ========
00400000-00401000 r-xp 00000000 00:1a 94896226                           /afs/pitt.edu/home/a/s/asm122/private/cs449/project2/mystrings
00600000-00601000 rw-p 00000000 00:1a 94896226                           /afs/pitt.edu/home/a/s/asm122/private/cs449/project2/mystrings
00601000-00622000 rw-p 00000000 00:00 0                                  [heap]
34fee00000-34fee20000 r-xp 00000000 fd:00 127                            /lib64/ld-2.12.so
34ff01f000-34ff020000 r--p 0001f000 fd:00 127                            /lib64/ld-2.12.so
34ff020000-34ff021000 rw-p 00020000 fd:00 127                            /lib64/ld-2.12.so
34ff021000-34ff022000 rw-p 00000000 00:00 0 
34ff600000-34ff78b000 r-xp 00000000 fd:00 131                            /lib64/libc-2.12.so
34ff78b000-34ff98a000 ---p 0018b000 fd:00 131                            /lib64/libc-2.12.so
34ff98a000-34ff98e000 r--p 0018a000 fd:00 131                            /lib64/libc-2.12.so
34ff98e000-34ff98f000 rw-p 0018e000 fd:00 131                            /lib64/libc-2.12.so
34ff98f000-34ff994000 rw-p 00000000 00:00 0 
3505a00000-3505a16000 r-xp 00000000 fd:00 609                            /lib64/libgcc_s-4.4.7-20120601.so.1
3505a16000-3505c15000 ---p 00016000 fd:00 609                            /lib64/libgcc_s-4.4.7-20120601.so.1
3505c15000-3505c16000 rw-p 00015000 fd:00 609                            /lib64/libgcc_s-4.4.7-20120601.so.1
7ffff7fd3000-7ffff7fd6000 rw-p 00000000 00:00 0 
7ffff7ffa000-7ffff7ffe000 rw-p 00000000 00:00 0 
7ffff7ffe000-7ffff7fff000 r-xp 00000000 00:00 0                          [vdso]
7ffffffea000-7ffffffff000 rw-p 00000000 00:00 0                          [stack]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
Aborted (core dumped)

I'm sure it's a stupid mistake, if anyone has any ideas how to solve this please let me know. Thank you!

1 Answers1

0

sizeof(f) returns the size of the f pointer, not the size of the file.

You can read about how to get the size of a file here: How can I get a file's size in C?

However, a better approach might be to use fgets: https://www.tutorialspoint.com/c_standard_library/c_function_fgets.htm

You can allocate a buffer of arbitrary size, say 256 bytes. Then try to read a line up to 256 bytes long. If you got less than 256 - you got a while line. Otherwise - increase the size of the buffer and try again.

Community
  • 1
  • 1
obe
  • 7,378
  • 5
  • 31
  • 40