如何忽略\ n for istream字符串

时间:2013-06-05 12:39:55

标签: c++ string output istream

有没有人对如何忽略来自istream的“\ n”有任何建议?我正在尝试从txt文件中提取数据,在某些“单元格”中,文本已被写入,以便当用户按Enter键时出现“\ 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);
}

1 个答案:

答案 0 :(得分:0)

您可以使用它从字符串中删除所有\n

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