input stream console?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: input stream console?

  1. #1
    Join Date
    Apr 2010
    Posts
    17

    input stream console?

    What I'm trying to do is, from the input stream by the console, I want to compress this text data.

    My problem is that, I don't know how to hold all that data from the input.

    Example:

    cin >> //here would be 500KB or more worth of data, from console input stream
    cin >> string //<---WRONG!

    So how can I do this? I can handle this problem if I was reading in a file and compress the amount of data I need at a time. Now from the console, not to familiar on how to approach this, any advice? For files I would use ifstream, and so I'm looking into how to do that same approach with iostream.
    Last edited by thirdeye; May 31st, 2010 at 05:59 PM.

  2. #2
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,292

    Re: input stream console?

    Let me start by asking how and why are you putting 500kB into your standard input stream (by "input stream by the console", I suppose we are talking about the standard input stream, cin?)

    If this was any other stream, any form of "while (stream >> word)" or "while(getline(stream , line))" would work perfectly fine, as that stream would one day end.

    The standard input stream is different though, as you can never read all of it. If you reach the end, the OS just waits for more, and from your program's point of view, it is not possible to "reach the end" of cin.

    Given your weird requirements, you have one of two solutions:
    1 - Try to put some sort of "End of stream" marker into your data
    2 - Write how long the input object is in the streamed header, and stop reading once that much data has been passed.
    Is your question related to IO?
    Read this C++ FAQ LITE article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  3. #3
    Join Date
    Apr 2010
    Posts
    17

    Re: input stream console?

    I am working on a project for class. In any case the professor wants us to compress files and standard input stream. Handle files was not so bad since I had the ifstream object. My guess is possibly that a test case for me would be to just copy and paste a file to console. I won't be able to put my own marker on files as to determine end of stream. I also will not know the length of the input before hand.

    Now looking at your suggestion, I am to use an object called stream in order to handle the input so I can compress? what exactly is stream refering to in?

    "while (stream >> word)" or "while(getline(stream , line))"

    stream, cin? streambuf? stringstream? istream?

    Still a little confused on this =/

  4. #4
    Join Date
    Aug 2007
    Posts
    858

    Re: input stream console?

    So how can I do this? I can handle this problem if I was reading in a file and compress the amount of data I need at a time. Now from the console, not to familiar on how to approach this, any advice?
    Probably you'll end up looking for an alternative method. The Windows (at least WinXP) console can only take about 256 characters of input at once. I'd imagine *nix probably has a larger limit, but I highly doubt it's going to let you enter 500kB at once.

    Edit-
    Handle files was not so bad since I had the ifstream object. My guess is possibly that a test case for me would be to just copy and paste a file to console. I won't be able to put my own marker on files as to determine end of stream. I also will not know the length of the input before hand.
    It sounds like your prof needs to clarify the assignment. You're going to have to take your input from stdin in relatively small chunks due to the limitations I just mentioned, so you're going to have to have some way to determine the end of the input. Will pretty much have to be either some kind of marker at the end of the file/input, or else something the user enters to signal the end.
    Last edited by Speedo; May 31st, 2010 at 09:54 PM.

  5. #5
    Join Date
    Apr 2010
    Posts
    17

    Re: input stream console?

    oh wow, didn't know we had a limit of 256 characters. Well looks like this portion requires a little more effort then the file stream. Looks like I need to give this one a little more thought, hmmm. I'll ask the professor again about the sizes to expect on the standard input.

    Thanks.

    EDIT-

    Professor said, "If it comes from the console keyboard, each *line* of input could be up to 256 characters in length, but there could be an unlimited number of lines."

    After reading this from Prof. I'm leaning more towards the implementation of "while(getline(char input[256] , line))" for an unlimited number of lines.
    Last edited by thirdeye; May 31st, 2010 at 11:42 PM. Reason: Response from Prof.

  6. #6
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,292

    Re: input stream console?

    Sounds more like a standard "process input as it comes" exercise.

    You are in lick, those aren't too hard.

    This will work:

    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
        std::string line;
        while(std::getline(std::cin, line))
        {
            std::cout << "Line is: " << line << std::endl;
        }
        return 0;
    }
    Note that for cin, as I mentioned, "while(std::getline(std::cin, line))" will ALWAYS loop, so it is kind of useless in this context. However, it works, and this is the way you want to do it if you had any other stream, so might as well do it like this.
    Is your question related to IO?
    Read this C++ FAQ LITE article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  7. #7
    Join Date
    Oct 2002
    Location
    Austria
    Posts
    1,284

    Re: input stream console?

    Quote Originally Posted by monarch_dodra View Post
    Note that for cin, as I mentioned, "while(std::getline(std::cin, line))" will ALWAYS loop...
    I don't see why this would ALWAYS loop. If the input comes from the keyboard then the user can alwais press ctrl-d ( or ctrl-z on windows ) to set EOF. If the input comes from a file ( via input redirection ) then cin behaves just like any other filestream.
    Kurt

  8. #8
    Join Date
    Apr 2010
    Posts
    17

    Re: input stream console?

    I was just reading about the always loop for std input and first thing came to mind was how would I know when user is done. It looks like I'll have to be sure to instruct user of ctrl-d or ctrl-z (depending on OS).

    Thanks for the help!

  9. #9
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: input stream console?

    Quote Originally Posted by thirdeye View Post
    Now looking at your suggestion, I am to use an object called stream in order to handle the input so I can compress? what exactly is stream refering to in?

    "while (stream >> word)" or "while(getline(stream , line))"

    stream, cin? streambuf? stringstream? istream?

    Still a little confused on this =/
    You should write the code so that stream is a generic istream reference. If cin happens to be the istream passed to the function, cool, but it's better not to rely on that (more reusable code).

    And all this talk of the limitations of cin doesn't really apply if you're planning to run the program with input redirection, as mentioned.

  10. #10
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,292

    Re: input stream console?

    Quote Originally Posted by ZuK View Post
    I don't see why this would ALWAYS loop. If the input comes from the keyboard then the user can alwais press ctrl-d ( or ctrl-z on windows ) to set EOF. If the input comes from a file ( via input redirection ) then cin behaves just like any other filestream.
    Kurt
    True. But in the context of a class, I doubt he should be using stream redirection, or expect the user to ctrl-something to eof the stream.

    Unless you take special measures, then cin will always loop, so I wanted to preemptively mention that.

    My use of the term "always" was indeed an exaggeration, but a fair warning.
    Is your question related to IO?
    Read this C++ FAQ LITE article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center