填写文件ASCII值

Dav*_*vid 1 c file-io aio

我想创建一个填充0或其他字母的文件.这是我的功能

int fill(const int d, struct aiocb *aiorp, void *buf, const int count){
        int rv = 0;
        memset( (void *)aiorp, 0, sizeof( struct aiocb ) ); // <-here second paramether is 0
        aiorp->aio_fildes = d;
        aiorp->aio_buf = buf;
        aiorp->aio_nbytes = count;
        aiorp->aio_offset = 0;
        rv = aio_write( aiorp );
        return rv;
}
Run Code Online (Sandbox Code Playgroud)

这是我的主要内容

int main(int argc, char * argv[]){
        int des;
        int rv;
        struct aiocb aior;
        char buffer[1000];
        if(argc == 3){
                printf("just %s\n", argv[1]);
                des = createFile(argv[1]);
                rv = fill(des, &aior, buffer, sizeof(buffer));
        }
        return 0;
}
Run Code Online (Sandbox Code Playgroud)

所以我的输出应该是文件填充零值,但我的文件充满了垃圾

^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@$
y?^X^@^@^@^@^@^@^@
s|?^@^@^@^@^@^@^@^@^@^@^@^@?r|?^@^@^@^@??^??(?????{??
y?^P^@^@^@?
y?^A^@^@^@d^Cy?^@^@^@^@?
y?^T
y?^P???^@^@^@^@^@^@^@^
...
Run Code Online (Sandbox Code Playgroud)

为什么?怎么了?

这是代码:

sukurti - 如果该文件不存在则创建新文件并填充 - 填充创建的文件

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

int sukurti(char *name);
int fill(const int d, struct aiocb *aiorp, void *buf, const int count);

int sukurti(char *name){
   int dskr;
   dskr = open( name, O_RDONLY );
   if( dskr == -1 ){
          printf("Failas sukurtas, nes jo nebuvo\n");
          dskr = open( name, O_WRONLY | O_CREAT, 0644);

   }else{
           printf("Jau yra toks failas!\n");
           exit(1);
   }
   return dskr;
}

int fill(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 );
        return rv;

}

int main(int argc, char * argv[]){
        int des;
        int rv;
        struct aiocb aior;
        int x = atoi(argv[2]);
        printf("%d\n", x);
        int size = MB * MB * x;
        char buffer[size];
        if(argc == 3){
                printf("just %s\n", argv[1]);
                des = sukurti(argv[1]);
                rv = fill(des, &aior, buffer, sizeof(buffer));
        }else{
                printf("Blogas\n");
        }
        return 0;
}
Run Code Online (Sandbox Code Playgroud)

编辑:我知道我的文件到文件结束

alk*_*alk 5

这里有三个问题:

  1. buffer不是初学者.这样做使用

    memset(buffer, 
      <what ever 8bit value you want the file to be filled with>, 
      sizeof buffer);
    
    Run Code Online (Sandbox Code Playgroud)

    在定义它之后main().

  2. aior错误地初始化.将其初始化为所有0使用

    memset(aiorb, 0, sizeof aior);
    
    Run Code Online (Sandbox Code Playgroud)

    在定义之后立即main()删除对memset()in 的调用fill().

  3. 最后,程序很可能缓冲区异步写入磁盘之前结束.

    要解决此问题,请定义下面提到的通知方法man 7 aio.并使程序结束程序之前等待接收此通知.

    例如,这可以通过询问通过信号的完成通知并等待该信号来完成.

    为此,请按以下方式修改代码:

    • 将以下两行添加到aiorp指向的初始化中fill():

      aiorp->aio_sigevent.sigev_notify = SIGEV_SIGNAL;
      aiorp->aio_sigevent.sigev_signo = SIGUSR1;
      
      Run Code Online (Sandbox Code Playgroud)
    • 为了能够处理发送的通知信号(不结束程序),需要设置信号处理程序:

      void handler(int sig)
      {
        /* Do nothing. */
      }
      
      Run Code Online (Sandbox Code Playgroud)
    • 通过调用安装此处理程序

      signal(handler, SIGUSR1);
      
      Run Code Online (Sandbox Code Playgroud)

      就在程序的开头.

    • 之前return从荷兰国际集团main()的呼叫wait_for_completion(SIGUSR1)可能是这样的:

      void wait_for_completion(int sig)
      {
        sigset_t set;
        sigemptyset(&set);
        sigaddset(&set, sig);
      
        sigwait(&set, &sig); /* This blocks until sig had 
                                been received by the program. */
        printf("Completion notification for asynchronous 'write'-operation received.\n");
      }  
      
      Run Code Online (Sandbox Code Playgroud)

    根据需要添加错误处理.为了便于阅读,我把它留了下来.