C 用';a';

C 用';a';,c,asynchronous,memset,C,Asynchronous,Memset,差不多两个小时了,我的程序出了问题。我试图用字符“a”填充我的文件,但我的程序无法运行。这是我的写函数 int da_aio_write(const int d, struct aiocb *aiorp, void *buf, const int count){ int rv = 0; memset( (void *)aiorp, 'a', sizeof( struct aiocb ) ); aiorp->aio_fildes = d; aiorp->aio

差不多两个小时了,我的程序出了问题。我试图用字符“a”填充我的文件,但我的程序无法运行。这是我的写函数

int da_aio_write(const int d, struct aiocb *aiorp, void *buf, const int count){
   int rv = 0;

   memset( (void *)aiorp, 'a', 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!!!\n"); // my program print this (Invalid argument)
       exit(1);
       return rv;
   }
   return rv;
}
不知何故,rv失败了
(rv==-1)
,我没有得到预期的结果。我添加了我的完整程序

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

#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("Failas sukurtas, nes jo nebuvo\n");
       dskr2 = open( name, O_WRONLY | O_CREAT, 0644);
   }else{
       printf("Toks failas jau yra!\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, 'a', 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) {
       printf("ERROR!!! ");
       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];
        memset(buffer, 0, sizeof buffer);
        d = da_open(argv[1]);
        da_aio_write( d, &aior, buffer, sizeof(buffer) );
        da_test_wait( &aior );
        da_close( d );
    }
    return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#定义MB 1024
int da_open(常量字符*名称);
int数据写入(常量int d、结构aiocb*aiorp、无效*buf、常量int计数);
内部数据测试等待(结构aiocb*aiorp);
内部数据关闭(内部fd);
int da_open(常量字符*名称){
int-dskr;
int-dskr2;
dskr=打开(名称,O_RDWR);
如果(dskr==-1){
printf(“Failas sukurtas,nes jo nebuvo\n”);
dskr2=打开(名称,O|u WRONLY | O|u CREAT,0644);
}否则{
printf(“Toks failas jau yra!\n”);
出口(1);
}
printf(“dskr1=%d\n”,dskr2);
返回dskr2;
}
整数数据写入(常数整数d、结构aiocb*aiorp、void*buf、常数整数计数){
int-rv=0;
memset((void*)aiorp,'a',sizeof(struct aiocb));
aiorp->aio_fildes=d;
aiorp->aio_buf=buf;
aiorp->aio\u nbytes=计数;
aiorp->aio_偏移=0;
rv=aio_写入(aiorp);
如果(rv==-1){
printf(“错误!!!”);
出口(1);
返回rv;
}
返回rv;
}
内部数据测试等待(结构aiocb*aiorp){
const struct aiocb*aioptr[1];
int-rv;
aioptr[0]=aiorp;
rv=aio_暂停(aioptr,1,空);
如果(rv!=0){
perror(“aio_暂停失败”);
中止();
}
rv=aio_返回(aiorp);
printf(“AIO完成,%d字节写入。\n”,rv);
返回1;
}
int da_关闭(int fd){
int-rv;
rv=关闭(fd);
如果(rv!=0)perror(“关闭()失败”);
其他认沽期权(“已关闭”);
返回rv;
}
int main(int argc,char*argv[]){
int sk;
int d;
结构aiocb aior;
如果(argc==3){
sk=atoi(argv[2]);
字符缓冲区[MB*MB*sk];
memset(buffer,0,sizeof buffer);
d=数据开放(argv[1]);
数据写入(d,&aior,buffer,sizeof(buffer));
数据测试等待(&aior);
达乌关闭(d);
}
返回0;
}
memset()的参数传递错误。线路应该是

memset(&aiorp, 'a', sizeof( struct aiocb ));
在main()函数中,您没有为缓冲区分配任何内容

da_aio_write()中的代码将是

而不是:
memset((void*)aiorp,'a',sizeof(struct aiocb))


不需要:
aiorp->aio_buf=buf

您可能需要检查失败原因?当检查失败时。例如,用于从错误中获取可打印的字符串,或直接打印消息。看起来您正在用“a”字节填充结构,然后通过设置结构的成员变量立即覆盖它。谢谢,我得到了无效参数。@samgak我刚设置了OffsetHanks,但随后我得到了分段错误(核心哑)。在使用gdb进行测试之后,我在../src/internetas.c:39 aiorp->aio\u fildes=d=3,aiorp=0x616161,buf=0x616161,count=1633771873的数据写入中得到这个0x08051026;这是因为aiorp是指向结构的指针,而不是结构。你能给我提供aiorp的结构吗?aiorp是指向结构aiocb的指针。我不明白那里怎么了/这里是memset()的问题。它正在生成运行时错误。memset()通常用于字符串。而且逻辑也不符合你的目标。您首先使用“a”对结构进行memeset,然后分配所有值。那么为什么要使用memset()?你能澄清这个目的吗?我想用“a”填充缓冲区。我应该在分配所有值后使用memset吗?(我试过了,但我得到了同样的结果…)
memset(aiorp->aio_buf, 'a', count);