按字符用C读取文件字符 [英] Reading a file character by character in C
问题描述
大家好,我正在写在C BF间preTER和我碰到的读取文件的问题。我曾经为了读取第一个字符串使用scanf函数,但你不能在你的BF code空格或注释。
现在这里是我的。
的char * READFILE(字符*文件名)
{
FILE *文件;
字符* code =的malloc(1000 * sizeof的(炭));
文件= FOPEN(文件名,R);
做
{
* code ++ =(char)的龟etc(文件); }而(!* code = EOF);
返回code;
}
我知道问题出现在我如何将文件到code指针在分配下一个字符,但我只是不知道那是什么。结果
我的指针知识缺乏是这项工作的地步。
跨preTER做工精细,全部采用指针,我只是有一个问题读取文件中的。
(我将只实现读+ - >< [],到文件后,虽然如果任何人有一个很好的办法做到这一点,那将是巨大的,如果你让我知道!)
在此先感谢
有许多事情不对您的code:
的char * READFILE(字符*文件名)
{
FILE *文件;
字符* code =的malloc(1000 * sizeof的(炭));
文件= FOPEN(文件名,R);
做
{
* code ++ =(char)的龟etc(文件); }而(!* code = EOF);
返回code;
}
- 如果是什么文件大于1000字节?
- 您正在增加
code
每次读取一个字符,并返回时间code
返回给调用者(即使在内存块的第一个字节就不再指向,因为它是由归国的malloc
)。 - 您是铸造
龟etc(文件)的结果
到字符
。你需要的结果转换为字符
前检查EOF
。
要维持由 malloc返回原来的指针是很重要的
,以便您可以稍后释放它。如果不顾文件大小,我们可以用下面的仍然实现这一点:
的char * READFILE(字符*文件名)
{
FILE *文件=的fopen(文件名,R);
字符* code;
为size_t N = 0;
INT℃; 如果(文件== NULL)
返回NULL; //无法打开文件 code =的malloc(1000); 而((C =龟etc(文件))!= EOF)
{
code [N +] =(焦炭)C;
} //不要忘记用空字符终止
code [N] ='\\ 0'; 返回code;
}
有不同的系统调用,会给你一个文件的大小;常见的一种是 统计
。
Hey everyone, I'm writing a BF interpreter in C and I've run into a problem reading files. I used to use scanf in order to read the first string, but then you couldn't have spaces or comments in your BF code.
Right now here is what I have.
char *readFile(char *fileName)
{
FILE *file;
char *code = malloc(1000 * sizeof(char));
file = fopen(fileName, "r");
do
{
*code++ = (char)fgetc(file);
} while(*code != EOF);
return code;
}
I know the problem arises in how I'm assigning the next char in the file to the code pointer but I'm just not sure what that is.
My pointer knowledge is lacking which is the point of this exercise.
The interpreter works fine, all using pointers, I'm just having a problem reading files in to it.
(I'm going to implement only reading "+-><[].," into the file later, although if anyone has a good way to do it, it would be great if you'd let me know!)
Thanks in advance
There are a number of things wrong with your code:
char *readFile(char *fileName)
{
FILE *file;
char *code = malloc(1000 * sizeof(char));
file = fopen(fileName, "r");
do
{
*code++ = (char)fgetc(file);
} while(*code != EOF);
return code;
}
- What if the file is greater than 1,000 bytes?
- You are increasing
code
each time you read a character, and you returncode
back to the caller (even though it is no longer pointing at the first byte of the memory block as it was returned bymalloc
). - You are casting the result of
fgetc(file)
tochar
. You need to check forEOF
before casting the result tochar
.
It is important to maintain the original pointer returned by malloc
so that you can free it later. If we disregard the file size, we can achieve this still with the following:
char *readFile(char *fileName)
{
FILE *file = fopen(fileName, "r");
char *code;
size_t n = 0;
int c;
if (file == NULL)
return NULL; //could not open file
code = malloc(1000);
while ((c = fgetc(file)) != EOF)
{
code[n++] = (char) c;
}
// don't forget to terminate with the null character
code[n] = '\0';
return code;
}
There are various system calls that will give you the size of a file; a common one is stat
.
这篇关于按字符用C读取文件字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!