c语言file*fp错误,C语言用fgetc()读文件错误

fp = fopen("2.txt","rb");

#include

#include

#include

#include

int main(void)

{

FILE* fp = fopen("2.txt", "rb");

if(!fp) {

printf("File opening failed\n");

return EXIT_FAILURE;

}

int count = 0;

int c; // note: int, not char, required to handle EOF

while ((c = fgetc(fp)) != EOF) { // standard C I/O file reading loop

for(int i=0;i<8;i++){

printf("%d", c>>(7-i)&1);

}

printf(" ");

if (!(++count%8)) {

printf("\n");

}

//getch();

}

if (ferror(fp))

printf("\nI/O error when reading\n");

else if (feof(fp))

printf("\nEnd of file reached successfully\n");

fclose(fp);

getch();

return 0;

}

结果

00000000 00000000 00000000 00000000 00100001 11100100 11000110 11111010

00000000 00000000 00000000 00000000 00101101 10111001 00000100 00101010

00000000 00000000 00000000 00000000 00011010 00001011 11011010 11001110

00000000 00000000 00000000 00000000 00101011 11000101 01011111 00101101

00000000 00000000 00000000 00000000 00000011 00101011 11101001 11101001

00000000 00000000 00000000 00000000 00110000 01010101 10011111 11111111

00000000 00000000 00000000 00000000 00001001 00011000 11000011 00100110

00000000 00000000 00000000 00000000 00001111 00001110 00010100 11011010

00000000 00000000 00000000 00000000 00100000 00001101 11100111 10101011

00000000 00000000 00000000 00000000 00101011 11000101 01011111 00101101

00000000 00000000 00000000 00000000 00100100 10100111 00101111 01100111

00000000 00000000 00000000 00000000 00000101 01100100 01011001 01111000

00000000 00000000 00000000 00000000 00011001 11100101 10100010 10001101

00000000 00000000 00000000 00000000 00011110 00010001 10101001 01010000

00000000 00000000 00000000 00000000 00000011 00101011 11101001 11101001

00000000 00000000 00000000 00000000 00010101 11100000 01100000 10100111

00000000 00000000 00000000 00000000 00001000 11001111 11000001 10000010

00000000 00000000 00000000 00000000 00100000 00101110 11001100 00101011

00000000 00000000 00000000 00000000 00110110 01001010 00110001 10000111

00000000 00000000 00000000 00000000 00000011 00101011 11101001 11101001

00000000 00000000 00000000 00000000 00110000 01010101 10011111 11111111

00000000 00000000 00000000 00000000 00001001 00011000 11000011 00100110

00000000 00000000 00000000 00000000 00001111 00001110 00010100 11011010

00000000 00000000 00000000 00000000 00101001 10100011 10010000 11111011

00000000 00000000 00000000 00000000 00011110 01100001 00101000 01000000

00000000 00000000 00000000 00000000 00001100 11101000 11111001 00001111

00000000 00000000 00000000 00000000 00001101 00001010 10010111 11101101

00111001 00000000 00000000 00000000 00000000 00000010 01010111 01010011

11011110 00000000 00000000 00000000 00000000 00010110 00001011 00101110

10010001 00000000 00000000 00000000 00000000 00100001 11100100 11000110

11111010 00000000 00000000 00000000 00000000 00100110 00011111 10001110

00000010

End of file reached successfully

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值