<分区>
我正在尝试写出用户输入到 argv[2] 中的数据。我必须使用 write() 系统调用 (unix)
例如,我输入“hi there”,但“hi th”被写入文件而不是整个文本。
#include <iostream>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <string.h>
using namespace std;
using std::cout;
using std::endl;
int main(int argc, char *argv[])
{
int fd, fileWrite;
char *file = argv[1]; //file to be create
char *text = argv[2]; //text stored here
fd = open(file, O_WRONLY | O_APPEND | O_CREAT);
//write message from command line
fileWrite = write(fd, text, sizeof(text));
fileWrite = write(fd, "\n", 1);
if(fileWrite == -1){
perror("fileWrite");
}
//close file
close(fd);
return 0;
}`