Trying to replace a line from a file

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
#include <iostream>
#include <fstream>
#include <string>
using namespace 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?
Topic archived. No new replies allowed.