Thread: Switching streams?

  1. #1
    <<>>
    Guest

    Switching streams?

    Having a bit of trouble making my program work.

    I am trying to get a stream that I can assign to either cin or an ifstream.

    My idea was this...

    Code:
    istream inputstream;
    
    inputstream = cin;
    
    if (whatever) {
    
      inFile = ifstream("myfile.txt", ios::in);
      inputstream = inFile;
    
    }
    However, I get erros when doing this that all the constructors and assignment operators for istream are "private within this context". I'm not qute sure what that means, but it won't let me use it later on.

    Does anyone have experience with this kind of thing?

    Also, it needs to be a stream, not a file pointer, so freopen() won't work.

    I need a stream so I can apply transform() on it.

    thanks if you can help me.

  2. #2
    Are you trying to redirect the CIN stream?? if so, here's an example code.

    Code:
    #include <iostream>
    #include <fstream>
    
    int main()
    {
        std::ofstream logFile("out.txt"); //This is an ofstream, this forum puts an EMOTICON instead of 'O'
        std::streambuf *outbuf = std::cout.rdbuf(logFile.rdbuf());
        std::streambuf *errbuf = std::cerr.rdbuf(logFile.rdbuf());
    
        // do the actual work of the program;
        // GUI code and event loop would go here
        std::cout << "This would normally go to cout but goes to the log file\n";
        std::cerr << "This would normally go to cerr but goes to the log file \n";
        logFile << "This goes to the log file\n";
        // end of program body
    
        // restore the buffers
        std::cout.rdbuf(outbuf);
        std::cerr.rdbuf(errbuf);
    }
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

  3. #3
    <<>>
    Guest

    Not working right...

    Getting closer...

    Still not working right though.

    I am here...

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <iterator>
    #include <algorithm>
    
    using namespace std;
    
    string avix(unsigned char);
    
    int main (int argc, char** argv) {
    
      streambuf* inbuf = cin.rdbuf();
      streambuf* outbuf = cout.rdbuf();
      cin.rdbuf(inbuf);
      cout.rdbuf(outbuf);
    
    
      while (argc > 1) {
    
        if (argc % 2 == 0 || argc > 5) {
    
          cerr << "Wrong number of arguments\n";
          exit(1);
    
        }
    
        if ( ( argc > 1) && ( !strcmp (argv[argc-2], "-i") ) ) {
    
          ifstream inFile  (argv[argc-1],  ios::in);
    
          if (!inFile.is_open()) {
    
            cerr << "Could not open file " << argv[argc-1] << " for reading!\n";
            exit(1);
    
          }
    
          argc -= 2;
    
          cin.rdbuf(inFile.rdbuf());
    
        } 
    
        if ( ( argc > 1) && ( !strcmp (argv[argc-2], "-o") ) ) {
    
          ofstream outFile (argv[argc-1], ios::app);
    
          if (!outFile.is_open()) {
    
            cerr << "Could not open file for " << argv[argc-1] << " writing!\n";
            exit(1);
    
          }
    
          argc -= 2;
    
          cout.rdbuf(outFile.rdbuf());
    
        }
    
      }
    
      transform(istream_iterator<unsigned char>(cin),istream_iterator<unsigned char>(),ostream_iterator<string>(cout),avix);
    
      cout << '\n';
    
      cout.rdbuf(outbuf);
      cin.rdbuf(inbuf);
    
      return 0;
    
    }
    With no arguments it works properly, and if I remove the argument handling and cin and cout and go straight from file to file it works, reading chars, translating them through avix() and then outputting them to a file or cout.

    But this way I either get a segmentation fault or no effect, it is has any args. (But calling it without args works perfectly)

    On a related note, does anyone know how to send EOF down cin? Or is there an end iterator for an input stream that I am overlooking? On files that transform() worked fine (with an fstream) but when input comes from cin it keeps taking more and more characters until I shut it down from outside.

  4. #4
    <<>>
    Guest

    This worked...

    For what I was saying before, this is the original files only version that I am trying to make accept input and output from command line.

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <iterator>
    #include <algorithm>
    
    using namespace std;
    
    string avix(unsigned char);
    
    int main (int argc, char** argv) {
    
      ifstream ifs("ezeck.txt", ios::in);
      ofstream ofs("ezeck.txt", ios::app);
    
      ofs << "... \n";
    
      transform(istream_iterator<unsigned char>(ifs), istream_iterator<unsigned char>(), ostream_iterator<string>(ofs), avix);
    
      ofs << '\n';
    
      return 0;
    
    }
    This appends ... and then the output of avix on each character of ezeck.txt to the end of ezeck.txt.

    And it works, for me at least.

    I'm thinking for the inputs maybe buffering it into a stringstream might help, but I don't see how it would be able to continue running transform until an eof if I did.

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    272
    eof can normally be entered into stdin using either ctrl-d or ctrl-z depending on your os.

    Can you not use a base pointer to a stream? As in -

    Code:
    	//if input required from stdin
    	istream* is = &cin;
    
    	//else
    	is = new ifstream("t.txt");
    	
    
    	transform(istream_iterator<char>(*is), istream_iterator<char>(),
    		ostream_iterator<char>(cout),toupper);
    
    	//...
    
    	if(is != &cin)
    		delete is
    Joe

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Node switching help
    By Link_26 in forum C++ Programming
    Replies: 0
    Last Post: 02-15-2009, 09:03 PM
  2. Writing to two streams: va_arg issues
    By dwks in forum C Programming
    Replies: 2
    Last Post: 09-26-2007, 10:14 AM
  3. Win32 File Switching within a directory
    By hemanth.balaji in forum Windows Programming
    Replies: 1
    Last Post: 06-10-2005, 10:20 AM
  4. Switching frequency and Bandwidth???
    By pastecopy in forum Tech Board
    Replies: 0
    Last Post: 03-23-2003, 11:13 AM
  5. Independent streams of pseudo random number generator
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-15-2001, 05:32 AM