Problems

Started by
13 comments, last by Drew_Benton 19 years, 4 months ago
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;
} 

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?
- k2"Choose a job you love, and you'll never have to work a day in your life." — Confucius"Logic will get you from A to B. Imagination will get you everywhere." — Albert Einstein"Money is the most egalitarian force in society. It confers power on whoever holds it." — Roger Starr{General Programming Forum FAQ} | {Blog/Journal} | {[email=kkaitan at gmail dot com]e-mail me[/email]} | {excellent webhosting}
Yes, the class is supposed to be an all in one class.

it works if i jsut do this:

xio >> i << i >> i << i;
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]
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...
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 =/
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.
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.
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.
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].

This topic is closed to new replies.

Advertisement