2016-03-18 29 views
4

Merhaba Bir kabuk uygulamak için kısa bir program yazıyorum ve sıra dışı bir soruna rastlıyorum. Bazı nedenlerden dolayı std :: cout tamponu temizlenemiyorum. Program mesajları yazdırmayacak. Basit bir çözümün std :: cerr'e geçmek olduğunu anlıyorum, ama cout ile yazdırmak için mesajlar almanın bir yolu var mı? Ben uğraş ettik şeyler:Kopyalama arabelleğini temizleme (C++)

  1. std::cout.flush()
  2. herşeyi std::endl takma standart dışarı yazılır.
  3. std::flush'un çıkış akışına
  4. eklenmesi, çıktıyı çözmemesi gereken bir şeydi.

Herhangi bir yardım çok burada takdir benim kodudur:

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

Her şeyi denediğiniz görünüyor. listelenen _ [burada] (http://stackoverflow.com/questions/36096103/clearing-the-cout-buffer-c) _. Gönderdiğiniz örneklerin gerçek kodunuzla eşleştiğinden emin misiniz? Hangi ortamı (OS, Derleyici) kullanıyorsunuz? – ryyker

+1

Bu _ [link] (http://stackoverflow.com/a/13809766/645128) _ yardımcı olabilir. Bu arada, 'cout' sadece C++. C etiketini yayınınızdan kaldırdım. – ryyker

+0

Geany ile linux ortamında kod yapıyorum. Bağlantıya bir göz atacağım. Kullanmakta olduğum derleyici seçenekleri şunlardır: g ++ -Wall -std = C++ 0x -c "% f" Yapı Seçenekleri: g ++ -Wall -std = C++ 0x -o "% e" "% f " –

cevap

3

sorun içinde UB yol açar while döngünün son yineleme üzerinde cout için NULL gönderme ve konum olmasıdır Senin durumun cout sıkışıyor. Eğer cout bir şey göndermeden önce NULL olup olmadığını kontrol edin ve iyisin:

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

Hiç onlar durum bilgilerini (the iostate, which I recommend you read about) taşıyabilir unutmayın sizin akışlarına ne olduğunu bilmek gerekir.

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 

Veya daha da basit: Aşağıdaki kodu hatasını izlemek yardımcı olabilirdi

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

Bu kod baktım nasıl olurdu gibi:

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

Sonuç:

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