Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/25.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 O_追加的使用_C_Linux - Fatal编程技术网

C O_追加的使用

C O_追加的使用,c,linux,C,Linux,运行此代码时出现错误的文件描述符错误 #include <stdio.h> #include <unistd.h> #include <fcntl.h> void main() { int fd; char buff[50]; char wrt[4]="Fine"; fd = open("temp.txt",O_APPEND); if (fd == -1) { perror("agia error"); } else {

运行此代码时出现错误的文件描述符错误

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>

void main() { 
  int fd;
  char buff[50];
  char wrt[4]="Fine";
  fd = open("temp.txt",O_APPEND);
  if (fd == -1) {
    perror("agia error");
  } else { 
    int cw = write(fd, wrt,4);
    if (cw == -1) {
      perror("Errori");
    }
    close(fd);
  }
}
#包括
#包括
#包括
void main(){
int-fd;
字符buff[50];
char wrt[4]=“罚款”;
fd=打开(“temp.txt”,O_APPEND);
如果(fd==-1){
佩罗尔(“agia错误”);
}否则{
int cw=写入(fd,wrt,4);
如果(cw==-1){
佩罗尔(“错误”);
}
关闭(fd);
}
}

虽然给出了答案,甚至有人指出缺少必要的标题(不是全部),但这不能保持原样

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
这应该是int main(void)

未使用的变量

  char wrt[4]="Fine";
不对。“Fine”的长度为4个字符,这意味着要实现适当的nul终止,需要5个字符的缓冲区。幸运的是,您不需要将大小指定为char wrt[]=“Fine”;我会帮你的。除了这里可能不需要这样的缓冲区,但需要char*wrt=“Fine”;相反

  fd = open("temp.txt",O_APPEND);
已经提到了错误的标志用法

  if (fd == -1) {
    perror("agia error");
返回一个错误,而不仅仅是打印出发生了什么

  } else { 
    int cw = write(fd, wrt,4);
容易出错的数字4的重复。如果您有一个bufer(可能还有减法1)或strlen,它应该使用sizeof

    if (cw == -1) {
      perror("Errori");
    }
    close(fd);
  }
}

虽然给出了答案,甚至有人指出缺少必要的标题(不是全部),但这不能保持原样

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
这应该是int main(void)

未使用的变量

  char wrt[4]="Fine";
不对。“Fine”的长度为4个字符,这意味着要实现适当的nul终止,需要5个字符的缓冲区。幸运的是,您不需要将大小指定为char wrt[]=“Fine”;我会帮你的。除了这里可能不需要这样的缓冲区,但需要char*wrt=“Fine”;相反

  fd = open("temp.txt",O_APPEND);
已经提到了错误的标志用法

  if (fd == -1) {
    perror("agia error");
返回一个错误,而不仅仅是打印出发生了什么

  } else { 
    int cw = write(fd, wrt,4);
容易出错的数字4的重复。如果您有一个bufer(可能还有减法1)或strlen,它应该使用sizeof

    if (cw == -1) {
      perror("Errori");
    }
    close(fd);
  }
}

您需要至少选择一种写入模式
O_WRONLY
O_RDWR
,然后添加
O_APPEND
,例如
O_WRONLY | O_APPEND
Jean-Baptiste Yunès

您至少需要选择一种书写模式
Oèu WRONLY
Oèu RDWR
,然后添加
Oèu APPEND
,例如
Oèu WRONLY | Oèu APPEND
。-Jean-Baptiste Yunès

在将来使用四个空格缩进代码片段(继续浏览-帮助页面)。例如,您需要选择一种书写模式
Oèu WRONLY
Oèu RDWR
,然后添加
Oèu APPEND
,因此
Oèu WRONLYèOèAPPEND
。请发布有效的代码-
main()
必须返回
int
,并且您需要包含
以声明
打开
写入
关闭
perror
@hek2mgl您仍然缺少的包含项<代码>写入需要
unistd.h
perror
需要
stdio.h
。编译器肯定在警告您隐式声明—您是否忽略了这些警告?
gcc-Wall-Wextra
是一个很好的编译器;-)在将来使用四个空格缩进代码片段(继续浏览-帮助页面),您需要选择一种写入模式
O_WRONLY
O_RDWR
,然后添加
O_APPEND
,因此,例如
O_WRONLY | O_APPEND
。请发布有效的代码-
main()
必须返回
int
,您需要包括
来声明
打开
写入
关闭
perror
@hek2mgl您仍然缺少的包括<代码>写入需要
unistd.h
perror
需要
stdio.h
。编译器肯定在警告您隐式声明—您是否忽略了这些警告?
gcc-Wall-Wextra
是一个很好的编译器;-)