You are here:

C++/How to declare array of istream

Advertisement


Question
Hi,

I want to open an array of input file-streams (ifstream) pointing to the same file.

I tried using the following but none of them work.

[1] filebuf fb;
fb.open ("test.txt",ios::in);
std::fstream my_s[2] ={ &fb,&fb};

//error--> `std::filebuf*' to non-scalar type `std::basic_istream<char, std::char_traits<char> >

[2]my_s[0].init(&fb);
my_s[1].init(&fb); //same error....

Best Regards
Gaurav Kapoor


Answer
My first reaction is that there seems little point in what you are doing. All the instances of the file you open using the techniques you are trying share the same filebuf, so what is the point of using more than one std::fstream object (or std::ifstream object for that matter), unless you wish to parse the file differently at the same time using different locale and formatting options?

If you wish to read from the file several times with each instance reading different portions of the file then I suggest you create an array or other collection (such as std::vector) of file streams and open each one using the same file name string. Then each instance will have its own independent filebuf instance (this of course presumes that you are allowed many readers of one file simultaneously):

   std::fstream my_s[2];
   my_s[0].open("thefile.dat", std::ios::in);
   my_s[1].open("thefile.dat", std::ios::in);

Of course I am not trying to solve your problem so maybe you have a perfectly valid reason for wanting do this. So if you really wish to re-use the same filebuf instance for all fstream instances then you have to use the base class version of the rdbuf function. That is the one declared by std::basic_ios. Unfortunately concrete stream classes such as std::fstream declare their own versions of rdbuf hiding the std::basic_ios implementation so you have to explicitly qualify the call with the base class name, thus:

   my_s[0].std::basic_ios<char>::rdbuf(&fb);
   my_s[1].std::basic_ios<char>::rdbuf(&fb);

At this point I would like to recommend a good book on standard C++ IOStreams, which also covers C++ locales: "Standard C++ IOStreams and Locales" by Langer and Kreft. If you are getting into using streams and stream buffers and other related topics then a good reference would be of use.

Finally I present below some code that demonstrates the differences of opening the same file on several streams and opening a file on a filebuf which is then shared by several streams.

I define two functions BufPerStream and BufSharedByAllStreams that open for reading the file test.txt, my version of which contains the text (the word FILE is the first item in the file):

FILE TEST.TXT
LINE 2
LINE 3
LINE 4
LINE 5

Both functions use a number of std::fstreams determined by the constant NumberOfStreams, which I have set to 10. BufPerStream opens the file for each stream using the stream's open operation. BufSharedByAllStreams however opens the file on a std::filebuf and then associates this filebuf with each stream as shown above. Both functions then go on to read and display information from each stream by calling a helper function ReadAndDisplayStreams, which reads and displays on std::cout one string from each stream in turn until they are no longer in a good state.

The output from BufPerStream is as follows:

Using one std::filebuf for each stream:
Stream 0: FILE
Stream 1: FILE
Stream 2: FILE
Stream 3: FILE
Stream 4: FILE
Stream 5: FILE
Stream 6: FILE
Stream 7: FILE
Stream 8: FILE
Stream 9: FILE
Stream 0: TEST.TXT
Stream 1: TEST.TXT
Stream 2: TEST.TXT
Stream 3: TEST.TXT
Stream 4: TEST.TXT
Stream 5: TEST.TXT
Stream 6: TEST.TXT
Stream 7: TEST.TXT
Stream 8: TEST.TXT
Stream 9: TEST.TXT
Stream 0: LINE
Stream 1: LINE
Stream 2: LINE
Stream 3: LINE
Stream 4: LINE
Stream 5: LINE
Stream 6: LINE
Stream 7: LINE
Stream 8: LINE
Stream 9: LINE
Stream 0: 2
Stream 1: 2
Stream 2: 2
Stream 3: 2
Stream 4: 2
Stream 5: 2
Stream 6: 2
Stream 7: 2
Stream 8: 2
Stream 9: 2
Stream 0: LINE
Stream 1: LINE
Stream 2: LINE
Stream 3: LINE
Stream 4: LINE
Stream 5: LINE
Stream 6: LINE
Stream 7: LINE
Stream 8: LINE
Stream 9: LINE
Stream 0: 3
Stream 1: 3
Stream 2: 3
Stream 3: 3
Stream 4: 3
Stream 5: 3
Stream 6: 3
Stream 7: 3
Stream 8: 3
Stream 9: 3
Stream 0: LINE
Stream 1: LINE
Stream 2: LINE
Stream 3: LINE
Stream 4: LINE
Stream 5: LINE
Stream 6: LINE
Stream 7: LINE
Stream 8: LINE
Stream 9: LINE
Stream 0: 4
Stream 1: 4
Stream 2: 4
Stream 3: 4
Stream 4: 4
Stream 5: 4
Stream 6: 4
Stream 7: 4
Stream 8: 4
Stream 9: 4
Stream 0: LINE
Stream 1: LINE
Stream 2: LINE
Stream 3: LINE
Stream 4: LINE
Stream 5: LINE
Stream 6: LINE
Stream 7: LINE
Stream 8: LINE
Stream 9: LINE
Stream 0: 5
Stream 1: 5
Stream 2: 5
Stream 3: 5
Stream 4: 5
Stream 5: 5
Stream 6: 5
Stream 7: 5
Stream 8: 5
Stream 9: 5

Which shows that each stream has its own independent filebuf and each progresses independly of the others.

The output from BufSharedByAllStreams on the other hand is like so:

Using shared std::filebuf for all streams:
Stream 0: FILE
Stream 1: TEST.TXT
Stream 2: LINE
Stream 3: 2
Stream 4: LINE
Stream 5: 3
Stream 6: LINE
Stream 7: 4
Stream 8: LINE
Stream 9: 5

Showing that all streams share one filebuf and therefore each stream progresses in step with the others, and you might just as well have used one stream.

The code is as follows:

#include <fstream>
#include <iostream>
#include <string>

char const    FileName[] = "test.txt";
size_t const  NumberOfStreams(10);

void ReadAndDisplayStreams( std::fstream streams[NumberOfStreams] )
{
   bool ok(true);
   while ( ok )
     {
       for ( int i(0); i<NumberOfStreams; ++i )
         {
         std::string text;
         streams[i] >> text;
         ok = streams[i].good();
         if ( ok )
         {
         std::cout << "Stream " << i << ": " << text << "\n";
         }
         }
     }
}

void BufPerStream()
{
   std::fstream streams[NumberOfStreams];
   for ( int i(0); i<NumberOfStreams; ++i )
     {
       streams[i].open(FileName, std::ios::in);
     }
   std::cout << "Using one std::filebuf for each stream:\n";
   ReadAndDisplayStreams(streams);
}

void BufSharedByAllStreams()
{
   std::filebuf fb;
   fb.open (FileName,std::ios::in);
   std::fstream streams[NumberOfStreams];
   for ( int i(0); i<NumberOfStreams; ++i )
     {
       streams[i].std::basic_ios<char>::rdbuf(&fb);
     }

   std::cout << "Using shared std::filebuf for all streams:\n";
   ReadAndDisplayStreams(streams);
}

int main()
{
   BufPerStream();
   std::cout << '\n';
   BufSharedByAllStreams();
   std::cout << std::endl;
}

Please note that the above code is for illustration purposes only and is not production quality. It was build and run using Microsoft Visual C++ 8.0 under the 2005 edition of Visual Studio and using g++ 4.0.2 built and run under SuSE Linux 10.0.

Hope this is of use to you.  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Ralph McArdell

Expertise

I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.

Experience

My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.

Education/Credentials

©2016 About.com. All rights reserved.