Like Tree2Likes
  • 2 Post By laserlight

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

This is a discussion on Mixing C and C++ IO. What extra syncing do I need ? within the C++ Programming forums, part of the General Programming Boards category; (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 ...

  1. #1
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498

    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?
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,701
    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
    21,598
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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 ?
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    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-11-2003, 11:32 PM
  5. Mixing gcc 2.9* with gcc 3.*
    By rotis23 in forum Linux Programming
    Replies: 1
    Last Post: 07-19-2003, 12:21 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21