Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何使用低级write()函数将字符串写入文件?_C_String_Low Level_Low Level Io_Low Level Code - Fatal编程技术网

C 如何使用低级write()函数将字符串写入文件?

C 如何使用低级write()函数将字符串写入文件?,c,string,low-level,low-level-io,low-level-code,C,String,Low Level,Low Level Io,Low Level Code,例如,我能够将输入文件的内容写入并使用以下命令输出文件: char buffer[1024]; // character buffer char userInput[1024]; // for user input char *p; char *q; int n; int input_file1; // file descriptor for file 1 int input_file2; // file descriptor for file 2 int output_file; // fil

例如,我能够将输入文件的内容写入并使用以下命令输出文件:

char buffer[1024]; // character buffer
char userInput[1024]; // for user input
char *p;
char *q;
int n;
int input_file1; // file descriptor for file 1
int input_file2; // file descriptor for file 2
int output_file; // file descriptor for output_file

    while((n = read(input_file1, buffer, sizeof(buffer))) > 0)
    {
    progress("entered first while loop");
        if((write(output_file, buffer, n)) < 0)
        {
            progress("couldn't write to output within while loop 1");
            perror(argv[3]);
            close(input_file1);
            close(input_file2);
            close(output_file);
            exit(1);
        }
    }
我想使用write()将用户输入附加到同一输出文件中

我该怎么做

----update----与

if(strcmp(argv[1], "-") == 0) // use standard-in for input file 1
    {
        progress("file 1 detected as \"-\": using std input");
        p = fgets(userInput, sizeof(userInput), stdin);
        if (write(output_file, userInput, sizeof(p)) < 0) {
            progress("write from stdin to output file failed");
            perror(argv[4]);
            exit(1);
        }
        progress("wrote from stdin to output file");
    }
if(strcmp(argv[1],“-”==0)//在输入文件1中使用标准
{
进度(“文件1检测为\“-\”:使用std输入”);
p=fgets(用户输入,sizeof(用户输入),标准输入);
if(write(输出_文件,用户输入,sizeof(p))<0){
进度(“从标准输入写入输出文件失败”);
perror(argv[4]);
出口(1);
}
进度(“从标准输入写入输出文件”);
}

只要你做同样的事情。但您不必在第一时间关闭该文件

从用户(stdin)获取输入,然后使用
write()
函数将其写入文件

q = fgets(userInput, sizeof(userInput), stdin);
write(output_file, userInput, strlen(userInput));
close(output_file);

假设您已经打开了
output\u文件
进行写入(并可能写入其中):

#定义最大值120
字符缓冲区[MAX];
int len=sprintf(缓冲区,“以%d个字符打印的任何内容。\n”,最大值);
if(写入(输出文件、缓冲区、len)<0){
perror(“无法写入文件”);
出口(1);
}
write()函数具有以下原型:

ssize_t write(int fildes, const void *buf, size_t nbyte);
在常规文件或其他能够查找的文件上,数据的实际写入应从文件中与fildes相关的文件偏移量指示的位置开始。在从write()成功返回之前,文件偏移量应增加实际写入的字节数。对于常规文件,如果增加的文件偏移量大于文件长度,则应将文件长度设置为该文件偏移量

对于无法查找的文件,应始终从当前位置开始写入。与此类设备关联的文件偏移量值未定义

如果设置了文件状态标志的O_APPEND标志,则应在每次写入之前将文件偏移量设置到文件的末尾,并且在更改文件偏移量和写入操作之间不应发生干涉文件修改操作。

打开()
使用O_APPEND模式输出文件

output_file = open("output", O_WRONLY | O_APPEND | O_CREAT);
这样,write将附加到文件中

write(output_file, userInput, strlen(userInput));

test.c:96:错误:调用的对象“userInput”不是functiontest.c:在函数“main”中:test.c:96:警告:内置函数“strlen”的不兼容隐式声明@Chips use
\include
。你必须做出一些努力来完成你的开发我尝试了这个,它会自动输入文本,而不是等待我输入任何东西。这是随机垃圾:?[]Ð?t&这回答了“如何使用低级write()函数将字符串写入文件?”。它也不会向您展示如何做其他事情,比如读取用户输入。你的问题中的这一部分是正确的。test.c:在函数“main”中:test.c:96:警告:内置函数“strlen”的不兼容隐式声明你必须
#包含
a+和我使用的有什么区别:O_WRONLY | O|u APPEND | O|u creatoromg。。。对不起,我弄错了。这确实是您所使用的,“a+”是php>_
output_file = open("output", O_WRONLY | O_APPEND | O_CREAT);
write(output_file, userInput, strlen(userInput));