简体   繁体   English

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

[英]Show in console a text read from file

I'm trying to read a file in Qt and then show it in console, this is my mainwindow.cpp: 我正在尝试在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;
}

I don't understand why this doesn't work, I googled it but I just found other method to read files from Qt. 我不明白为什么这行不通,我用谷歌搜索了一下,但是我发现了另一种从Qt读取文件的方法。 I need this specific method but if this is not possible, tell me what is the short way to read a file with a lot of lines in this format: 我需要这种特定的方法,但是如果无法实现,请告诉我读取具有这种格式的多行文件的简短方法是什么:

123123123 aname 123123

The code you pasted is printing the contents of the file datos.txt into stdout . 您粘贴的代码将文件datos.txt的内容打印到stdout Just make sure the file is placed in the directory the program is ran in. 只要确保将文件放置在运行程序的目录中即可。

also this specific method is not related to Qt , this is using the Standard C++ library . 同样,这种特定的方法Qt无关,它使用的是Standard C ++库

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

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