c - 从不兼容的指针类型传递 fwrite 的参数 4
问题描述
我想将源文件的内容复制到目标文件,但我收到以下警告:
warning: passing argument 4 of ‘fwrite’ from incompatible pointer type [-Wincompatible-pointer-types]
fwrite(target, sizeof(char), targetSize, sourceContent);
如果我忽略警告,则会出现分段错误。
FILE *source = fopen(argv[1], "r");
FILE *target = fopen(argv[2], "w");
if (source == NULL || target == NULL) {
printf("One or both files do NOT exist\n");
abort();
}
fseek(source, 0, SEEK_END);
long sourceSize = ftell(source);
fseek(source, 0, SEEK_SET);
char *sourceContent = (char *)malloc(sourceSize);
fread(sourceContent, sizeof(char), sourceSize, source);
long targetSize = sourceSize;
fwrite(target, sizeof(char), targetSize, sourceContent);
解决方案
两者都fread()
将fwrite()
读取/写入的缓冲区作为第一个参数,将文件作为第四个参数。
// This is fine.
fread(sourceContent, sizeof(char), sourceSize, source);
// Swap the first and fourth argument in the fwrite call.
fwrite(sourceContent, sizeof(char), targetSize, target);
推荐阅读
- reactjs - 错误:无法在回调中调用 React Hook “useLogout”。阿波罗
- twitter - Twitter:图像元标记未显示其 Twitter 卡的图像
- javascript - jQuery可拖动对象位置保存在SQL中
- ios - 真实相机透视投影
- amazon-web-services - AWS AppSync 中的 InvalidUserPoolException
- php - 如果购物车不为空,登录后重定向到购物车页面
- ios - 尝试转到另一个页面时,UITableView 跳下导航工具栏
- python - “AttributeError:模块'bezier'没有属性'Curve'错误
- javascript - 访问 .JSON 文件中的特定对象并创建选择菜单
- angular - 如何在 Angular 中制作通用解析器 (2+)