Jump to content
  • Advertisement
Sign in to follow this  
Si0n

Problems

This topic is 4938 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
Advertisement
For starters, this line
Quote:
xio >> i << i << (xio.to_file("xioTest.txt"), i);

looks a little wacky. Are the stream operators supposed to be opposing like that?

Share this post


Link to post
Share on other sites
Yes, the class is supposed to be an all in one class.

it works if i jsut do this:

xio >> i << i >> i << i;

Share this post


Link to post
Share on other sites
Well the whole main.cpp file is wrong (see bottom). Can you please tell us what you are tying to do, so we can help you. Are you trying to save data to a file or load data?

[edit]The problem is the:

while (std::cin.get() != '\n') ;
std::cin.get();

part.
[/edit]

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
Why are you doing

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

???

It's confusing and unnecessary. Break it down so it makes sense.

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!