How to write and read to/from a text file simultaneosly ??

Pages: 12

you missed my post above where i said post your final program with code tags. Anyway, i thought it better to modify the program myself rather than continue with this long post. here is the program. Learn from this program and try to simplify this as its a big mess to understand...


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
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

class GiugnoBis
{
private:
	std::string f1_path;
	std::string f2_path;
	fstream f1;
	fstream f2;
public:

	GiugnoBis(string A, string B)
	{
		f1.open(A.c_str());
		f2.open(B.c_str());
		f1_path = A;
		f2_path = B;

	} // end of costructor

	~GiugnoBis()
	{

		f1.close();
		f2.close();
	}

void concatenate()
{
	fstream newfile;
	string aa;
	string bb;
	string c;

	newfile.open("c:\\newfile.txt", fstream::in | fstream::out | fstream::app);
	if(newfile.fail())
	{

		return;

	}
	while(!(f1.eof()&&f2.eof()))
	{
		getline(f1,aa);
		getline(f2,bb);
		c=aa+bb;
		newfile<< c.c_str() <<endl;

	}

	newfile.seekg(0, ios::beg);	//to the beginning again

	
	string dd;
	f1.close();
	f2.close();

	f1.open(f1_path.c_str(), fstream::out | fstream::app);
	if(f1.fail())
	{
		return;
	}
	while(!newfile.eof())
	{
		
		getline(newfile,dd);
		f1<<dd;
	}

	newfile.close();
	f1.close();
	

}//end of concatenate

void replaceWithCount()
{
	fstream lastfileA;
	string aaa;
	int counta=0;
	lastfileA.open("c:\\LASTFILEA.txt", fstream::out | fstream::app);
	
	f1.open(f1_path.c_str(), fstream::in);
	while(!f1.eof())
	{
		getline(f1,aaa);
		counta=aaa.size();
		lastfileA<<counta<<endl;
	}
	lastfileA.close();
	f1.close();

	int m=0;
	lastfileA.open("c:\\LASTFILEA.txt", fstream::in);
	f1.open(f1_path.c_str(), fstream::out | fstream::app);
	while(!lastfileA.eof())
	{
		lastfileA>>m;
		f1<<m<<endl;
	}
	f1.close();
	lastfileA.close();


	fstream lastfileB;
	string bbb;
	int countb=0;
	lastfileB.open("c:\\LASTFILEB.txt", fstream::out | fstream::app);
	
	f2.open(f2_path.c_str(), fstream::in);
	while(!f2.eof())
	{
		getline(f2,bbb);
		countb=bbb.size();
		lastfileB<<countb<<endl;
	}
	lastfileB.close();
	f2.close();


	int n=0;
	lastfileB.open("c:\\LASTFILEB.txt", fstream::in);
	f2.open(f2_path.c_str(), fstream::out | fstream::app);
	while(!lastfileB.eof())
	{
		lastfileB>>m;
		f2<<m<<endl;
	}
	lastfileB.close();
	f2.close();

} // end of replace with cout



};//end of class GiugnoBis

int main()
{
	GiugnoBis student("c:\\file1.txt","c:\\file2.txt");
	student.concatenate();
	student.replaceWithCount();
	return 0;
} 

Thank you writetonsharma! Now it's almost done :)
Topic archived. No new replies allowed.
Pages: 12