用零填写文件

时间:2015-05-06 11:02:59

标签: c aio-write

我的文件应该填充0.我想使用aio_write来做这个结果我的文件应该看起来像000000000....但是结果我得到了我的文件充满了垃圾

^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@
s|▒^@^@^@^@^@^@^@^@^@^@^@^@▒r|▒^@^@^@^@▒▒^?▒(▒▒▒▒s|▒▒
y▒^P^@^@^@^X▒▒▒
s|▒^X^@^@^@▒.....

我甚至无法想象出错是什么。首先,我使用异步写入,所以我需要在da_aio_write完成时等待

int da_test_wait( struct aiocb *aiorp ){
   const struct aiocb *aioptr[1];
   int rv;
   aioptr[0] = aiorp;
   rv = aio_suspend( aioptr, 1, NULL );
   if( rv != 0 ){
      perror( "aio_suspend failed" );
      abort();
   }
   rv = aio_return( aiorp );
   printf( "AIO complete, %d bytes write.\n", rv );
   return 1;
}

这也是我的写作功能

 int da_aio_write(const int d, struct aiocb *aiorp, void *buf, const int count){
   int rv = 0;
   memset( (void *)aiorp, 0, sizeof( struct aiocb ) );
   aiorp->aio_fildes = d;
   aiorp->aio_buf = buf;
   aiorp->aio_nbytes = count;
   aiorp->aio_offset = 0;

   rv = aio_write( aiorp );

   if( rv == -1) {
       perror("Error da_aio_write\n");
       exit(1);
       return rv;
   }
   return rv;
}

和我的主要

int main(int argc, char *argv[] ){
        int sk;
        int d;
        struct aiocb aior;
        if(argc == 3){
                sk = atoi(argv[2]);
                char buffer[MB * MB * sk];
                //memset(&aior, 0, sizeof( struct aiocb ));
                d = da_open(argv[1]);
                da_aio_write( d, &aior, buffer, sizeof(buffer) );
                da_test_wait( &aior );
                da_close( d );
        }
        return 0;
}

任何想法我做错了什么? 编辑: 我知道我的da_aio写在main之后结束,因为我编译后得到

File created
 dskr1 = 3 
AIO complete, 1048576 bytes write. 
closed

编辑我的完整更新代码

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

#define MB 1024

int da_open(const char *name);
int da_aio_write(const int d, struct aiocb *aiorp, void *buf, const int count);
int da_test_wait( struct aiocb *aiorp );
int da_close(int fd);

int da_open(const char *name){
   int dskr;
   int dskr2;
   dskr = open( name, O_RDWR );
   if( dskr == -1 ){
       printf("File created\n");
       dskr2 = open( name, O_WRONLY | O_CREAT, 0644);
   }else{
       printf("End job!\n");
       exit(1);
   }
   printf( "dskr1 = %d\n", dskr2 );
   return dskr2;
}

int da_aio_write(const int d, struct aiocb *aiorp, void *buf, const int count){
   int rv = 0;
   memset( (void *)aiorp, 0, sizeof( struct aiocb ) );
   aiorp->aio_fildes = d;
   aiorp->aio_buf = buf;
   aiorp->aio_nbytes = count;
   aiorp->aio_offset = 0;

   rv = aio_write( aiorp );

   if( rv == -1) {
       perror("Error da_aio_write\n");
       exit(1);
       return rv;
   }
   return rv;
}

int da_test_wait( struct aiocb *aiorp ){
   const struct aiocb *aioptr[1];
   int rv;
   aioptr[0] = aiorp;
   rv = aio_suspend( aioptr, 1, NULL );
   if( rv != 0 ){
      perror( "aio_suspend failed" );
      abort();
   }
   rv = aio_return( aiorp );
   printf( "AIO complete, %d bytes write.\n", rv );
   return 1;
}

int da_close(int fd){
   int rv;
   rv = close( fd );
   if( rv != 0 ) perror ( "close() failed" );
   else puts( "closed" );
   return rv;
}

int main(int argc, char *argv[] ){
        int sk;
        int d;
        struct aiocb aior;
        if(argc == 3){
                sk = atoi(argv[2]);
                char buffer[MB * MB * sk];
                int size; 
                size = MB * MB * sk;
                memset( buffer, '\0', size);
                //memset(&aior, '\0', sizeof( struct aiocb ));
                d = da_open(argv[1]);
                da_aio_write( d, &aior, buffer, sizeof(buffer) );
                da_test_wait( &aior );
                da_close( d );
        }
        return 0;
}

1 个答案:

答案 0 :(得分:3)

char buffer[MB * MB * sk];

堆栈变量不会自动初始化。所以你的缓冲区包含垃圾。如果那是你要写入文件的内容,则memset首先为0。