Problems

Started by
13 comments, last by Drew_Benton 19 years, 4 months ago
err, i got it to work and made it look neater, check it out.

#include "xtream.hpp"using cpp::xtream;int main(){    xtream xio("xioTest.txt", cpp::file);        int i = 3;        xio << i << cpp::set_console() >> i << i;    xio.to_file("xioTest.txt", std::ios::in, std::ios::out | std::ios::app);    xio >> i;    xio.to_console();    xio << i;        while (std::cin.get() != '\n') ;    std::cin.get();    return 0;}    


#ifndef LOCK_xtream_Hpp#define LOCK_xtream_Hpp#include <iostream>#include <fstream>#include <sstream>#include <cassert>namespace cpp{    enum iostate { console, file, string };    class xtream    {    public:                xtream(iostate state = console);        xtream(const char *str, iostate state = string,         		std::ios::openmode is = std::ios::in,           		std::ios::openmode os = std::ios::out);                bool to_console();        bool to_file(const char *str,         				std::ios::openmode is = std::ios::in,        				std::ios::openmode os = std::ios::out);        bool to_string(const char *str);                friend xtream &operator>>(xtream &xio, int &val);        friend xtream &operator<<(xtream &xio, const int &val);        	    private:                std::istream *istrm;        std::ostream *ostrm;                iostate m_state;    };        struct state    {        iostate m_state;        char str[255];        std::ios::openmode is, os;    };        const state set_console();    const state set_file(const char *str, std::ios::openmode is = std::ios::in,        									std::ios::openmode os = std::ios::out);    const state set_string(const char *str);          xtream &operator>>(xtream &xio, const state &res);    xtream &operator<<(xtream &xio, const state &res);           }#endif


#include "xtream.hpp"cpp::xtream::xtream(iostate state)	: m_state(state){    if (m_state == console)    {    	istrm = &std::cin;    	ostrm = &std::cout;   	}   	   	else if (m_state == file)   	{   		istrm = new std::ifstream;   		ostrm = new std::ofstream;    }  		    else if (m_state == string)    {    	istrm = new std::istringstream;    	ostrm = new std::ostringstream;    }   	} cpp::xtream::xtream(const char *str, iostate state, std::ios::openmode is,           												std::ios::openmode os)	: m_state(state){    assert(m_state == file || m_state == string);       	if (m_state == file)   	{   		istrm = new std::ifstream(str, is);   		ostrm = new std::ofstream(str, os);    }  		    else if (m_state == string)    {    	istrm = new std::istringstream(str);    	ostrm = new std::ostringstream(str);   	}   	} bool cpp::xtream::to_console(){    if (m_state != console)    {        if (m_state == file)        {            dynamic_cast<std::ifstream*>(istrm)->close();            dynamic_cast<std::ofstream*>(ostrm)->close();        }                    delete istrm;        delete ostrm;    }        m_state = console;        istrm = &std::cin;    ostrm = &std::cout;        return (istrm && ostrm) ? true : false;}  bool cpp::xtream::to_file(const char *str, std::ios::openmode is, std::ios::openmode os){    if (m_state != console)    {        if (m_state == file)        {            dynamic_cast<std::ifstream*>(istrm)->close();            dynamic_cast<std::ofstream*>(ostrm)->close();        }                    delete istrm;        delete ostrm;    }        m_state = file;        istrm = new std::ifstream(str, is);    ostrm = new std::ofstream(str, os);        return (istrm && ostrm) ? true : false;}bool cpp::xtream::to_string(const char *str){    if (m_state != console)    {        if (m_state == file)        {            dynamic_cast<std::ifstream*>(istrm)->close();            dynamic_cast<std::ofstream*>(ostrm)->close();        }                    delete istrm;        delete ostrm;    }        m_state = string;        istrm = new std::istringstream(str);    ostrm = new std::ostringstream(str);        return (istrm && ostrm) ? true : false;}       cpp::xtream &cpp::operator>>(cpp::xtream &xio, int &val){    *(xio.istrm) >> val;        return xio;} cpp::xtream &cpp::operator<<(cpp::xtream &xio, const int &val){    *(xio.ostrm) << val;        return xio;}const cpp::state cpp::set_console(){    state res = { console };        return res;}        const cpp::state cpp::set_file(const char *str, std::ios::openmode is,        										std::ios::openmode os){    state res;        res.m_state = file;    strcpy(res.str, str);    res.is = is;    res.os = os;        return res;}    const cpp::state cpp::set_string(const char *str){    state res;        res.m_state = string;    strcpy(res.str, str);        return res;}cpp::xtream &cpp::operator>>(cpp::xtream &xio, const cpp::state &res){    switch (res.m_state)    {        case cpp::console:            xio.to_console();            break;        case cpp::file:            xio.to_file(res.str, res.is, res.os);            break;        case cpp::string:            xio.to_string(res.str);            break;    }        return xio;}        cpp::xtream &cpp::operator<<(cpp::xtream &xio, const cpp::state &res){    switch (res.m_state)    {        case cpp::console:            xio.to_console();            break;        case cpp::file:            xio.to_file(res.str, res.is, res.os);            break;        case cpp::string:            xio.to_string(res.str);            break;    }        return xio;}



The set functions were the obvious escape, I should've known that. But anyway, that just comes to show us that nothing is impossible, except for overriding the stupid precendence rules...
Advertisement
btw, before i go off implementing some more crazy functions (like reading from the console and writing to file with same object), do you guys know if, wehn i call delete on a fstream object, does it close the file automattically?
Quote:Original post by Si0n
btw, before i go off implementing some more crazy functions (like reading from the console and writing to file with same object), do you guys know if, wehn i call delete on a fstream object, does it close the file automattically?


After you close it, you can delete it, but then you can no longer use that variable for I/O w/o reallocating the streams for it.
But does the fstream destructors call close()?
I think so actually, but I ain'tsure.
(I normally use the FILE pointer, dunno why, I wonder if I'm the only one...)
I don't think so. It is alwasy advisible to not let the deconstructors do the cleanup for you [wink].

This topic is closed to new replies.

Advertisement