问题
Hi guys I have a text file which is known as dictionary.txt. Basically i am doing a menu of two choices, 1. add new words to dictionary and 2. delete words from dictionary. right now i managed to do the menu and add new words. However, I am stuck at deleting from a file. I want that when the user enters for example "runners" the word is searched in the dictionary.txt and it is deleted. To tell you everything I haven't covered it yet at school but I am looking for some ideas here so that I can move on with the task. I have tried some things but as I already told you, I haven't covered it yet so I don't know how to actually do it. I appreciate all the help. below is my program.
回答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.
Josuel, as taken from my previously answered question by Richard Urwin
You can use the following code:
#include <stdio.h>
int main()
{
FILE *fileptr1, *fileptr2;
char filename[40];
char ch;
int delete_line, temp = 1;
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;
}
回答2:
There is no way to delete something from a file. Filesystems do not support it.
Thats how you can modify the file's contents:
- delete the whole file contents (whenever you open a file for writing, it happens by default)
- delete the file itself
- rewrite some part of a file, replacing several bytes by the same number of bytes
- append to the end of the file
So to remove a word, you should either read the whole file to memory, delete the word, and then rewrite it, or replace the word by spaces (or any other characters).
回答3:
you can open the file in binary mode so then load the content to a string or array of strings then do the searching/deleting/editing on the string then clear the content of the file and at last you write the new content to the file.
来源:https://stackoverflow.com/questions/41547756/how-can-i-delete-from-a-text-file