Thread: cin cout with winapi ?

  1. #1
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638

    cin cout with winapi ?

    can you use cin and cout with the winapi ? such as drawtext() and the other functions ? of so how ?

    i was thinking
    Code:
        cout<<drawtext()<<endl
    something like that.

    also little trouble figureing out exactly what is going on with this ':' in the inline function

    Code:
    public:
      Rectangle(int w = 0, int h = 0)
        : wide(w), high(h) {}
    
    
    and
    
    MyType::MyType(int i) : Bar(i) { // ...
    
    MyType2::MyType2(int i) : Bar(i), m(i+1) { // ...
    so bar(i) is in the scope of mytype2 or just share the variable i ? same i or different i ? passed by ? reference or address ?

    inline inside class rectangle with var wide and high ? yes ?

    !giffcopy==idea.bad ?


    You realize why you can not do this ?

    Code:
    /* regiff.cpp */
    
    #include <string>
    #include <fstream>
    using namespace std;
    
    int main() {
      ifstream in("File.gif"); // Open for reading
      ofstream out("File2.gif"); // Open for writing
      string s;
      while(getline(in, s)) // Discards newline char
        out << s << "\n"; // ... must add it back
    }
    because it would be considered regiffing. meow.
    Last edited by kryptkat; 12-22-2009 at 09:39 AM. Reason: missinghalfofmessage

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by kryptkat View Post
    can you use cin and cout with the winapi ? such as drawtext() and the other functions ? of so how ?

    i was thinking
    Code:
        cout<<drawtext()<<endl
    something like that.
    What is your question? Cout will print out anything that the function returns, if it's a recognized type, just like any other functions you might have.

    also little trouble figureing out exactly what is going on with this ':' in the inline function

    Code:
    public:
      Rectangle(int w = 0, int h = 0)
        : wide(w), high(h) {}
    
    
    and
    
    MyType::MyType(int i) : Bar(i) { // ...
    
    MyType2::MyType2(int i) : Bar(i), m(i+1) { // ...
    so bar(i) is in the scope of mytype2 or just share the variable i ? same i or different i ? passed by ? reference or address ?

    inline inside class rectangle with var wide and high ? yes ?
    It's the initializer list.
    wide(w), high(h) -> initialize wide with w and high with h.
    MyType::MyType(int i) : Bar(i) -> call the base constructor Bar with the formal parameter i.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Not sure about the (point of the) first, but the second (: operator) is called the member initialisation list... basically calls the constructors of the class members. You could just do Bar = i; inside the scope of the constructor, but there's
    You may want to open the files in binary mode, and use .read() and .write() instead of getline.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The point about the initializer list is that it calls the constructor for something, and the assignment inside the constructor body calls the assignment operator.
    So it can be more efficient to call the constructor instead of assignment for some objects, and sometimes you must call the constructor for certain objects.
    These are the two areas where the initializer list is a good thing.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    so it call only constructors and objects ? and does not initialize variables ?

    rewinapiquestion

    What is your question? Cout will print out anything that the function returns, if it's a recognized type, just like any other functions you might have.
    so if i use hwnd or as the type

    Code:
     case CM_EDIT_COPY:
                   SendDlgItemMessage(hwnd, IDC_CHILD_EDIT, WM_COPY, 0, 0);
    
    to
    cout<<(hwnd, IDC_CHILD_EDIT, WM_COPY, 0, 0);
    
    or
    MessageBox(hwnd, "MDI Child creation failed."....
    to 
    cout<<MessageBox<<(hwnd, "MDI Child creation failed."...)
    tia

    so it is console or stream only ? cin cout.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by kryptkat View Post
    so it call only constructors and objects ? and does not initialize variables ?
    It does initialize variables. Objects are also stored in variables.
    It initialized them by calling its constructor. For built-it types, the constructor is basically the same as just assigning to it.

    Code:
     case CM_EDIT_COPY:
                   SendDlgItemMessage(hwnd, IDC_CHILD_EDIT, WM_COPY, 0, 0);
    
    to
    cout<<(hwnd, IDC_CHILD_EDIT, WM_COPY, 0, 0);
    
    or
    MessageBox(hwnd, "MDI Child creation failed."....
    to 
    cout<<MessageBox<<(hwnd, "MDI Child creation failed."...)
    Let me ask you this:
    What is it that you are trying to do and why do you think it will work?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    with winapi i normally just use drawtext() or settext() or whatever to "print text on " the dialog box or window edit or mdi area. or whereever needed. was wondering if cin and cout works with winapi to send text to the window instead of a console.

    let meow refrase the question. is there an advantage to using the cpp cin and cout with the winapi to set text on the window. if that is even possible. to use cin and cout like that. still getting a feel for cin and cout what will work and what will not work. ok ?

    thinking is this. if you can send the stream to or from a file or to or from a console whay not a window ? or dialog box ?

    i could go macro usage but i prefer the pure lang.

  8. #8
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    cin and cout are variables of type istream and ostream respectively. The cin stream is initialized to read from stdin, and cout is initialized to write to stdout. You cannot directly change this behavior.

    Now if you really wanted to, you could close stdin and stdout, and reopen them to something else. This would indirectly change the behavior of cin and cout, but I don't see the benefit to doing this.

    Instead, I would just create wrapper functions like PrintToWindow(const char*, ...), and then let that function set the text using the winapi.
    bit∙hub [bit-huhb] n. A source and destination for information.

  9. #9
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    You cannot directly change this behavior.
    ok so it is more like printf() then. thank you.

    Now if you really wanted to, you could close stdin and stdout, and reopen them to something else. This would indirectly change the behavior of cin and cout, but I don't see the benefit to doing this.
    i guess that is what i was trying to do. with winapi. ok. seeing that will not work now could you please show me how to close stdin and stdout and say reopen it to a usb flashdrive.

    something like
    Code:
       assuming M: is the usb flash drive 
       ifstream in(M:\"File.gif"); // Open for reading
      ofstream out(M:\"File2.gif"); // Open for writing
    or did you have something different in mind ?

  10. #10
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Well you can create new istream and ostream objects point to whatever you want (a usb flashdrive for instance). It's only a little tricky if you want to explicitly use the cin and cout variables.

    Here is a little example:
    Code:
    #include <iostream>
    #include <cstdio>
    
    int main()
    {
        freopen("./output", "w", stdout);
    
        std::cout << "This should go to the file" << std::endl;
    
        fclose(stdout);
    }
    bit∙hub [bit-huhb] n. A source and destination for information.

  11. #11
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    ah freopen() got it. thank you.

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    There's no guarantee that this will work, though. It assumes that the C and C++ streams are coupled so tightly that reopening C's stdout also changes C++'s cout.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  13. #13
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    i supp if you do not use both printf and cin cout mixed not a issue. i was just about to ask that.

  14. #14
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    There is absolutely no reason using std::cin and std::cout. Just name them differently. Makes more sense. With code already using std::cout and std::cin just use find-replace to ofstream/ifstream you define.

    thinking is this. if you can send the stream to or from a file or to or from a console whay not a window ? or dialog box ?
    You can of course send it to a window. But what do you mean "send to a window"? Where on the window? In any case the closest thing on what you want is define a standard input/output for the window. Lets say you define a textBox as the standard input/output. Then you can just create a stdPrint class. It will override << and >> and it can do whatever you want. For example (w/o using namespace std
    Code:
    std::string res;
    stdPrint cin(myTextBox);
    cin >> res;
    
    stdPrint cout(myLabel);
    cout << "hey dude" << stdPrint::endl;

  15. #15
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    i will give that a try thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. CIN Input count question?
    By kamran in forum C++ Programming
    Replies: 5
    Last Post: 10-24-2006, 04:06 PM
  2. Binary I/O with cin and cout
    By The Urchin in forum C++ Programming
    Replies: 4
    Last Post: 10-24-2006, 12:47 PM
  3. Cin & Cout VS. Scanf & Printf
    By MatriXxX in forum C Programming
    Replies: 19
    Last Post: 08-08-2003, 11:47 AM
  4. Redirecting cout stream
    By Arrow Mk84 in forum C++ Programming
    Replies: 1
    Last Post: 10-08-2002, 04:17 PM
  5. i don't know about cin, cout, flag.
    By comwin in forum C++ Programming
    Replies: 2
    Last Post: 11-08-2001, 04:26 AM