Reading from standard in

Discussion in 'C++' started by Olivier Maurice, Oct 1, 2004.

  1. Hi all,


    I suppose some of you know the program Redmon (type redmon in google, first
    result). This neat little tool allows to hook up any functionality to a
    printer by putting the file printed to the printer to standard in.

    You have to provide yourself the program that does something with that data,
    so I thought with some quick c/c++ programming that would be no problem. I
    tried dozens of ways of reading the data from stdin or cin, char by char
    (like sample below), buffers... but I only got it working for text files,
    e.g. pdf-files, html-files,...
    When a gif or any other binary file is sent to it, the none of the ways
    would work: in case of byte by byte reading it stops way too early (example
    1), in case of buffered reading (example 2), it only goes through the buffer
    1 time, like if he already encountered eof.

    I know in case of binary files this is perfectly possible, but how to read
    then binary data from the standard input?

    A hint, url, some names of methods to be used, are fine. I am no noobie...


    Regards,

    Olivier

    //example 1
    ofstream logfile(fileName.c_str(), ios::binary | ios::eek:ut);
    char c;
    cin.get(c);
    while (!cin.eof())
    {
    logfile << c;
    cin.get(c);
    }
    logfile.close();

    //example2
    char *buffer = new char[1000];
    ofstream outputfile(filenameTmp->c_str(), ios::eek:ut);
    while ( !cin.eof() )
    {
    cin.read(buffer, 1000);
    outputfile.write(buffer, 1000);
    }
    outputfile.close();
    Olivier Maurice, Oct 1, 2004
    #1
    1. Advertising

  2. Olivier Maurice

    David Hilsee Guest

    "Olivier Maurice" <> wrote in message
    news:415d9def$0$10394$...
    > Hi all,
    >
    >
    > I suppose some of you know the program Redmon (type redmon in google,

    first
    > result). This neat little tool allows to hook up any functionality to a
    > printer by putting the file printed to the printer to standard in.
    >
    > You have to provide yourself the program that does something with that

    data,
    > so I thought with some quick c/c++ programming that would be no problem. I
    > tried dozens of ways of reading the data from stdin or cin, char by char
    > (like sample below), buffers... but I only got it working for text files,
    > e.g. pdf-files, html-files,...
    > When a gif or any other binary file is sent to it, the none of the ways
    > would work: in case of byte by byte reading it stops way too early

    (example
    > 1), in case of buffered reading (example 2), it only goes through the

    buffer
    > 1 time, like if he already encountered eof.
    >
    > I know in case of binary files this is perfectly possible, but how to read
    > then binary data from the standard input?
    >
    > A hint, url, some names of methods to be used, are fine. I am no noobie...


    Have you consulted the FAQ (http://www.parashift.com/c -faq-lite/) setion
    15 ("Input/output via <iostream> and <cstdio>") question 13 ("How can I
    "reopen" std::cin and std::cout in binary mode?")?

    --
    David Hilsee
    David Hilsee, Oct 1, 2004
    #2
    1. Advertising

  3. I am afraid I will need a little more hint or help :). This is some great
    resource though. Thanks already.


    > Have you consulted the FAQ (http://www.parashift.com/c -faq-lite/) setion
    > 15 ("Input/output via <iostream> and <cstdio>") question 13 ("How can I
    > "reopen" std::cin and std::cout in binary mode?")?
    >
    > --
    > David Hilsee
    >
    >
    Olivier Maurice, Oct 3, 2004
    #3
  4. Olivier Maurice wrote:
    >
    > I am afraid I will need a little more hint or help :). This is some great
    > resource though. Thanks already.
    >


    OK.
    The FAQ clearly says:
    There is no standard way to do it. It all depends on your compiler/platform.

    Thus: You need to ask in a newsgroup which discusses your compiler and/or platform.
    comp.lang.c++ deals with platform independent ascpects of C++ only.


    --
    Karl Heinz Buchegger, GASCAD GmbH
    Teichstrasse 2
    A-4595 Waldneukirchen
    Tel ++43/7258/7545-0 Fax ++43/7258/7545-99
    email: Web: www.gascad.com

    Fuer sehr grosse Werte von 2 gilt: 2 + 2 = 5
    Karl Heinz Buchegger, Oct 3, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. steve.leach

    How standard is the standard library?

    steve.leach, Apr 18, 2005, in forum: Python
    Replies:
    1
    Views:
    392
    Christos TZOTZIOY Georgiou
    Apr 18, 2005
  2. funkyj
    Replies:
    5
    Views:
    1,128
    funkyj
    Jan 20, 2006
  3. Replies:
    5
    Views:
    862
  4. Sriram Srinivasan
    Replies:
    13
    Views:
    561
    Benjamin Kaplan
    Nov 12, 2009
  5. Venks
    Replies:
    5
    Views:
    239
    Ken Bloom
    Dec 6, 2007
Loading...

Share This Page