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("trial.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_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::out);
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::out);
}
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;
--
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. 9 5255
In article <UJ********************@comcast.com>, so***********@comcast.net says... 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.
A vector of fstream isn't allowed. Anything you put into
a vector must be assignable, and an fstream isn't -- the
assignment operator is private in std::ios_base, and
(unless I've missed something) none of its descendents
makes it public again.
You'll probably need to do something like an array of
(possibly smart) pointers to fstreams instead.
--
Later,
Jerry.
The universe is a figment of its own imagination.
Jerry coffin, I find your error in your answer that is not
std::ios_base but std::basic_ios, you know? 'cause the assignment
operator is public in std::ios_base, and is private¬ defined in
std::basic_ios, although std::ios_base is the base of std::basic_ios,
and std::basic_istream comes from std::basic_ios, std::fstream comes
from std::basic_istream,
I suppose that you might write error, don't you?
and, OK, I ignore another mistake: anything you put into a vector must
be assignable. It's not necessary, but copy constructor must be PUBLIC.
Tsubasa wrote: Jerry coffin, I find your error in your answer that is not std::ios_base but std::basic_ios, you know? 'cause the assignment operator is public in std::ios_base, and is private¬ defined in std::basic_ios, although std::ios_base is the base of std::basic_ios, and std::basic_istream comes from std::basic_ios, std::fstream comes from std::basic_istream, I suppose that you might write error, don't you?
From the standard:
27.4.2 Class ios_base
namespace std {
class ios_base {
public:
class failure;
typedef T1 fmtflags;
static const fmtflags boolalpha;
...
[lots of stuff snipped]
...
static bool sync_with_stdio(bool sync = true);
protected:
ios_base();
private:
// static int index; exposition only
// long* iarray; exposition only
// void** parray; exposition only
private:
ios_base(const ios_base&);
ios_base& operator=(const ios_base&);
};
Note the private copy constructor and assignment operator, each of which by
itself makes ios_base and classes derived therefrom unsuitable for use in
std::vector.
Best
Kai-Uwe Bux
Someonekicked wrote: 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("trial.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_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::out); 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::out); }
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;
IAW C++ standard, iostream and derived class fstream are not assignable
or copiable, which means you can't use it as a concrete object in your
vector.
However, you could create a vector of pointer to fstream.
I would recommend using a smart pointer like boost::shared_ptr
See following link of list of smart pointers: http://axter.com/smartptr/other_smart_pointers.htm
And see following link for example usage of smart pointers: http://axter.com/smartptr/index.htm
In article <1145181037.707901.163820
@i40g2000cwc.googlegroups.com>, ch**********@gmail.com
says... Jerry coffin, I find your error in your answer that is not std::ios_base but std::basic_ios, you know? 'cause the assignment operator is public in std::ios_base, and is private¬ defined in std::basic_ios, although std::ios_base is the base of std::basic_ios, and std::basic_istream comes from std::basic_ios, std::fstream comes from std::basic_istream, I suppose that you might write error, don't you?
I was pretty sure ios_base declared them private, but
maybe my memory has failed me.
It's more or less beside the point which base class
causes it though. Creating a vector of X requires that X
be copyable and assignable. Streams are neither copyable
nor assignable, so you can't create vectors of streams.
--
Later,
Jerry.
The universe is a figment of its own imagination.
In article <UJ********************@comcast.com>,
"Someonekicked" <so***********@comcast.net> wrote: 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.
In addition to the good answers you've already received, I'd like to
add: The committee is seriously considering changes to the C++ language
and standard library which allow your "with vector" example to work
exactly as you coded it.
Reference: http://www.open-std.org/jtc1/sc22/wg...005/n1771.html
-Howard
In my opinion, the element type used for vector should be a value-like
type. And the fstream, manages a file handler, cannot be copy and
assign.
In article <11**********************@i39g2000cwa.googlegroups .com>,
"Tom" <ye*****@gmail.com> wrote: In my opinion, the element type used for vector should be a value-like type. And the fstream, manages a file handler, cannot be copy and assign.
No worries.
vector::value_type will continue to be value-like. And fstream will
continue to be non-copyable and non-assignable. Move semantics
introduces a powerful new way of looking at things that compliments your
existing views, and allows things to be more efficient, not less.
Consider:
std::fstream
open_the_file_my_way(const char* filename)
{
std::fstream result ...
return result;
}
....
void test ()
{
std::fstream my_file = open_the_file_my_way("the file");
...
}
There is no logic error above. Every time the fstream changes
locations, the old location is about to be destroyed. The underlying
file handle can be passed from fstream to fstream, and no one is the
wiser. You get what today seems like copy behavior, but really is move
behavior.
At the same time, you will still be prohibited from copying or assigning
fstreams:
void test ()
{
std::fstream my_file = open_the_file_my_way("the file"); // ok
...
std::fstream another_file = my_file; // won't compile
}
Here if the copy succeeded you would have two streams running around
pointing to the same file. I think we're all agreed that would be a bad
thing. This will continue to be enforced at compile time.
*********** Second , with vector ********
vector<fstream> inoutData(1); inoutData[0].open("trial.txt", ios::binary | ios::in | ios::out); 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::out); }
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;
In the OP's code, the fstream is never copied, even while it is inside
the vector. If the OP attempts an operation with the vector<fstream>
that would *visibly* copy an fstream, the compiler will reject it. E.g.
vector<fstream> inoutData(1); // ok
fstream f = inoutData[0]; // compile time error
inoutData.push_back(fstream("another file")); // ok
In the latest example, no one has a reference to that temporary being
put into the vector. There's no danger of two fstreams pointing to the
same file.
fstream f;
inoutData.push_back(f); // compile time error
If this compiled, then both f and inoutData.back() would point to the
same file (bad!).
-Howard This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Marc Schellens |
last post by:
I have (or better had) a class wich contains
a fstream (as a private member).
I made a vector of this class.
When I resize() the vector I got several error messages,
about some stuff being...
|
by: Gaurav |
last post by:
Hello
I have a program that basically inverts the contents of files except
first line.
It compiles fine but gives me core dump on running. If i comment
temp.clear() it runs fine, but i need...
|
by: Duncan |
last post by:
I need to populate a vector with the following struct details:\
struct group
{
string groupname;
int gid;
list<string> members;
};
the text file which this is to read from is in the...
|
by: Macca |
last post by:
Hi,
I have been using <fstream.h> in stdafx.h,(i'm using MFC) to output to
text files. I have now started to use vectors and when i added
#include <vector>
using namespace std;
to...
|
by: kittykat |
last post by:
Hello,
I am writing a program that will read each line of a file into a vector of
vectors. This data will then be analysed. Here is what i have done:
typedef vector<string> lines;
......
|
by: Dave Reid |
last post by:
Hi everyone...
I'm pretty much a newbie C++ user, and I've run into a problem.
I'm trying to read in a large text file, and then do manipulations on
it. I can read it into a large 2-dimensional...
|
by: Eric Lilja |
last post by:
Hello, consider the following assignment and my code for it:
/*
Write a program that does the following:
* Integer numbers shall be read from a textfile and stored in a
std::vector.
The name...
|
by: arnuld |
last post by:
This is the partial-program i wrote, as usual, i ran into problems
halfway:
/* C++ Primer - 4/e
*
* Exercise 8.9
* STATEMENT:
* write a function to open a file for input and then read...
|
by: D. Susman |
last post by:
Hi,
In the code snippet below, I am trying to read the contents of a file
into a vector. However I can't get it compiled. I am using Sunstudio
which compiles C++ using cc.
Here is the code:...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
| | |