как открыть файл внутри цикла while, который зависит от информации, еще не объявленной? - PullRequest
0 голосов
/ 06 марта 2019

Я пытаюсь открыть файл, который станет известен только после его создания в каталоге, однако функция FILE * infile (и т. Д. И т. Д.) В этом сценарии не работает, так как ранее «infile» не работалабыл в восторге.Я не могу понять, как объявить это перед циклом, чтобы он получал текущий файл, который повторяется в то время.

    #include <stdio.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <string.h>
    #include <errno.h>
    #include <sys/inotify.h>
    #include <openssl/sha.h>

    int main (int argc, char *argv[])
    {
            int result;
            int fd;
            int wd;
            unsigned char c[SHA512_DIGEST_LENGTH];
            int i;
            //FILE *inFile = fopen (filename, "rb"); //I'm aware this would usually
                                                     //be declared here              
            SHA512_CTX mdContext;
            int bytes;
            unsigned char data[1024];
            const int event_size = sizeof(struct inotify_event);
            const int buf_len = 1024 * (event_size + FILENAME_MAX);


            fd = inotify_init();

            if (fd < 0) {
              perror("inotify_init");
            }

            wd = inotify_add_watch(fd, "/home/joe/Documents", IN_CREATE);

            while (1) {
              char buff[buf_len];
              char target[FILENAME_MAX];
              int no_of_events, count = 0;

              no_of_events = read (fd, buff, buf_len);
              while (count < no_of_events) {
                struct inotify_event *event = (struct inotify_event *)&buff[count];
                if (event->len) {
                  if (event->mask & IN_CREATE)
                  if(!(event->mask & IN_ISDIR)) {
                    printf("The file %s has been created\n", event->name);

                    //FILE *infile = fopen (filename, "rb");  //issue arises here 
                                                              //when not commented
                    SHA512_Init (&mdContext);
                    while ((bytes = fread (data, 1, 1024, filename)) != 0)
                        SHA512_Update (&mdContext, data, bytes);
                    SHA512_Final (c,&mdContext);
                    for(i = 0; i < SHA512_DIGEST_LENGTH; i++) printf("%02x", c[i]);
                    printf (" %s\n", event->name);
                    fclose (filename);
                    return 0;
                    fflush(stdout);
                  }
                }
                count += event_size + event->len;
              }
            }
            return 0;
    }

Я пытаюсь решить проблему, поэтому комментарии итакже необъявленное "имя файла".

1 Ответ

1 голос
/ 06 марта 2019

Имя файла, который вы хотите открыть, хранится в event->name. Это то, что вы хотите передать fopen. Кроме того, вы хотите передать infile обоим fread и fclose.

FILE *infile = fopen (event->name, "rb");                   // event->name is the filename
SHA512_Init (&mdContext);
while ((bytes = fread (data, 1, 1024, infile )) != 0)       // read from infile
    SHA512_Update (&mdContext, data, bytes);
SHA512_Final (c,&mdContext);
for(i = 0; i < SHA512_DIGEST_LENGTH; i++) printf("%02x", c[i]);
printf (" %s\n", event->name);
fclose (infile);                                            // close infile
...