Sign in to follow this  

Problems

This topic is 4746 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Why isthis not working main.cpp
#include "xtream.hpp"

using cpp::xtream;

int main()
{
    xtream xio;
    
    int i = 3;
    
    xio >> i << i << (xio.to_file("xioTest.txt"), i);
    
    while (std::cin.get() != '\n') ;
    std::cin.get();
    return 0;
}    

xtream.hpp
#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 flag = console);
        xtream(const char *str, iostate flag = string, bool binary = false);
        
        xtream &to_file(const char *str);
        
        template<typename T> T &operator,(T &val)	{ return val; }
        
        friend xtream &operator>>(xtream &xio, int &val);
      	friend xtream &operator<<(xtream &xio, const int &val);
        	
    private:
        
        std::istream *istrm;
        std::ostream *ostrm;
        iostate m_flag;
    };
}

#endif    

xtream.cpp
#include "xtream.hpp"

cpp::xtream::xtream(iostate flag)
	: m_flag(flag)
{
    if (m_flag == console)
    {
    	istrm = &std::cin;
    	ostrm = &std::cout;
   	}   	
   	else if (m_flag == file)
   	{
   		istrm = new std::ifstream;
   		ostrm = new std::ofstream;
    }  		
    else if (m_flag == string)
    {
    	istrm = new std::istringstream;
    	ostrm = new std::ostringstream;
    }   	
} 

cpp::xtream::xtream(const char *str, iostate flag, bool binary)
	: m_flag(flag)
{
    assert(m_flag == file || m_flag == string);
    
   	if (m_flag == file)
   	{
   		istrm = new std::ifstream(str);
   		ostrm = new std::ofstream(str);
    }  		
    else if (m_flag == string)
    {
    	istrm = new std::istringstream(str);
    	ostrm = new std::ostringstream(str);
   	}   	
} 

cpp::xtream &cpp::xtream::to_file(const char *str)
{
    if (m_flag != console)
    {
        delete istrm;
        delete ostrm;
    }
    
    istrm = new std::ifstream(str);
    ostrm = new std::ofstream(str);
    
    return *this;
}    

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;
} 

Share this post


Link to post
Share on other sites
eer... why would that be the problem?

I'm trying to make a stream class that can embrace both strings, console and file.

so you can do stuff like this

xtream iox;

iox >> i << i << (iox.to_file("Joe"), i) >> (iox.to_console(), i) >> i; etc...

Share this post


Link to post
Share on other sites
err, I see because of the precedence of the "." operator i can't really do what i want
However,

xio >> i << i >> i << i;
xio.to_file("xioTest.txt");
xio << i;

should work and it is not writing to the file. weird =/

Share this post


Link to post
Share on other sites
Quote:
Original post by Si0n
eer... why would that be the problem?

I'm trying to make a stream class that can embrace both strings, console and file.

so you can do stuff like this

xtream iox;

iox >> i << i << (iox.to_file("Joe"), i) >> (iox.to_console(), i) >> i; etc...


That would be a problem because the get() function returns a istream, not a character. Furthermore, you are not assigning any data after you get it. An all in one class does not mean an **all at once**. It means it supports files, consoles, and strings, but not all at the same time in the manner you are using it (why do you think its not working?). You will need to do each separate operation on its own and not group everything together at once time.

Share this post


Link to post
Share on other sites
well, it is not really a problem, i can always return a reference ot the base class itself.

It works perfectly when I open the file first.

Btw, it is not homework or anything, and I'm not likely touse this, I just think itis possible and I'm doing this for fun.

Share this post


Link to post
Share on other sites
Quote:
Original post by Si0n
well, it is not really a problem, i can always return a reference ot the base class itself.

It works perfectly when I open the file first.

Btw, it is not homework or anything, and I'm not likely touse this, I just think itis possible and I'm doing this for fun.


Oh ok, yea it is pretty intresting...havent seen so much chaos in my whole life before though [lol].

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 4746 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this