gpt4 book ai didi

C - write() 不写入文件

转载 作者:行者123 更新时间:2023-11-30 17:27:58 25 4
gpt4 key购买 nike

我的问题很简单,当我尝试确认函数是否写入文件时,没有任何显示,文件在那里,但文件内没有任何内容,其中应该是我想要的字符串的 1024 倍。

int escreve1x( const char* path , const char* cadeia )

int fd = open( path, O_CREAT, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH );
int i;

printf("%s\n", cadeia);

for ( i=0 ; i<=1024 ; i++ )
{
write( fd, cadeia, 10);
}

return 0 ;

最佳答案

1-添加“O_RDWR”标志以使 fd 可写。2-始终调试 write 系统调用的结果;

int fd = open( path, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH );
if(fd<0) {
printf("Open failed\n");
return 1;
}
int i;
printf("%s\n", cadeia);
for ( i=0 ; i<=1024 ; i++ ) {
int status=write( fd, cadeia, 10);
if(status<0) {
printf("Write failed");
}
}
return 0;

如果它说写入失败,则检查 errno。

关于C - write() 不写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26195332/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com