S
Someonekicked
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:ut);
if(!inoutData)
{
ofstream newfile;
newfile.open("trial.txt",ios::binary);
newfile.clear();
newfile.close();
inoutData.clear();
inoutData.open("trial.txt", ios::binary | ios::in | ios:ut);
}
int p = 24;
inoutData.clear();
inoutData.seekp(0);
inoutData.write(reinterpret_cast<const char *>(&p), sizeof(p));
inoutData.seekg(0);
int q;
inoutData.read(reinterpret_cast< char *>(&q), sizeof(q));
cout << q << endl;
*********** Second , with vector ********
vector<fstream> inoutData(1);
inoutData[0].open("trial.txt", ios::binary | ios::in | ios:ut);
if(!inoutData[0])
{
ofstream newfile;
newfile.open("trial.txt",ios::binary);
newfile.clear();
newfile.close();
inoutData[0].clear();
inoutData[0].open("trial.txt", ios::binary | ios::in | ios:ut);
}
int p = 24;
inoutData[0].clear();
inoutData[0].seekp(0);
inoutData[0].write(reinterpret_cast<const char *>(&p), sizeof(p));
inoutData[0].seekg(0);
int q;
inoutData[0].read(reinterpret_cast< char *>(&q), sizeof(q));
cout << q << endl;
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:ut);
if(!inoutData)
{
ofstream newfile;
newfile.open("trial.txt",ios::binary);
newfile.clear();
newfile.close();
inoutData.clear();
inoutData.open("trial.txt", ios::binary | ios::in | ios:ut);
}
int p = 24;
inoutData.clear();
inoutData.seekp(0);
inoutData.write(reinterpret_cast<const char *>(&p), sizeof(p));
inoutData.seekg(0);
int q;
inoutData.read(reinterpret_cast< char *>(&q), sizeof(q));
cout << q << endl;
*********** Second , with vector ********
vector<fstream> inoutData(1);
inoutData[0].open("trial.txt", ios::binary | ios::in | ios:ut);
if(!inoutData[0])
{
ofstream newfile;
newfile.open("trial.txt",ios::binary);
newfile.clear();
newfile.close();
inoutData[0].clear();
inoutData[0].open("trial.txt", ios::binary | ios::in | ios:ut);
}
int p = 24;
inoutData[0].clear();
inoutData[0].seekp(0);
inoutData[0].write(reinterpret_cast<const char *>(&p), sizeof(p));
inoutData[0].seekg(0);
int q;
inoutData[0].read(reinterpret_cast< char *>(&q), sizeof(q));
cout << q << endl;