Reading char by char from an input file in C?

2019-09-05 22:15发布

I am trying to read a file and then read each character until i reach a new line the do some work on that line. Here is what i have done so far:

  char line[] = "";
  char *charcter = "";

  //If i do this here it works fine, but below it's not working at all
  charcter = "asssss";
  strcat(line,charcter);

  //Read file
  inputFile = fopen(fileName,"r");
  if (inputFile) 
  {
    while ((charcter = (char)getc(inputFile)) != EOF)
           strcat(line,charcter); //This piece code keeps crashing my program on run

    fclose(inputFile);
  }

I am a C# developer and i am really frustrated that i can't figure out this thing, please help.

Edit:

I have modified the piece line and allocated memory to it like this:

char *line = (char*)malloc( 400 *sizeof(char));

now the strcat in the while loop works but crashes after taking all the values (the input is much smaller than the allocated memory), and if i put the same strcat statment inside an if statment it will take the first letter then crash.

So what is the problem here ?

标签: c strcat
2条回答
闹够了就滚
2楼-- · 2019-09-05 22:57

You're trying to modify a string in an array on the the stack. You allocate line[] as "" (i.e. one byte, the null character) and then try to scribble over the memory with strcat (which will modify the memory pointed to by its first argument).

When you put a string into the text of your source and assign it to an array variable in a function, that much memory is allocated on your stack in the function's call-frame. If you access memory after it, you're scribbling over other bits of your stack, which can modify not only the data but the behaviour of your program.

It's like creating a fixed array in C# and then trying to append to it. The language won't let you do it, because that would involve writing over any other memory that happens after the end of the array. C will let you try, but then crash because you're scribbling over memory you don't own, or worse, let you do it and then continue running in an unknown state. Who knows what else is found a few bytes along from the address of line? You don't.

See this question for more info: String literals: Where do they go?

Once you have a pointer to a memory address, C only knows that it's a pointer. It doesn't know what kind of memory it points to (static constants, stack, heap), so it will let you make this mistake. When you do make the mistake, the operating system might say "actually no, that's not allowed" by raising a segmentation fault, or it might not if it's a more subtle bug.

You should allocate a big buffer with malloc, enough to fit the file (dangerous) or re-allocate the buffer to expand it each time.

EDIT I realised that line is a char[] not a char*, so edited my answer.

查看更多
Deceive 欺骗
3楼-- · 2019-09-05 23:13
  • You cannot strcat() a character - you need a string
  • The type of character variable should be int (check the prototype for getc())
  • If you still want to do it character by character, consider adding it to a string by index (and NUL-terminate at the end)
查看更多
登录 后发表回答