我有一个包含字符串的文件
你好,我的名字是乔
你好吗?
你好吗?
我正在尝试按原样输出该文件,但我的程序输出为"HellomynameisJoeHowAreyouDoing?Goodyou?" 我遇到空间和新线路问题.
int main (int argc, char* argv[])
{
index_table table1;
string word;
ifstream fileo;
fileo.open(argv[1]); //where this is the name of the file that is opened
vector<string> line;
while (fileo >> word){
line.push_back(word);
}
cout << word_table << endl;
for (int i=0; i < line.size(); i++)
{
if (find(line.begin(), line.end(), "\n") !=line.end())
cout << "ERRROR\n"; //My attempt at getting rid of new lines. Not working though.
cout << line[i];
}
fileo.close();
Run Code Online (Sandbox Code Playgroud)
返回0;
只需使用: std::getline
while (std::getline(fileo, word))
{
line.push_back(word);
}
Run Code Online (Sandbox Code Playgroud)
然后,
for (int i=0; i < line.size(); i++)
{
std::cout<<line[i]<<std::endl;
}
Run Code Online (Sandbox Code Playgroud)
或者只是: -
std::copy(line.begin(), line.end(),
std::ostream_iterator<std::string>(std::cout, "\n") );
//With C++11
for(const auto &l:line)
std::cout<<l<<std::endl;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4445 次 |
| 最近记录: |