iostream->fstream

Jan 15, 2012 at 4:34pm
http://www.cplusplus.com/reference/iostream/
According to diagram on page above, fstream is part of iostream (or something like that). Does that mean there is some way of using iostream to read and write into files without including fstream? I know this sounds silly but I am about to enter a programming contest that may improve my chances of getting into school I want to attend and there are only few libraries allowed and fstream is not one of them. There is always a possibility of using C way of file i/o but I rarely used it in past so I would prefer something that I am more experienced with. Thanks
Jan 15, 2012 at 4:39pm
I think the diagram was showing inheritance. So... sorry, but while I suppose it's possible, it would not be in any way straightforward and would probably end with you inheriting from iostream and writing your own read/write code.

EDIT: If the contest explicitly forbids file streams, then planning to use C file I/O might not be a good idea, as (I'm guessing) they didn't want you to use file I/O. If it says you can use the standard library, though, and doesn't explicitly forbid the use of certain modules, then using file streams should be okay. That's my 2¢.

-Albatross
Last edited on Jan 15, 2012 at 4:43pm
Jan 15, 2012 at 4:40pm
Not sure if you can read/write a file in c++ without fstream. But here's a link in C, by using stdio.h
http://en.wikipedia.org/wiki/Cstdio
Last edited on Jan 15, 2012 at 4:40pm
Jan 15, 2012 at 4:41pm
fstream inherits from iostream. This means that fstream starts with iostream and then adds things to it. The extra things it adds involve files, so the answer is no; iostream does not enable you to manipulate files in the way that fstream does. Those abilities are what fstream adds to iostream.

That said, if you're allowed to use streambuf and filebuf, you could rewrite that functionality yourself. That would probably be more hassle than just using the C style functions provided in <cstdio>, though.
Jan 15, 2012 at 5:04pm
Oh, ok thanks anyway.
Jan 15, 2012 at 5:09pm
> iostream does not enable you to manipulate files in the way that fstream does.
> Those abilities are what fstream adds to iostream.

std::streambuf does not enable you to manipulate files in the way that std::filebuf does.
Those abilities are what std::filebuf adds to std::streambuf.

std::ifstream, std::ofstream, and std::fstream are just (very) thin facades. They add very little to their respective base classes - a constructor which creates the std::filebuf and open() which opens the filebuf.

You still need to #include <fsteam> - that has the definition of the std::filebuf class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <fstream>
#include <string>

int main()
{
    std::filebuf fbuf ;
    fbuf.open( __FILE__, std::ios_base::in ) ;
    {
        // read this file line by line and write each line to stdout
        auto oldbuf = std::cin.rdbuf( &fbuf ) ;
        std::string line ;
        while( std::getline( std::cin, line ) ) std::cout << line << '\n' ;
        std::cin.rdbuf(oldbuf) ;
    }

    fbuf.close() ;
    fbuf.open( "out.txt", std::ios_base::out ) ;
    {
        // read lines from stdin and write each line to out.txt
        auto oldbuf = std::cout.rdbuf( &fbuf ) ;
        std::string line ;
        while( std::getline( std::cin, line ) ) std::cout << line << '\n' ;
        std::cout.rdbuf(oldbuf) ;
    }
}

Last edited on Jan 15, 2012 at 5:10pm
Topic archived. No new replies allowed.