0

I am trying write a simple thing to a file it seems AIO doesn't working. What can be the problem ? I know there are extra headers which are unnecessary.

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

1 Answers1

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

is actually

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

Did you mean to busy wait until the write completed instead?

while(aio_error(&cb) == EINPROGRESS);
//                                  ^
close(file);
simonc
  • 41,632
  • 12
  • 85
  • 103
  • Yes I mean to busy wait, thanks I changed my code. But I can't read the file it says "gedit has not been able to detect the character encoding". I am using debian. – CrimsonKing Dec 22 '13 at 18:26
  • 1
    @IsmailHatip: you're writing 94 chars of garbage (well, zeros here) in that file after the "Sample" test. Fill the buffer with text or write out less if you want to read it as a normal text file. – Mat Dec 22 '13 at 18:27