读取char 0x1A时发生文件结尾 [英] End of File occurs when reading char 0x1A

查看:87
本文介绍了读取char 0x1A时发生文件结尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试读取其中包含大约1000个字符的文件.遇到0x1A字符时,文件读取终止.我想要那个:

I am trying to read a file with around 1000 characters in it. The file reading terminates when an 0x1A character is encountered. I want that:

  1. 0x1A不应终止读取.

0x1A应该像普通字符一样存储.

0x1A should be stored like a normal character.

也许可以使用另一种读取文件的方法吗?

Can I use an alternate method of reading the file, maybe?

int main(void)
{
    int x=0,ch = ' ', file_name[25], arr[1000];
    FILE *fp;

    printf("Enter the name of file you wish to see\n");
    gets(file_name);

    fp = fopen(file_name, "r"); // read mode

    if (fp == NULL)
    {
        perror("Error while opening the file.\n");
        exit(EXIT_FAILURE);
    }

    printf("The contents of %s file are :\n", file_name); getchar();

    int y= 0;
    while ((ch = fgetc(fp)) != EOF)
    {
        printf("%d) %x \n",y, ch);
        arr[y++] = ch;
        printf(" %x \n", arr[(y- 1)]);
    }

    printf("Press to see the data off array..."); getchar();
    for (int x = 0; x < y; x++)
    {
        printf("%d ", (x + 1));
        printf(". %x \n", arr[x]);
    }
    getchar();
    fclose(fp);

    return(0);
}

推荐答案

您以txt模式打开了文件,

You opened the file as txt mode,

fp = fopen(file_name, "r"); // txt mode

请尝试以二进制模式读取0x1A,例如

Please try binary mode to read 0x1A,like

fp = fopen(file_name, "rb"); // binary mode

这篇关于读取char 0x1A时发生文件结尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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