2016-03-18 7 views
4

Hallo Ich schreibe ein kurzes Programm, um eine Shell zu implementieren, und ich stoße auf ein ungewöhnliches Problem. Aus irgendeinem Grund kann ich den std :: cout-Puffer nicht löschen. Das Programm druckt keine Nachrichten. Ich verstehe, dass eine einfache Lösung ist, zu std :: cerr zu wechseln, aber gibt es eine Möglichkeit, Nachrichten mit cout zu drucken? Dinge, die ich habe versucht habe:Löschen des Cout-Buffer (C++)

  1. std::cout.flush()
  2. Einfügen std::endl nach etwas geschrieben zu Standard aus.
  3. Einfügen eines std::flush in den Ausgabestrom
  4. std::cout.setf(std::ios::unitbuf); was war etwas, das ich finden sollte, sollte die Ausgabe unbuffer.

Jede Hilfe sehr geschätzt wird hier ist mein 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; 
} 
+0

Es scheint, dass Sie alles versucht haben aufgeführt _ [hier] (http://stackoverflow.com/questions/36096103/clearing-the-cout-buffer-c) _. Sind Sie sicher, dass Ihre geposteten Beispiele mit Ihrem tatsächlichen Code übereinstimmen? Welche Umgebung (OS, Compiler) verwenden Sie? – ryyker

+1

Dieses _ [link] (http://stackoverflow.com/a/13809766/645128) _ könnte helfen. "Cout" ist übrigens rein C++. Ich habe das C-Tag aus deinem Beitrag entfernt. – ryyker

+0

Ich bin in einer Linux-Umgebung mit Geany codieren. Ich werde den Link durchsehen. Die Compileroptionen, die ich verwende, sind: g ++ -Wall -std = C++ 0x -c "% f" Erstellungsoptionen: g ++ -Wall -std = C++ 0x -o "% e" "% f " –

Antwort

3

Das Problem ist, dass Sie NULL-cout auf der letzten Iteration der while Schleife senden, die an UB führt, und in Ihr Fall ist Jamming cout. Überprüfen Sie für NULL, bevor Sie etwas zu cout senden und Sie sind fein:

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

Wenn Sie jemals wissen müssen, was Ihre Streams passiert, denken Sie daran, dass sie Statusinformationen tragen können (the iostate, which I recommend you read about).

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 

Oder noch einfacher:

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

hätte aussah Dies ist, wie Ihr Code:

#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; 
} 

Ergebnis:

Der folgende Code könnte Ihre Fehler geholfen verfolgen
$ ./a.out 
myshell> 1 2 3 
1 
2 
3 
cout is messed up... fixing it... 
3 items were added to the command array