2013-06-05 70 views
0

有沒有人對我怎麼能夠無視「\ n」的任何建議,由istream的未來嗎?我試圖從txt文件中提取數據,其中某些「單元格」中的文本已經寫入,使得當用戶按下Enter時出現「\ n」。如何忽略istream的字符串 n

我的目標是在這方面採取的文本輸出它的某些部分「;」分隔的部分。然而,在這樣做的時候,有時一個「\ n」被吸入,輸出單元開始向下繼續,而不是從右向左行(這是首選)。

任何意見將不勝感激!

string vectorToString(vector<size_t> positionVector, string mainString, 
             string outputString, int numLetters) 

{ 
    //Check how many different positions were found and make that the length 
    //of the vector that will store all the strings for each finding 
    int positionVectorLength = positionVector.size(); 
    //foundPos is the position of the cursor along the foundPositions vector 
    int vectorPosition=0; 
    while (vectorPosition < positionVectorLength) 
    { 
     //define local variable that will store the value along the vector 
     size_t vectorPositionValue = positionVector.at(vectorPosition); 


     //append the numLetters after the positionValue in string frameNum 
     outputString.append(mainString, vectorPositionValue, numLetters); 
     outputString.append(" ; ");  

     //reiterate until all the positions have been recorded 
     vectorPosition+=1; 
    } 

    //return the string of frame numbers 
    return(outputString); 
} 
+0

你應該使用常量&和與您的函數參數。你不能只使用string :: find和string :: erase在你的輸入字符串中刪除'\ n'的所有出現? – UldisK

+0

@UldisK你爲什麼這麼說? – ktosayev

+0

你提到用戶輸入enter並且被吸入。也許這跟使用istream :: getline()一樣簡單? http://www.cplusplus.com/reference/istream/istream/getline/ 它的默認delim是'\ n',它會丟棄它。 –

回答

0

你可以用它來刪除您的字符串的所有\n

mainString.erase(std::remove(mainString.begin(),mainString.end(),'\n'),mainString.end()); 
+0

但最好不要讓他們擺在首位。 –

+0

是的,但是「我試圖從txt文件中提取數據」,它們已經在那裏,所以在某一點OP將不得不刪除它們。但我不認爲它們更好,我同意。 – Djon

+0

不,許多從文件中讀取的方法都是跳過換行符。 :) –