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

Pages: 12
Hi everybody!
I i'm trying to open a file (NEWFILE).Then i have to get the lines from two other files (FILE1 and FILE2) and write this lines on NEWFILE. After this i must write the content of NEWFILE on FILE1. So i need to write and read simultaneosly, but my compiler dosen't do this.Does anybody know how can get through this ?
THANK YOU!
Do you know anything about <Ofstream> ?
You mean #include<fstream> ??? The standard library fstream.
Last edited on
How do you know that your compiler doesn't read and write simultaneously?
Why don't you just open FILE1, FILE2 and NEWFILE and store the information you need from each one into variables? Then write out the information however you like.
I think the problem which he is facing is, he is not closing NEWFILE after writing to it and straightaway trying to read it which will not be possible.
The program that i'm writting is a little bit more complex , all these operations that i'm doing are included in a class.But i always close the NEWFILE. The problem is that i can't close the other two files wich are opened in the constructor.
If you have time here is the code. The member function "concatenate" has to read the lines from to files and write them to the first one. The member fuction "replaceWithCount" must read from two files and write to each one the number of characters in each line.The costructor has two string parameters wich represent the name of two files.THE PROBLEM IS DOESN'T WRITE ON FILE 1 and IT DOESN'T WRITE on both files the number of characters of their lines.




#include <iostream>
#include <string>
#include <fstream>
using namespace std;

class GiugnoBis
{
private:
fstream f1;
fstream f2;
public:
GiugnoBis(string A, string B)
{
f1.open(A);
f2.open(B);

} // end of costructor


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

newfile.open("NEWFILE.txt");
while(!(f1.eof()&&f2.eof()))
{
getline(f1,aa);
getline(f2,bb);
c=aa+bb;
newfile<<c<<endl;

}
newfile.close();

newfile.open("NEWFILE.txt");
string dd;

while(!newfile.eof())
{

getline(newfile,dd);
f1<<dd;
}
newfile.close();

}//end of concatenate

void replaceWithCount()
{
fstream lastfileA;
string aaa;
int counta=0;
lastfileA.open("LASTFILEA.txt");
while(!f1.eof())
{
getline(f1,aaa);
counta=aaa.size();
lastfileA<<counta<<endl;
}
lastfileA.close();
int m=0;
lastfileA.open("LASTFILEA.txt");
while(!lastfileA.eof())
{
lastfileA>>m;
f1<<m<<endl;
}

fstream lastfileB;
string bbb;
int countb=0;
lastfileB.open("LASTFILEB.txt");
while(!f2.eof())
{
getline(f2,bbb);
countb=bbb.size();
lastfileB<<countb<<endl;
}
lastfileB.close();
int n=0;
lastfileB.open("LASTFILEB.txt");
while(!lastfileB.eof())
{
lastfileB>>m;
f2<<m<<endl;
}
lastfileB.close();
} // end of replace with cout



};//end of class GiugnoBis

int main()
{
GiugnoBis student("FILEONENAME.txt","FILETWONAME.txt");
student.concatenate();
student.replaceWithCount();
return 0;
}
Last edited on
always use code tags.

The first suggestion I will give you when using many file open/close operations is, use full paths. And i think this is the problem with your program. This way the files can be created in any directory which is a current directory. so you will create in one and try to read from other.

So give a full path, like "c:\\newfile.txt"

see if this solves your problem.
I did but i took off the paths so it's more simple for you to read it. When i tried it i used the paths of the files as file name with double slashes "\\".
while( !( f1.eof() || f2.eof() ) ) { ... }
I'm on my phone, sorry for the short reply.
Last edited on
Thank you for your replies but this didn't work too.It started to write on NEWFILE.txt infinitly.
which compiler you are using?

in case its VS, you dont have to give the default second argument for open(). default has fstream::in | fstream::out

which becomes a "r+" and this fails if the file is not already existing. I didn't go deep to know why they are doing this.

so you have to explicitly say:

newfile.open("file_path", fstream::in | fstream::out | fstream::app);

see if this works for you.
Ok, thank you.I'll try.
Sorry, i just tried it.It didn't work too.
could you post your exact final program with code tags. This is getting funny.
and which compiler you are using? I hope this is not turbo c? hahaha..
Visual Studio 2010.
final program ?
sorry i don't understand, whatdo you mean by final program?
Pages: 12