editing a .txt file

Hi,
I am trying to edit a text file through c++ and this is what I tried but don't know why its not working

here's my class
1
2
3
4
5
6
7
class A{
public:
int a;
char b[15];
char c[15];
};
A q;


the test.txt file
1
2
3
4
1 one number1
2 two number2
3 three number3
4 four number4


and the code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
fstream fio;
fio.open("text.txt",ios::in|ios::ate);

fio.seekg(0);//putting pointer to 0
long pos;

while(!fio.eof())//untill the file ends
   {
   pos=fio.tellg();//tell the position of stuff
   fio>>q.a>>q.b>>q.c;//taking values
   if(q.a==3)//if the value is 3
    
 {
      fio.seekg(pos);//put the cursor to that element
      fout<<10<<' '<<"new name"<<' '<<"new type"<<'\n'; 

 }
   }


What am I doing wrong? please help
Last edited on
If you want to write to file, open it with std::ios::out flag set.

Just want to warn you that it will not work as you want anyway. Text files are generally not editable, they should be just overwritten on save.
MiiNiPaa wrote:
Text files are generally not editable, they should be just overwritten on save.


Can you tell me how can I do that?
1) Read everything from file.
2) Change read data as needed.
3) Write everything to file discarding old content.
Thanks, this is what I did and its working :D

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
fstream fio;
fio.open("test.txt",ios::in|ios::ate);
fio.seekg(0);

int i=0;
while(fio>>a[i].b>>a[i].c>>a[i].d)
i++;

for(int j=0;j<i;j++)
{
if(a[j].b==2)
	{
	a[j].b=10;
	strcpy(a[j].c,"Hello");
	strcpy(a[j].d,"World");
	}
}

ofstream fout;
fout.open("temp.txt");

for(j=0;j<i;j++)
fout<<a[j].b<<' '<<a[j].c<<' '<<a[j].d<<'\n';

remove("test.txt");  //delete test file
rename("temp.txt","test.txt");      //rename the temp to text 


is there any scope of improvement in this code?
1) If you are going to only read to file, use ifstream.
2) There is no need for ate flag
3) Consider opening file in stream constructor.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
std::ifstream fin("test.txt");

int count;
while( fin >> a[i].b >> a[i].c >> a[i].d)
    ++count;

fin.close(); //We want to close file explicitely, because we need to manipulate it later

for(int i = 0; i < count; ++i)
    if(a[i].b == 2)
        //...

std::ofstream fout("temp.txt");
for(int i = 0; i < count; ++i)
    fout << a[i].b<<' '<<a[i].c<<' '<<a[i].d<<'\n';
fout.close(); //Ditto

std::remove("test.txt");  //delete test file
std::rename("temp.txt","test.txt");      //rename the temp to text 

MiiNiPaa wrote:
There is no need for ate flag


But why? won't ios::out (default) delete the all old records?
Last edited on
won't ios::out (default) delete the all old records?
If you do not specify out flag, it will not be used at all. fstream do not have implicit "always on" flag like istream (ios::in) and ostream (ios::out) do, it just have a default ios::in | ios::out value, which will be replaced completely by any user-defined one. And std::ifstream do not even have out as default.
Thanks...
Topic archived. No new replies allowed.