In my program, I need to open multiple files, and I wont know till after the
program execution how many of them (user will enter that value). So I am
using a vector of fstream. I am using fstream since I will need to write and
read from files, and I am using those files as binary files.
I made a sample of what's going on (below). Without using vector, everything
works fine, but with using vectors, something is going wrong somewhere!??
I compiled the code with MS VC6++, and it compiled without errors, but still
the piece of code with vectors wont give correct answer.
with MSV .net 2003, compiling the code with vector will give me error,
something about (no copy constructor available or copy constructor is
declared 'explicit'), is that the problem?
*********** First , without vector ********
fstream inoutData;
inoutData.open( "trial.txt" , ios::binary | ios::in | ios::out);
if(!inoutData)
{
ofstream newfile;
newfile.open("t rial.txt",ios:: binary);
newfile.clear() ;
newfile.close() ;
inoutData.clear ();
inoutData.open( "trial.txt" , ios::binary | ios::in | ios::out);
}
int p = 24;
inoutData.clear ();
inoutData.seekp (0);
inoutData.write (reinterpret_ca st<const char *>(&p), sizeof(p));
inoutData.seekg (0);
int q;
inoutData.read( reinterpret_cas t< char *>(&q), sizeof(q));
cout << q << endl;
*********** Second , with vector ********
vector<fstream> inoutData(1);
inoutData[0].open("trial.tx t", ios::binary | ios::in | ios::out);
if(!inoutData[0])
{
ofstream newfile;
newfile.open("t rial.txt",ios:: binary);
newfile.clear() ;
newfile.close() ;
inoutData[0].clear();
inoutData[0].open("trial.tx t", ios::binary | ios::in | ios::out);
}
int p = 24;
inoutData[0].clear();
inoutData[0].seekp(0);
inoutData[0].write(reinterp ret_cast<const char *>(&p), sizeof(p));
inoutData[0].seekg(0);
int q;
inoutData[0].read(reinterpr et_cast< char *>(&q), sizeof(q));
cout << q << endl;
--
Quotes from The Weather Man:
Robert Spritz: Do you know that the harder thing to do, and the right thing
to do, are usually the same thing? "Easy" doesn't enter into grown-up
life... to get anything of value, you have to sacrifice.
program execution how many of them (user will enter that value). So I am
using a vector of fstream. I am using fstream since I will need to write and
read from files, and I am using those files as binary files.
I made a sample of what's going on (below). Without using vector, everything
works fine, but with using vectors, something is going wrong somewhere!??
I compiled the code with MS VC6++, and it compiled without errors, but still
the piece of code with vectors wont give correct answer.
with MSV .net 2003, compiling the code with vector will give me error,
something about (no copy constructor available or copy constructor is
declared 'explicit'), is that the problem?
*********** First , without vector ********
fstream inoutData;
inoutData.open( "trial.txt" , ios::binary | ios::in | ios::out);
if(!inoutData)
{
ofstream newfile;
newfile.open("t rial.txt",ios:: binary);
newfile.clear() ;
newfile.close() ;
inoutData.clear ();
inoutData.open( "trial.txt" , ios::binary | ios::in | ios::out);
}
int p = 24;
inoutData.clear ();
inoutData.seekp (0);
inoutData.write (reinterpret_ca st<const char *>(&p), sizeof(p));
inoutData.seekg (0);
int q;
inoutData.read( reinterpret_cas t< char *>(&q), sizeof(q));
cout << q << endl;
*********** Second , with vector ********
vector<fstream> inoutData(1);
inoutData[0].open("trial.tx t", ios::binary | ios::in | ios::out);
if(!inoutData[0])
{
ofstream newfile;
newfile.open("t rial.txt",ios:: binary);
newfile.clear() ;
newfile.close() ;
inoutData[0].clear();
inoutData[0].open("trial.tx t", ios::binary | ios::in | ios::out);
}
int p = 24;
inoutData[0].clear();
inoutData[0].seekp(0);
inoutData[0].write(reinterp ret_cast<const char *>(&p), sizeof(p));
inoutData[0].seekg(0);
int q;
inoutData[0].read(reinterpr et_cast< char *>(&q), sizeof(q));
cout << q << endl;
--
Quotes from The Weather Man:
Robert Spritz: Do you know that the harder thing to do, and the right thing
to do, are usually the same thing? "Easy" doesn't enter into grown-up
life... to get anything of value, you have to sacrifice.
Comment