【问题标题】:How can I write a string to a file using the low-level write() function?如何使用低级 write() 函数将字符串写入文件?
【发布时间】:2013-11-09 17:36:12
【问题描述】:

例如,我可以将输入文件的内容写入和输出文件:

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);
        }
    }

我还有一些用户输入:

printf("\nEnter text then hit enter: ");
q = fgets(userInput, sizeof(userInput), stdin);

我想使用 write() 将用户输入附加到同一个输出文件中;

我该怎么做?

-----更新----适用于

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");
    }

【问题讨论】:

    标签: c string low-level low-level-io low-level-code


    【解决方案1】:

    只需open() 模式为 O_APPEND 的输出文件

    output_file = open("output", O_WRONLY | O_APPEND | O_CREAT);
    

    这样,write 将追加到文件中。

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

    【讨论】:

    • test.c:在函数'main'中:test.c:96:警告:内置函数'strlen'的隐式声明不兼容
    • 你必须#include &lt;string.h&gt;
    • a+和我用的有什么区别:O_WRONLY|O_APPEND|O_CREAT
    • 天啊...对不起,我弄错了。这确实是您使用的,“a+”是 php >_open 的调用无处可寻。当你问如何附加时,我认为你没有正确打开它。
    【解决方案2】:

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

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

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

    【讨论】:

    • test.c:96: 错误:调用的对象‘userInput’不是函数
    • test.c:在函数'main'中:test.c:96:警告:内置函数'strlen'的隐式声明不兼容
    • @Chips 使用#include &lt;srting.h&gt;。你必须付出一些努力才能完成你的开发
    【解决方案3】:

    write() 函数有原型:

    ssize_t write(int fildes, const void *buf, size_t nbyte);
    

    在常规文件或其他能够查找的文件上,数据的实际写入应从文件中与 fildes 关联的文件偏移量指示的位置开始。在从 write() 成功返回之前,文件偏移量应增加实际写入的字节数。在普通文件上,如果这个增加的文件偏移量大于文件的长度,文件的长度应该设置为这个文件的偏移量。

    在无法查找的文件上,写入总是从当前位置开始。与此类设备关联的文件偏移值未定义。

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

    【讨论】:

      【解决方案4】:

      假设您已经打开output_file 进行写入(并且可能写入):

      #define MAX 120
      char buffer[MAX];
      int len = sprintf(buffer, "Whatever you want to print in %d characters.\n", MAX);
      if (write(output_file, buffer, len) < 0) {
         perror("Cannot write to file.");
         exit(1);
      }
      

      【讨论】:

      • 我试过了,它会自动输入文本,而不是等待我输入任何内容。这是随机垃圾:?[]Ð?t&
      • 这回答了“如何使用低级 write() 函数将字符串写入文件?”的问题。它也没有向您展示如何做其他事情,例如阅读用户输入。你的问题中的那部分是正确的。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-24
      • 1970-01-01
      • 2011-10-23
      • 2018-11-25
      • 1970-01-01
      • 2023-01-09
      • 1970-01-01
      相关资源
      最近更新 更多