通过阅读炭炭从C中的输入文件? [英] Reading char by char from an input file in C?

查看:106
本文介绍了通过阅读炭炭从C中的输入文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图读取一个文件,然后读取每个字符,直到我达到一个新的线做这一行做一些工作。以下是我迄今所做的:

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);
  }

我是一个C#开发人员,我感到很沮丧,我想不通这件事,请帮助。

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

编辑:

我已经修改那条线,分配的内存这样的:

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

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

现在在while循环作品strcat的,但把所有的值(输入大于分配的内存更小),如果我把同样的strcat的语句内的一个,如果说明书将采取的第一个字母,然后崩溃后崩溃。

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.

那么,现在的问题是?

推荐答案

您正试图在堆栈中的数组修改字符串。你分配行[] (即一个字节,在字符),然后尝试用 strcat的(将修改内存指向它的第一个参数)。

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.

这就像建立在C#中固定数组,然后试图将追加到它。该语言不会让你这么做,因为这将会导致在阵列结束后发生的任何其他存储器的写入。按c让你尝试,但随后崩溃,因为你在涂鸦你不拥有,或者更糟的记忆,让你做到这一点,然后继续处于未知状态下运行。谁知道还有什么是从的地址找到了几个字节一起?你不知道。

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.

有关更多信息,请参见该问题: C字符串文字:哪里他们去?

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

一旦你有一个指向一个内存地址,C只知道这是一个指针。它不知道它指向的(静态常量,栈,堆)是什么样的内存,因此它会让你犯这样的错误。当你犯错误时,操作系统可能会说:其实也没什么,这是不允许的通过提高分段错误,也可能不是,如果它是一个更微妙的错误。

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.

您应该分配与的malloc ,足以适应文件(危险)或重新分配缓冲区每次展开一个大的缓冲区。

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

修改我意识到,的char [] 不是的char * ,所以编辑我的答案。

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

这篇关于通过阅读炭炭从C中的输入文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆