File Handling in C - Removing specific words from a list in text file

99封情书 提交于 2019-12-02 12:45:50

问题


I am populating a short dictionary from my basic C program using the following code :

void main () {

FILE *fp;

fp = fopen("c:\\CTEMP\\Dictionary2.txt", "w+"); 

fprintf(fp, Word to Dictionary");

However I would also wish to remove certain words which I do not longer wish to be in the dictionary. I did some research and I know that

" You can't remove content from a file and have the remaining content shifted down. You can only append, truncate or overwrite.

Your best option is to read the file in to memory, process it in memory and then write it back to disk"

How can I create a new file without the word I want to remove ?

Thanks


回答1:


  • You open two files: the one you've got (for reading) and a new one (for writing).
  • You loop through the first file reading each line in turn.
  • You compare the contents of each line with the words you need to delete.
  • If the line does not match any of the deletion words, then you write it to the new file.

If the manipulation that you need to do is much more complex then you can literally "read it into memory" using mmap(), but that is a more advanced technique; you need to treat the file as a byte array with no zero terminator and there are lots of ways to mess that up.




回答2:


I used the following code :

printf("Enter file name: ");
        scanf("%s", filename);
        //open file in read mode
        fileptr1 = fopen("c:\\CTEMP\\Dictionary.txt", "r");
        ch = getc(fileptr1);
        while (ch != EOF)
        {
            printf("%c", ch);
            ch = getc(fileptr1);
        }
        //rewind
        rewind(fileptr1);
        printf(" \n Enter line number of the line to be deleted:");
        scanf("%d", &delete_line);
        //open new file in write mode
        fileptr2 = fopen("replica.c", "w");
        ch = getc(fileptr1);
        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("c:\\CTEMP\\Dictionary.txt");
        //rename the file replica.c to original name
        rename("replica.c", "c:\\CTEMP\\Dictionary.txt");
        printf("\n The contents of file after being modified are as follows:\n");
        fileptr1 = fopen("c:\\CTEMP\\Dictionary.txt", "r");
        ch = getc(fileptr1);
        while (ch != EOF)
        {
            printf("%c", ch);
            ch = getc(fileptr1);
        }
        fclose(fileptr1);
        scanf_s("%d");
        return 0;

    }


来源:https://stackoverflow.com/questions/41546323/file-handling-in-c-removing-specific-words-from-a-list-in-text-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!