从文本文件打印后如何摆脱新线条,并在打印时摆脱结局空间


                string foo ="";
                ifstream openfile(argv[i+1]);//argv[i+1] is file name
                if(openfile.is_open())
                { 
                    while(getline(openfile, foo))
                     {
                       istringstream myString(foo);
                       string w1;
                       while(myString >> w1)
                         cout<<w1<<' ';
                    cout <<""<< endl;
                }   

我需要正常打印出文本,这意味着没有额外的新线,并且在每行末尾没有空间。

输出

Launch Terminal
< Words Words Words                                                                                                                                                                                                       
< Words Words Words Words Words Words                                                                                                                                                                                                       
<                                                                                                                                                                                                                                        
<                                                                                                                                                                                                                                        
< Words Words Words Words                                                                                                                                                                                                       
< Words Words Words Words                                                                                                                                                                                                        
< Words Words Words Words                                                                                                                                                                                                      
<                                                                                                                                                                                                                                        
<                                                                                                                                                                                                                                        
<                                                                                                                                                                                                                                        
<                                                                                                                                                                                                                                        
< Words 

谢谢

好吧,要删除新的线字符,只需替换它们即可。为了摆脱每行末端的空间,不要打印它们,您自己添加它们XD XD代码

string foo;
ifstream openfile (argv[i+1]);
if(openfile.is_open())
{ 
    while(getline(openfile, foo))
    {
        //Remove new line character, if any
        std::replace(foo.begin(), foo.end(), 'n', '');
        //Remove carraige return (windows new line is 'rn' instead of just 'n')
        std::replace(foo.begin(), foo.end(), 'r', '');
        std::cout << foo; //print line
    }
}   

但是,也许您实际上想在每行之间添加一个空间(因此在打印了foo之后(,因为另一行的第一线可能会粘在以前的最后一行,因为有一个空间,也可能存在休息时间以保持它们的appart(

编辑:如果要保留原始的新系列字符,请在代码中删除两个std::replace(...)行;(您可能还需要在打印FOO后打印一个endl,具体取决于您期望的输出因为这在您的问题中不太清楚

edit2:当我对您实际想要的内容获得更多信息时,这是一个更新的代码(至少(删除每行末端的其他空间。如果这仍然不做您想要的事情,请更清楚特定文件的输出的外观!

string foo;
ifstream openfile (argv[i+1]);
if(openfile.is_open())
{
    while(getline(openfile, foo))
    {
        istringstream myString(foo);
        string w1;
        bool firstWord=true;
        while(myString >> w1)
        {
            if(firstWord==false)
            {
                cout << " ";
            }
            firstWord = false;
            cout << w1;
        }
        cout << endl;
    }
}

最新更新