在 C 中逐字符读取文件 [英] Reading a file character by character in C

查看:18
本文介绍了在 C 中逐字符读取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 C 编写 BF 解释器,但在读取文件时遇到了问题.我曾经使用 scanf 来读取第一个字符串,但随后你的 BF 代码中不能有空格或注释.

现在这就是我所拥有的.

char *readFile(char *fileName){文件 *文件;char *code = malloc(1000 * sizeof(char));file = fopen(fileName, "r");做{*code++ = (char)fgetc(file);} while(*代码!= EOF);返回码;}

我知道问题在于我如何将文件中的下一个字符分配给代码指针,但我只是不确定那是什么.
我的指针知识缺乏,这是本练习的重点.解释器工作正常,全部使用指针,我只是在读取文件时遇到问题.

(我将在稍后实现只将 +-><[]., 读入文件,尽管如果有人有好的方法,如果你会让我知道的!)

解决方案

你的代码有很多问题:

char *readFile(char *fileName){文件 *文件;char *code = malloc(1000 * sizeof(char));file = fopen(fileName, "r");做{*code++ = (char)fgetc(file);} while(*代码!= EOF);返回码;}

  1. 如果文件大于 1,000 字节怎么办?
  2. 每次读取一个字符时都会增加 code,然后将 code 返回给调用者(即使它不再指向字符的第一个字节)malloc 返回的内存块.
  3. 您正在将 fgetc(file) 的结果转换为 char.在将结果转换为 char 之前,您需要检查 EOF.

保持malloc返回的原始指针很重要,以便以后可以释放它.如果我们不考虑文件大小,我们仍然可以通过以下方式实现:

char *readFile(char *fileName){FILE *file = fopen(fileName, "r");字符*代码;size_t n = 0;国际 c;如果(文件 == NULL)返回空;//无法打开文件代码 = malloc(1000);而((c = fgetc(文件))!= EOF){代码[n++] = (char) c;}//不要忘记以空字符结束代码[n] = '';返回码;}

有多种系统调用可以为您提供文件大小;一个常见的是stat.>

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!)

解决方案

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

  1. What if the file is greater than 1,000 bytes?
  2. You are increasing code each time you read a character, and you return code back to the caller (even though it is no longer pointing at the first byte of the memory block as it was returned by malloc).
  3. You are casting the result of fgetc(file) to char. You need to check for EOF before casting the result to char.

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] = '';        

    return code;
}

There are various system calls that will give you the size of a file; a common one is stat.

这篇关于在 C 中逐字符读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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