Flushing cout buffer (C ++)

Hello, I am writing a short program to implement a shell, and I am having an unusual problem. For some reason, I cannot clear the std :: cout buffer. The program does not print messages. I understand that a simple solution is to switch to std :: cerr, but is there a way to get messages to print using cout? Things I tried:

  • std::cout.flush()
  • Insert std::endlafter everything is written to standard.
  • Insertion std::flushin the output stream
  • std::cout.setf(std::ios::unitbuf);I found that I should output unbuffer output.

Any help is much appreciated here, this is my code:

int main()
{
    //Tryed this to unbuffer cout, no luck.
    std::cout.setf(std::ios::unitbuf);

    std::string input;

    //Print out shell prompt and read in input from keyboard.
    std::cout << "myshell> ";   
    std::getline(std::cin, input);

    //**********************************************************************
    //Step 1) Read in string and parse into tokens.
    //**********************************************************************

    char * buf = new char[input.length() + 1];
    strcpy(buf, input.c_str());

    int index = 0;
    char * command[256]; 

    command[index] = std::strtok(buf, " ");    //Get first token.
    std::cout << command[index] << std::endl;

    while (command[index] != NULL)
    {
        ++index;
        command[index] = std::strtok(NULL," ");    //Get remaining tokens.
        std::cout << command[index] << std::endl;
    }   

    std::cout.flush(); //No luck here either

    //HERE IS WHERE MY PROBLEM IS.
    std::cout << index << " items were added to the command array" << std::endl;

    delete[] buf;

    return 0;   
}
+4
source share
2 answers

, NULL cout while, UB, cout. NULL - cout, :

if (command[index] != NULL) {
    std::cout << command[index] << std::endl;
}
+3

- , , , (iostate, ). :

 try {
      std::cout.exceptions(std::cout.failbit);          
 } catch(const std::ios_base::failure& e) {
      std::cerr << "stream error: " << e.what() << std::endl;
      std::cout.clear();
 }

 // continue working with cout, because std::cout.clear() removed
 // failbit

, :

if(not std::cout) {
    // address your error (if it is recoverable)
 }

:

#include <cstring>
#include <string>
#include <iostream>
int main()
{
    //Tryed this to unbuffer cout, no luck.
    std::cout.setf(std::ios::unitbuf);

    std::string input;

    //Print out shell prompt and read in input from keyboard.
    std::cout << "myshell> ";   
    std::getline(std::cin, input);

    //**********************************************************************
    //Step 1) Read in string and parse into tokens.
    //**********************************************************************

    char * buf = new char[input.length() + 1];
    strcpy(buf, input.c_str());

    int index = 0;
    char * command[256]; 

    command[index] = std::strtok(buf, " ");    //Get first token.
    std::cout << command[index] << std::endl;

    while (command[index] != NULL)
    {
        ++index;
        command[index] = std::strtok(NULL," ");    //Get remaining tokens.
        std::cout << command[index] << std::endl;
    }

    // I added from here...
    if(not std::cout) {
      std::cerr << "cout is messed up... fixing it..." << std::endl;
      std::cout.clear();
    }
    // ... to here.

    std::cout.flush(); //No luck here either

    //HERE IS WHERE MY PROBLEM IS.
    std::cout << index << " items were added to the command array" << std::endl;

    delete[] buf;

    return 0;   
}

:

$ ./a.out
myshell> 1 2 3
1
2
3
cout is messed up... fixing it...
3 items were added to the command array
+1

All Articles