简体   繁体   中英

C program only writing ? to new file

I have this program:

int countLineTotal(FILE *file) {
    int line = 1;
    int ch = 'a';

    while (ch != EOF) {
        ch = fgetc(file);
        if (ch == '\n') {
            line++;
        }
    }
    return line;
}

void removeLine(char *name_of_file, int delete_line) {

    FILE *source_file;
    FILE *temporary_file;
    int temp = 1;
    int ch = 'a';

    source_file = fopen(name_of_file, "r");
    if (source_file == NULL)
    {
        perror("Could not open file: "); 
        return 1;
    }

    int line_total = countLineTotal(source_file);
    /*
        verify entered line number is valid
    */

    temporary_file = fopen("remove.temp", "w");
    if (temporary_file == NULL)
    { 
        perror("Could not open 'remove.temp': "); 
        return 1;
    }

    while (ch != EOF) {
        ch = fgetc(source_file);
        if (temp != delete_line) {
            putc(ch, temporary_file);
        }
        if (ch == '\n') {
            temp++;
        }
    }

    fclose(source_file);
    fclose(temporary_file);
    remove(name_of_file);
    rename("remove.temp", name_of_file);
}

int main() {
    int line_num = 3;
    char *file_name = "myfile.txt";

    removeLine(file_name, line_num);
    return 0;
}

Contents of myfile.txt :

content on line1
content on line2
content on line3
content on line4
content on line5

Final output in myfile.txt :

?

The program is either not writing anything to the temporary file, or it's completely overwriting it with... nothing? When trying to debug, I noticed that the while (ch != EOF) exited when temp was 1 and never higher. Not sure why this is at all.

Edit: updated code. After adding the code I missed out, I realized that I forgot to set the pointer back to the beginning of the file... which explains why temp was never higher than 1.

the code below works. Check out the differences, with tool like tkdiff. I changed a few things like eg file_name[12] and while-loop to do{}while(); , because you can NOT check "ch" BEFORE you have read any value to "ch". The code works now...

COMMENT: the "remove" and "rename" is really dangerous in this program. If you call program twice, then you have removed line 3 and 4, compared to original file. Rather work on a copy, or create a backup first... anyway that is not your question.

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

void removeLine(char *name_of_file, int delete_line) {

    FILE *source_file;
    FILE *temporary_file;
    int temp = 1;
    char ch;
    
    source_file = fopen(name_of_file, "r");
    if (source_file == NULL)
    {
        printf("Could not open file: "); 
        return;
    }

    temporary_file = fopen("remove.temp", "w");
    if (temporary_file == NULL)
    { 
        printf("Could not open 'remove.temp': "); 
        return;
    }

    do{
        ch = fgetc(source_file);
        if (temp != delete_line) {
            fputc(ch, temporary_file);
        }
        if (ch == '\n') {
            temp++;
        }
    }while(ch != EOF);

    fclose(source_file);
    fclose(temporary_file);
    remove(name_of_file); // NOT GOOD
    rename("remove.temp", name_of_file); // NOT GOOD

}

int main() {
    int line_num = 3;
    char file_name[12] = "myfile.txt";

    removeLine(file_name, line_num);
    return 0;
}

Here, see the "dangerous" effect of calling the program several times:

$ cat myfile.txt
test1
test12 aa
test14 xx xx
test15  cc
test16
test17
test18
$ ./a.out
$ cat myfile.txt
test1
test12 aa
test15  cc
test16
test17
test18
$ ./a.out
$ cat myfile.txt
test1
test12 aa
test16
test17
test18

I realized that I had forgot to set the pointer back to the beginning of the file, so the program started reading from the end of the file. This explains why temp was never higher than 1.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM