Reputation: 13
I've been having such a difficult time trying to "delete" a line from a text file and have looked everywhere. I'm a beginner with C++ so I have no idea how to use vectors yet.
When I run this program, it will not write anything to the tempFile. Am I missing something?
string deleteLine, line;
ifstream addressFile;
addressFile.open("AddressBook.txt");
ofstream tempFile;
tempFile.open("temp.txt");
int i = 1;
while (getline(addressFile, line)) {
cout << (i++) << " " << line << endl;
}
cout << "Which line do you want to remove?" << endl;
cin >> deleteLine;
// GOOD UP TO HERE
while (getline(addressFile, line)) {
if (line != deleteLine); {
tempFile << line << endl;
}
}
tempFile.close();
addressFile.close();
remove ("AddressBook.txt");
rename ("temp.txt", "AddressBook.txt");
cout << endl << endl;
Update - This is the current code I have and still not writing to tempFile
ifstream addressFile;
addressFile.open("AddressBook.txt");
ofstream tempFile;
tempFile.open("temp.txt");
string line;
int lineNum, i;
i = 1;
while (getline(addressFile, line)) {
cout << (i++) << " " << line << endl;
}
addressFile.seekg(0);
cout << "Which line number do you want to remove?" << endl;
cin >> lineNum;
// GOOD UP TO HERE
i = 1;
while (getline(addressFile, line)) {
if (i++ != lineNum) {
tempFile << line << endl;
}
}
tempFile.close();
addressFile.close();
//remove ("AddressBook.txt");
//rename ("temp.txt", "AddressBook.txt");
cout << endl << endl;
Upvotes: 0
Views: 378
Reputation: 595702
In your 1st while
loop, you are reading lines from addressFile
until the end of the file is reached. Then, your 2nd while
loop is trying to read lines from the same file again, but there is nothing left to read.
You need to seek addressFile
back to the beginning of the file before you can re-read the lines again. Add this statement between your 2 loops:
addressFile.seekg(0);
With that fixed, you still have some other problems.
In your 2nd loop, your if
has an erroneous ;
on it, so you are not going to actually ignore any lines, you will write all of the lines to tempFile
.
Also, you should consider asking the user for a line number to ignore, not an actual line of text. And validate the user's input before using it.
You should also verify that the files are being opened successfully.
Try this:
ifstream addressFile("AddressBook.txt");
if (!addressFile.is_open()) {
cerr << "Can't open AddressBook.txt" << endl;
return;
}
ofstream tempFile("temp.txt");
if (!tempFile.is_open()) {
cerr << "Can't create temp.txt" << endl;
return;
}
string line;
int lineNum, i;
i = 1;
while (getline(addressFile, line)) {
cout << (i++) << " " << line << endl;
}
addressFile.seekg(0);
do {
cout << "Which line number do you want to remove?" << endl;
if (cin >> lineNum) {
if (lineNum > 0) break;
cerr << "Please choose a number greater than zero" << endl;
}
else {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cerr << "Invalid number, try again" << endl;
}
}
while (true);
i = 1;
while (getline(addressFile, line)) {
if (i != lineNum) {
tempFile << line << endl;
}
++i;
}
tempFile.close();
addressFile.close();
remove ("AddressBook.txt");
rename ("temp.txt", "AddressBook.txt");
cout << endl << endl;
Upvotes: 2