【问题标题】:aio_write() isn't working when I try to write a simple text当我尝试编写简单文本时,aio_write() 不起作用
【发布时间】:2013-12-23 02:22:19
【问题描述】:

我正在尝试将一个简单的东西写入文件,它似乎 AIO 不起作用。可能是什么问题?我知道有多余的标题是不必要的。

#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<string.h>
#include<errno.h>
#include<stdlib.h>
#include<aio.h>

const int SIZE_TO_WRITE = 100;
char buffer[100];
struct aiocb cb;

int main()
{   

    int file = open("samp", O_CREAT|O_RDWR|O_TRUNC,0664);

    strcpy(buffer,"Sample");

    cb.aio_nbytes = SIZE_TO_WRITE;
    cb.aio_fildes = file;
    cb.aio_buf = buffer;    
    if(aio_write(&cb) == -1){
        printf("ERROR");
    }

    while(aio_error(&cb) == EINPROGRESS)    

    close(file);

    return 0;
}

【问题讨论】:

    标签: c aio


    【解决方案1】:
    while(aio_error(&cb) == EINPROGRESS)    
    close(file);
    

    其实是

    while(aio_error(&cb) == EINPROGRESS)    
        close(file);
    

    您的意思是忙着等到写入完成吗?

    while(aio_error(&cb) == EINPROGRESS);
    //                                  ^
    close(file);
    

    【讨论】:

    • 是的,我的意思是忙着等待,谢谢我改变了我的代码。但我无法读取它说“gedit 无法检测到字符编码”的文件。我正在使用 debian。
    • @IsmailHatip:在“样本”测试之后,您在该文件中写入了 94 个垃圾字符(好吧,这里是零)。如果您想将其作为普通文本文件读取,请用文本填充缓冲区或少写。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-17
    相关资源
    最近更新 更多