#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
int main(){
char fileNam[80] = "c:\\test.txt";
string line;
char check;
int lNum;
long linPos;
ifstream InpFile;
//cout << "File to open: ";
//gets(fileNam); //get the name of the file from the user
InpFile.open(fileNam);
cout << "Which line do you want to delete?: ";
cin >> lNum ;
if (!InpFile) {
cerr << "Unable to open the file: " << fileNam << endl; //show an error if file not found
exit(1);
}
int lineNum = 0;
while (! InpFile.eof()){
getline (InpFile,line);
++lineNum;
if (lineNum == lNum)
linPos = InpFile.tellg();
cout << line << endl;
}
cout << "Pos: " << linPos << endl << "Total Lines: " << lineNum <<endl;
InpFile.close();
fstream outFile;
outFile.open(fileNam, ios::app);
if (!outFile) {
cerr << "Unable to open the file: " << fileNam << endl; //show an error if file not found
exit(1);
}
cout << outFile.tellg();
//outFile << "Delete this line";
//outFile.seekp (linPos);
//outFile.write ("",1);
lineNum = 0;
while (! outFile.eof()){
getline (outFile,line);
++lineNum;
cout << "line:" << line << lineNum << " " << lNum; //Own check for pointer location
if (lineNum == (lNum-1)){
outFile << "Delete this line";
}
}
return 0;
}
I want to replace the file in a .txt file. When I run this code, I can not see any change to the file. What am I doing wrong?