Thread: Mixing C and C++ IO. What extra syncing do I need ?

  1. #1
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657

    Mixing C and C++ IO. What extra syncing do I need ?

    (Please ignore portability issues, as this is just for fun)
    Code:
    std::string InputUI(std::string msg="Input",int numchars=50)
    {
        std::string command = "kdialog --inputbox \"" + msg + "\"";
        auto f = popen(command.c_str(),"r");
        char* ret = new char[numchars];
        fgets(ret,numchars,f);
        ret[numchars-1]='\0';
        std::string ret_str = std::string(ret);
        delete [] ret;
        return ret_str;
    }
    But If I call this function twice in a row...and cout the strings returned, they seem to output after the second one is returned, even when I use endl in between.
    Other than that, this seems to work fine for me, but have I missed something obvious?

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Interesting problem, I don't think this is being caused by the mixing of C-stdio and C++ streams but maybe something to do with how cout chains the output.

    With the following calls
    Code:
       cout << InputUI("Input", 1000) << " ";
       cout.flush();
       cout << InputUI("Input 2", 1000) << endl;
    
       cout << InputUI("Input", 1000) << InputUI("Input 2", 1000) << endl;
    I got the following results when entering "This is a test.", "Does it work?" for the first two dialogs, and "bye!", "Good" for the second two.

    This is a test. Does it work?

    and then

    Goodbye!
    Also shouldn't the following line:
    Code:
    ret[numchars-1]='\0'
    Actually be:
    Code:
    ret[strlen(ret)-1]='\0'
    If you are trying to remove the trailing end of line character.

    Jim

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The problem with this line:
    Code:
    cout << InputUI("Input", 1000) << InputUI("Input 2", 1000) << endl;
    is that order of evaluation is unspecified, so when InputUI has side effects, the order in which the side effects occur depends on which function call is chosen to be evaluated first.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Shouldn't you be pcloseing the pipe?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Why use new when you can use a vector? This is not thread safe.
    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.

  6. #6
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by jimblumberg View Post
    Actually be:
    Code:
    ret[strlen(ret)-1]='\0'
    If you are trying to remove the trailing end of line character.
    Ok.
    Quote Originally Posted by oogabooga View Post
    Shouldn't you be pcloseing the pipe?
    Yes.. I should be.
    (Why doesn't it get closed upon the end of the block, though ?)

    Quote Originally Posted by Elysia View Post
    Why use new when you can use a vector? This is not thread safe.
    What happens to the vector's size after being filled, if I pass its address ?

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The vector's size does not change. You should pre-allocate it to a proper size. If you were using a C++ function, you could use a push_back_iterator and forget about pre-allocating size.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Syncing data
    By ssasa in forum C# Programming
    Replies: 0
    Last Post: 03-21-2008, 04:01 AM
  2. Mixing C and C++
    By curlious in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 09-10-2005, 10:30 AM
  3. What about mixing c with c++
    By gandalf_bar in forum C++ Programming
    Replies: 3
    Last Post: 06-19-2004, 08:30 AM
  4. Mixing C and Assembly
    By Dragoon_42 in forum C Programming
    Replies: 6
    Last Post: 11-12-2003, 12:32 AM
  5. Mixing gcc 2.9* with gcc 3.*
    By rotis23 in forum Linux Programming
    Replies: 1
    Last Post: 07-19-2003, 12:21 AM