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.
Asked
Active
Viewed 9,175 times
1
-
7You cannot push a button, somewhere, and delete something from the beginning, middle or the end of the file. You have to read the existing contents of the file, and create a new file, without the removed parts. – Sam Varshavchik Jan 09 '17 at 12:09
-
1Why the downvote? I don't find anything weird in this question. – Alfonso Nishikawa Jan 09 '17 at 12:17
-
@SamVarshavchik yes, I am aware of that. I ve seen that there is a function fgets() which can be applied to read a line from the text file and stores it into the string pointed to. however, I dont know how I am going to apply it. Since I want to replace that string with nothing as I want to delete it. – Josuel Spiteri Jan 09 '17 at 12:19
-
@AlfonsoNishikawa I don't know why.. all I am doing here is asking for help just like everybody else and I think it's a question that makes sense. Thank you. – Josuel Spiteri Jan 09 '17 at 12:20
-
Free clue: now this particular line you "want to delete it". But what about every line that comes before it and after it. You understand that you can use `fgets()` to read each one. Ok, what do you think should happen to those lines, then? – Sam Varshavchik Jan 09 '17 at 12:23
-
1) `void main()` is an invalid signature. 2) This looks like C, not C++. Removed the unrelated tag. If you compile as C++, change the tag, but don't spam tags! – too honest for this site Jan 09 '17 at 12:36
-
@JosuelSpiteri Replace it with something that will tell your program to ignore it. For example, perhaps replace "`runners`" with "`-------`" and code your program to ignore a "word" that consists only of dashes. Or rewrite the file. Or use a database. – David Schwartz Jan 09 '17 at 12:52
3 Answers
5
- 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;
}

rrz0
- 2,182
- 5
- 30
- 65
4
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).

user31264
- 6,557
- 3
- 26
- 40
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.

Raindrop7
- 3,889
- 3
- 16
- 27
-
-
1@JosuelSpiteri that's how most of file editing is done. You read the whole contents; store it in memory; manipulate it; and transfer **new** contents into the file again. – mr5 Jan 09 '17 at 12:33
-
or you should try SQL. it takes care about manipulatig file content but require additional libraries. And that way more complex than suggested – user5821508 Jan 09 '17 at 12:36
-
1@user5821508: That's nonsense. You cannot use SQL to manipulate a **text** file. – too honest for this site Jan 09 '17 at 12:40