473,888 Members | 1,660 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

vector of fstream, does not work?

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.
Apr 16 '06 #1
9 5296
In article <UJ************ ********@comcas t.com>,
so***********@c omcast.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.
Apr 16 '06 #2
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&not defined in
std::basic_ios, although std::ios_base is the base of std::basic_ios,
and std::basic_istr eam comes from std::basic_ios, std::fstream comes
from std::basic_istr eam,
I suppose that you might write error, don't you?

Apr 16 '06 #3
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.

Apr 16 '06 #4
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&not defined in
std::basic_ios, although std::ios_base is the base of std::basic_ios,
and std::basic_istr eam comes from std::basic_ios, std::fstream comes
from std::basic_istr eam,
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
Apr 16 '06 #5

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("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;


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_p tr
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

Apr 16 '06 #6
In article <1145181037.707 901.163820
@i40g2000cwc.go oglegroups.com> , ch**********@gm ail.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&not defined in
std::basic_ios, although std::ios_base is the base of std::basic_ios,
and std::basic_istr eam comes from std::basic_ios, std::fstream comes
from std::basic_istr eam,
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.
Apr 16 '06 #7
In article <UJ************ ********@comcas t.com>,
"Someonekic ked" <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
Apr 16 '06 #8
Tom
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.

Apr 18 '06 #9
In article <11************ **********@i39g 2000cwa.googleg roups.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_t ype 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_m y_way(const char* filename)
{
std::fstream result ...
return result;
}

....

void test ()
{
std::fstream my_file = open_the_file_m y_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_m y_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.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;


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("a nother 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
Apr 18 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3436
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 private int that context. Is this gcc (3.2) specific or is there a 'official' reason for this? thanks, marc
12
5074
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 it to clear the temp vector for each file. ********************* code *******************
3
4772
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 following form:
1
2159
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 stdafx.h, I found that to compile i had to change <fstream.h> to <fstream> to get it to compile.
6
1910
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; ... vector<lines> SourceVector; string one_line;
6
8154
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 character array, but I'd really like to read it into a vector of strings. Here's how I'm doing the read into the char array: int main() { string str1("booger"); string str2("test");
3
2178
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 of the input file is to be given on the command line. * All negative values in the vector shall then be replaced with their positive counterpart, using the standard algorithm for_each.
15
5875
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 its coontents into a vector of strings, storinig each line as separate
7
3717
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: #include <fstream> #include <vector>
0
11176
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10772
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10880
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10434
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7988
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6012
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4638
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4241
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3246
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.