在fopen("myfile", "r+")什么之间的区别"r+"和"w+"开放模式?我看了这个:
"r"打开文本文件进行阅读."w"打开文本文件以进行写入,将现有文件截断为零长度,或者创建文件(如果该文件不存在).
"r+"打开文本文件进行更新(即读取和写入)."w+"打开文本文件以进行更新(读取和写入),首先将文件截断为零长度(如果存在)或创建文件(如果文件不存在).
我的意思是不同的是,如果我打开文件"w+",文件将被删除?
我想创建一个命名管道,然后写入它,然后我想读取它。这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#define FIFO "fifo0001"
int main(intargc, char *argv[]){
char input[256];
FILE *fp;
char str[50];
printf("Please write some text:\n");
scanf("%s", input);
unlink(FIFO); /* Because it already exists, unlink it before */
umask(0);
if(mkfifo(FIFO, 0666) == -1){
printf("Something went wrong");
return EXIT_FAILURE;
}
if((fp = fopen(FIFO, "a")) == NULL){
printf("Something went wrong");
return EXIT_FAILURE;
}
fprintf(fp, "%s", input);
if(fgets(str, 50, fp) != NULL){
puts(str);
}
fclose(fp); …Run Code Online (Sandbox Code Playgroud)