繁体   English   中英

在控制台中显示从文件读取的文本

[英]Show in console a text read from file

我正在尝试在Qt中读取文件,然后在控制台中显示它,这是我的mainwindow.cpp:

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <iostream>
#include <fstream>
using namespace std;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    ifstream F("datos.txt");
    int id;
    string name;
    int age;

    while(F >> id >> name >> age){                         /*reading the file*/
        cout << id << ", " << name << ", " << age << endl; /*this doesn't work*/
    }
}

MainWindow::~MainWindow()
{
    delete ui;
}

我不明白为什么这行不通,我用谷歌搜索了一下,但是我发现了另一种从Qt读取文件的方法。 我需要这种特定的方法,但是如果无法实现,请告诉我读取具有这种格式的多行文件的简短方法是什么:

123123123 aname 123123

您粘贴的代码将文件datos.txt的内容打印到stdout 只要确保将文件放置在运行程序的目录中即可。

同样,这种特定的方法Qt无关,它使用的是Standard C ++库

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM