【发布时间】:2016-11-09 17:56:06
【问题描述】:
如何在文件中写入我的输入? 这是我的代码
#include <iostream>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
using namespace std;
int main(int argc,char *argv[]){
int fd;
int x;
int *y;
fd=open(argv[1], O_WRONLY|O_CREAT , S_IREAD|S_IWRITE);
cin>>x;
*y=x;
write(fd,y, 4);
}
return 0;
}
也许我给文件的权限有问题,因为当我尝试从目录打开文件时,它给出了这个
“打开文件“/home/ubuntu/Desktop/file.txt”时出现问题。您打开的文件包含一些无效字符。如果继续编辑此文件,您可能会损坏此文档。 您也可以选择其他字符编码再试一次。”
【问题讨论】:
-
您没有使用
std::osftream的任何特殊原因? -
看起来像纯 c 代码。
-
@πάνταῥεῖ 不,有
cin参与 -
y声明在哪里? -
@Slava Eeek,我没有在所有大喊大叫的文件打开选项中发现这一点。