使用FWRITE用C写逐字节的文件字节() [英] Write a file byte by byte in C using fwrite()

查看:371
本文介绍了使用FWRITE用C写逐字节的文件字节()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图读取字节的文件字节,并将其写入到另一个文件。我有这个code:

I'm trying to read a file byte by byte and write it to another file. I have this code:

if((file_to_write = fopen(file_to_read, "ab+")) != NULL){

  for(i=0; i<int_file_size; i++){
    curr_char = fgetc(arch_file);

    fwrite(curr_char, 1, sizeof(curr_char), file_to_write);
  }
}

其中, int_file_size 是我想读的字节数量, arch_file 是我从阅读文件和 curr_char 是一个字符指针。

where int_file_size is the amount of bytes I want to read, arch_file is the file I'm reading from, and curr_char is a char pointer.

然而,这是行不通的。我得到在循环的第一次迭代分割故障(核心转储)错误。我是pretty肯定有什么问题我FWRITE()语句。任何帮助将是AP preciated。

However this doesn't work. I get Segmentation fault (core dumped) error on the first iteration in the loop. I'm pretty sure there is something wrong with my fwrite() statement. Any help would be appreciated.

推荐答案

您应该通过的地址 curr_char 的,而不是 curr_char 本身:

fwrite(&curr_char, 1, sizeof(curr_char), file_to_write);
//     ^------ Here

这篇关于使用FWRITE用C写逐字节的文件字节()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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