Tell me about deleting a file from your textfile

Good day sir

Can someone teach me how to delete a file or line in a textfile.

By the way, what i am trying to figure out this time sir is to delete the searched string... and when the user wishes to delete it.. it will include the next line..

Sample:

in my text file:

MyName1
SeatNumber1
MyName2
SeatNumber2
Myname3
SeatNumber3



I wonder how to delete MyName2 including SeatNumber2..


Thanks!.. Please help me
I don't understand. What do you mean by "include the next line". You want to delete the chosen line and the line after it?

I'm not sure if there's a function to remove lines from a file, but the way I'd do it would be to read the whole file into memory:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
std::string buffer[bufsz]; // An array of strings. bufsz should be a variable of type const int large enough to store the whole file.

std::ifstream infile;
int i = 0;

infile.open("filename.txt");

if (infile.is_open()) {
    while (!infile.eof()) { // Read while eofbit (end-of-file) not set (more content in file).
        std::getline(std::cin, buffer[i]); // Read current line into buffer.
        ++i;
    }

    infile.close()
}

// Now we have the whole file in memory 


Then I would delete the appropriate lines from the array:
1
2
3
4
for (; buffer[i] != searched_string; ++i); // The array does everything for us and will stop when searched_string == a line in the file.

// Found appropriate line, delete it from the array:
buffer[i] = buffer[i + 1] = ""; // Set both lines to NULL/blank string 


and then write buffer to the file again:

1
2
3
4
5
6
7
8
std::ofstream outfile;

outfile.open("filename.txt", ios::trunc); // ios::trunc tells fstream to truncate the file -- if the file already exists it's content is deleted.

if (outfile.is_open()) {
    outfile << buffer; // Write the buffer back to outfile without the strings we didn't want
    outfile.close();
}


and that's all.

If you want to delete a file; the C standard library has the function remove() which deletes a whole file. You could use that to delete the file and then rewrite it rather than use ios::trunc; but I think that my way above is better.
Last edited on
Help sir, how can i do that... hmm am i doing it right?

Here is my code sir.. for the case 5:

hmm..i enter a string.. and nothing happens.. hmm

Thanks for helping me guyz :)




Last edited on
You can use two file, one is temporary file.
What do you mean sir?
Could you please wrap your code in [code][/code] tags? I can't read it like that.
Sorry sir.. please forgive me i'm just a noob guy..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <string>

using namespace std;

int main()
{
FILE *fp;
char *ptrname;
char *ptrseat;
int index;
char name[100];
char seat[20];
int selection;
ptrname = name;
ptrseat = seat;
char name1[100];
int lines_read = 0;
string line_of_file;
string nameline;
string filecontrol;

cout<<"Good Day Sir/Maam!\n";
cout<<"What is your First name?\n";
cin.getline(name,100);
system("cls");
cout<<"What do you want me to do?\n";
while (selection != 6)
{
cout<<"\n1. Check all user registered with their seats\n2. Check if my name is registered\n3. Register someone\n4. Check Occupied Seats\n5. Delete a file.\n6. Exit\n";
cin>>selection;
system("cls");

switch (selection)
{
case 1:

cout<<"\nThe files from your text are: \n";
{
string line;

fstream myFile("Sample.txt");
if(myFile.is_open())
{
while (! myFile.eof())
{
getline (myFile,line);
cout<<line<<endl;

}
cin.get();
myFile.close();
system("pause");
system("cls");
}

else cout<<"Unable to open file";
}

break;
case 2:
cout<<"Well, let us see if you are registered in my file. . .\n";
{
ifstream input_file("sample.txt");
while(!input_file.eof())
{
getline(input_file, line_of_file);
if(input_file)
{
if (line_of_file == name)
{
filecontrol = line_of_file;
cout << "Seat number -> "<< line_of_file<<endl;
cout << "So you are already registered "<<name<<"\n";
}
}

else
break;
}
}
system("pause");
system("cls");
break;
case 3:

{
cout<<"What is the name? \n";
cin>>name1;
cin.get();
ifstream input_file("sample.txt");
while(!input_file.eof())
{
getline(input_file, line_of_file);
if(input_file)
{

if (line_of_file == name1)
{
cout<<"The name is already registered.\n";
system("pause");
break;
}
else
{
cout<<"Where do you want to seat? (<digits 1-8><letters a-z> sample 4B)\n";
cin>>seat;
cin.get();
ofstream outfile;
outfile.open("sample.txt", ios::out|ios::app);

outfile<<name1<<endl;
outfile<<seat<<endl;

system("pause");
system("cls");
outfile.close();
}
}
}
}
break;



case 4:
{
cout<<"Here are the list of the occupied seats\n";
ifstream input_file("sample.txt");
while ( !input_file.eof() )
{
getline(input_file, line_of_file);
if(input_file)
{
++lines_read;
if(lines_read % 2 == 0 )
cout << "Seat number -> "<< line_of_file<<endl;
}
else break;
}
}
system("pause");
system("cls");
break;


case 5:
{
string filename;
cout<<"Enter a name to be deleted\n";
cin>>filename;
std::string buffer[255];
std::ifstream infile1;
int a = 0;
infile1.open("sample.txt");

if(infile1.is_open()){
while (!infile1.eof()){
std::getline(std::cin, buffer[a]);
++a;
}
infile1.close();
}
for(; buffer[a] != filename; ++a);
{
buffer[a] = buffer[a+1] = "";
}
std::ofstream outfile1;

outfile1.open("Sample.txt", ios::trunc);

if(outfile1.is_open())
{
outfile1<<buffer;
outfile1.close();
}
}
break;

case 6:
cout<<"Goodbye!\n";
system("pause");
break;

default:
cout<<"Invalid";
system("cls");

break;
}
}

return 0;
}
Do you honestly write code like that? No offence meant; but by "wrap your code in [code] tags" I meant so that it was indented... Anyway;

Why are you doing it like that? There is a much shorter, quicker, more standard and more readable method, like the way I showed you.

It would be much easier if you did it the way I showed you. That places the whole file into buffer which is an array of strings big enough to hold the whole file. Then you read through the file, placing lines into the buffer. When you've done that, you find the appropriate line and delete it, and the next line, from the file. Then you rewrite the file, removing the contents of the original file, such that you are left with the same file but with those two lines missing.

Don't use system("pause") either. Use
std::cin.get();
which will do almost the same thing but in a better and more standard way. Using std::cin.get(); your program will wait until the enter key is pressed; which is preferable to using the operating system to do the same thing. It will take much longer and is in fact fairly insecure. What do you think would happen if someone put a file called pause.exe in the same directory as your own program? You would run it. Try not to use system(anything);. It's bad practice.
Read:
http://cplusplus.com/forum/articles/10515/
http://cplusplus.com/forum/articles/11153/
I am sorry sir. So i should place the whole file in an array of string?

Thank you for your comment sir..

I am trying to teach myself about c++

Thank you for helping me sir..

Can you teach me more about it sir? I will try my best to give my codes more clearer sir..
Last edited on
Topic archived. No new replies allowed.