对不起,这家伙的基本问题,但我有一个问题只是让fwrite()
正常工作?fwrite()失败,没有写任何东西到现有的文件
#include <iostream>
#include <fstream>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int main() {
FILE* fd = NULL;
fd = fopen("out","rw");
if (fd == NULL) {
printf("Open failed\n");
return -1;
}
int error = 0;
printf("Attempting write ... \n");
char buff[] = {"hello?\n"};
if((error = fwrite(buff, 1, 7, fd)) != 7) {
printf("fwrite() failed with code %d \n", error);
return -1;
}
fclose(fd);
return 0;
}
此代码失败 - fwrite()
刚刚返回0,当它应该返回7而不是用于写入文件7个1B字符。该文件确实存在于相同的目录中;我已经尝试完整的文件路径,而不是;我已经chmod'd输出文件out
为777万一是这个问题(它不是); fread()
和fseek()
都按预期工作,但为了简洁起见,我已将它们取出。
我在这里做错了什么?任何帮助表示赞赏。
这看起来像一个'c'问题而不是'C++',你应该改正标记 –
@appleapple你是对的 - 我必须在代码的某些部分使用C库,但总体项目我工作是技术上使用C++ – APaul