C编程 - 解析临时文件夹的难度

时间:2017-02-24 04:14:56

标签: c parsing

我在解析包含目录中各种文件的基本名称的temp.txt文件夹时遇到了一些麻烦。我想对这个文件进行排序,逐行找出每个文件类型的内容,然后从临时文件中删除它们。我已经能够对文件名进行解析以发现它们的类型,但是当我调用我的删除函数时...它只适用于某些文件并偶尔留下一些垃圾。

输入文件如下所示: temp.txt input

运行后的输出文件如下所示: temp.txt output

#include < stdio.h > 
#include < string.h > 
#include < unistd.h > 
#include < stdlib.h >

    /* This program module parses through the temp.txt file and finds .gif, .png, and .bmp files
       and prints to standard output their file type in the order in which they're found.
       It also takes all the files that aren't of those three types and puts them in a junk file.
    */
    int deleteline(int delete_line);
int main(int argc, char * argv[]) {
    FILE * file = fopen("temp.txt", "r"); /* should check the result */
    FILE * myfile = fopen("junkfiles.txt", "w");
    char line[4069];
    char * gif = ".gif";
    char * png = ".png";
    char * bmp = ".bmp";
    int i = 0;
    int j = 0;
    // if tempfile cannot be created, error handle
    if (!file) {
        puts("Some kind of file error!");
        return 1;
    }
    // if junkfile cannot be created, error handle
    if (!myfile) {
        puts("Some kind of file error!");
        return 1;
    }
    while (fgets(line, sizeof(line), file)) {
        i++;
        if (strstr(line, gif) != NULL) {
            j = deleteline(i);
            fflush(NULL);
            if (j == 0) {
                printf("File on line %d is: ", i);
                puts(line);
                printf("This is a .gif\n\n");
            } else {
                printf("Some error on line %d\n", i);
            }
        } else if (strstr(line, png) != NULL) {
            j = deleteline(i);
            if (j == 0) {
                printf("File on line %d is: ", i);
                puts(line);
                printf("This is a .png\n\n");
            } else {
                printf("Some error on line %d\n", i);
            }
        } else if (strstr(line, bmp) != NULL) {
            j = deleteline(i);
            if (j == 0) {
                printf("File on line %d is: ", i);
                puts(line);
                printf("This is a .bmp\n\n");
            } else {
                printf("Some error on line %d\n", i);
            }
        } else {
            j = deleteline(i);
            if (j == 0) {
                printf("The file on line %d is junk.\n\n", i);
            } else {
                printf("Some error on line %d\n\n", i);
            }
            fprintf(myfile, "%s", line);
        }
    }
    /* may check feof here to make a difference between eof and io failure -- network
       timeout for instance */
    fclose(file);
    fclose(myfile);
    fflush(NULL);
    return 0;
}
int deleteline(int delete_line) {
    FILE * fileptr1, * fileptr2;
    char * filename = "temp.txt";
    char ch;
    int temp = 1;
    //open file in read mode
    fileptr1 = fopen(filename, "r");
    ch = getc(fileptr1);
    //rewind
    rewind(fileptr1);
    //open new file in write mode
    fileptr2 = fopen("replica.txt", "w");
    while (ch != EOF) {
        ch = getc(fileptr1);
        if (ch == '\n')
            temp++;
        //except the line to be deleted
        if (temp != delete_line) {
            //copy all lines in file replica.c
            putc(ch, fileptr2);
        }
    }
    fclose(fileptr1);
    fclose(fileptr2);
    remove(filename);
    //rename the file replica.c to original name
    rename("replica.txt", filename);
    return 0;
}

0 个答案:

没有答案