读取并显示文件,但输出不正确

Reading and displaying a file but output is incorrect

本文关键字:输出 不正确 显示文件 读取      更新时间:2023-10-16

我有这个文件:

###BEGIN
Player
FName: Jens
LName: Hogh
Club: Aalborg
Player
FName: Sebastian
LName: Perez
Club: Monaco
###COMPLETE

我必须读取并显示它。这是我到目前为止的代码:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main ()
{
    string txt[300];
    
    int i=0;
    ifstream file ("example.txt");
    std::string line;
    while (std:: getline (file, line)) 
    {
        std:: cout << line; /* use `line' inside the while-loop. */ 
    } 

 
    return 0;
}

输出错误!它显示如下:

BEGINPlayerFName: JensLName: HoghClub: alborgplayerfname: SebastianLName: PerezClub: Monaco###COMPLETE

都压在一起了。我如何得到它被分开,看起来像上面的文件?

std::cout << line;你从不告诉程序打印换行符试试std::cout << line << 'n';

你也在你的文件开头指定using namespace std;,这意味着你不需要把std::之前的内置功能了。所以std::cout可以写成cout