2013-11-02 189 views
1

當我使用cout很長的字符串時,它不輸出整個字符串,只輸出結尾。需要幫助輸出長字符串

std::string test[] = {"alotof text--sdfsdfsdfsdfsdfsfsdferer----248 characters"}; 
std::cout << *test << std::endl; 

輸出:呃---- 248個字符

這是字符串的結束而已,最開始的被切斷

+4

當您使用字符串而不是字符串數組時會發生什麼? – juanchopanza

+1

它不應該因爲指針指向數組的第一個元素,這將是他的字符串。 –

回答

1

寫輸出到一個文件,並檢查內容是否在文件中。我猜想你的字符串包含回車('\r')字符,它們會將輸出的位置移動到行的前面,從視覺上覆蓋已寫在此行上的字符。但是,所有的字符都應該在文件中可見。

+0

TY你是正確的,我沒有使用例子中的字符串沒有考慮它會影響多少。 –

0

我這個測試:

#include <iostream> 

int main(){ 
    std::string test[] = {"alotof text--sdfsdfsdfsddfasdfsdfasdfadfadfadfadfdfadfadfadfadfadfadfadsfasdfasdfasdfasdfasdfafdasdfadsfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfadsfasdfasdfadsfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfadsfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfadsafsdffsdfsfsdferer----248 characters"}; 
    std::cout << test[0].length() << std::endl; 
    std::cout << *test << std::endl; 
} 

這裏是我的輸出:

336 
alotof text-- sdfsdfsdfsddfasdfsdfasdfadfadfadfadfadfadfadfadfadfadfadfadsfasdfasdfasdfasdfasdfafdasdfadsfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfadsfasdfasdfadsfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfadsfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfadsafsdffsdfsfsdferer----248 characters 

似乎沒有要什麼錯

+0

您必須考慮某些變量。僅僅因爲它根據你的工作並不意味着它適用於每個人。 – 0x499602D2

0

我用Visual Studio 2010的C++編譯器測試,並且完全按照您所寫的和我的輸出編碼:

335 
alotof text--sdfsdfsdfsddfasdfsdfasdfadfadfadfadfdfadfadfadfadfadfadfadsfasdfasdfasdfasdfa 
sdfafdasdfadsfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfadsfasdfasdfadsfasdf 
asdfasdfasdfasdfasdfasdfasdfasdfasdfadsfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfas 
dfasdfasdfasdfasdfasdfasdfadsafsdffsdfsfsdferer----248 characters 

適合我。