开发者

Trying to read in the files of a directory and write it to a list

开发者 https://www.devze.com 2023-02-25 03:11 出处:网络
I\'m trying to create 5000 junk files, write them to a file and delete them. But this code only is writing a portion of the files to the file. ls -l | grep ^- | wc -l says I have 1598 files remaining

I'm trying to create 5000 junk files, write them to a file and delete them. But this code only is writing a portion of the files to the file. ls -l | grep ^- | wc -l says I have 1598 files remaining in the directory that is supposed to be emptied with unlink();. If I remove close(fd) I get a seg fault if I do any more than 1000 files. Any suggestions?

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>



main (int argv, char *args[]){
    if(argv<3){
        printf("Please run with proper command line arguements.\n");
        return;
    }
    int numFiles = atoi(args[1]);
    char *fileName = args[2];
    char *fileList[numFiles];
    int x, ret,fd;
    char buff[50];
    for(x=0;x<numFiles;x++){
        ret = sprintf(buff,"./stuff/%s-%d.junk",fileName, x);
        fd = creat(buff);
        close(fd);
    }
    DIR *odir = opendir("./stuff");        
    struct dirent *rdir = NULL;
    FILE *fp;
    fp = fopen("./files.list", "w");
    x=0;
    while(rdir = readdir(odir)){
        char* name = rdir->d_name;
        ret = sprintf(buff,"./stuff/%s-%d.junk",fileName, x);
        if(strcmp(name,"..")!=0){
            if(strcmp(name,".")!=0){
                fprintf(fp,"%s  %d\n",name,x);
                x++;
            }
        }
        unlink(buff);
    }
    close(fp);
    closedir(odir);
}

Thanks!

Note: Use of creat(), opendir(), readdir() and unlink() were required for the assignment. And as for error checking, your right of course but I'm under time constraints and the TA really, really doesn't care... But t开发者_如何学Gohank you all!


Here you're using fopen:

FILE *fp;
fp = fopen("./files.list", "w");

But then you're using close instead of fclose to close it:

close(fp);

I'm not at all sure this is what's causing the problem you're seeing, but it's definitely wrong anyway. You probably just want unlink(rdir->d_name) instead of unlink(buff). You embedded the number into the file name when you created it -- you don't need to do it again when you're reading in the name of the file you created.


You're removing things from the directory while calling readdir; I think that's supposed to work OK, but you might want to consider avoiding it.

More to the point: as you iterate over the directory with readdir you're potentially removing different files from the ones readdir is listing. (Because what you pass to unlink is buff which you've filled in from the steadily-incrementing x rather than from anything returned by readdir.) So, here's a toy example to show why that's problematic. Suppose the directory contains files 1,2,3,4 and readdir lists them in the order 4,3,2,1.

  • readdir tells you about file 4. You delete file 1.
  • readdir tells you about file 3. You delete file 2.
  • readdir would have told you about file 2, but it's gone so it doesn't.
  • readdir would have told you about file 1, but it's gone so it doesn't.

You end up with files 3 and 4 still in the directory.

0

精彩评论

暂无评论...
验证码 换一张
取 消