使用 QTextStream C++ 读取 txt 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15115571/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:03:25  来源:igfitidea点击:

Reading a txt file using QTextStream C++

c++qt

提问by AngryDuck

I am making a small program I have done before in Java however I want to try and get the same working in C++. The idea is to merge two text files

我正在制作一个我之前在 Java 中做过的小程序,但是我想尝试在 C++ 中获得相同的工作。这个想法是合并两个文本文件

file1:

文件 1:

a
b
c

file2:

文件2:

1
2
3

output file should read:

输出文件应为:

a1
b2
c3

I have looked at the QTextStream docs and this was the suggested code to read a file by line into strings

我查看了 QTextStream 文档,这是按行将文件读入字符串的建议代码

QFile file(input); // this is a name of a file text1.txt sent from main method
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
    return 1;
}
QTextStream in(&file);
QString line = in.readLine();
while (!line.isNull())
{
    line = in.readLine();
}

Yet for some reason nothing is being loaded from the file at all. I proved this by printing 'line' to console and got nothing.

然而由于某种原因,根本没有从文件中加载任何内容。我通过将“line”打印到控制台来证明这一点,但一无所获。

So any ideas? All I want is to read the file and end up with a string like this

那么有什么想法吗?我想要的只是读取文件并以这样的字符串结尾

QString text1 = "a\n2\n3"

I'd do this for both files, split the strings into QStringList (most likely) join them together in the format I want and write them to a 3rd txt file.

我会为这两个文件执行此操作,将字符串拆分为 QStringList (最有可能)以我想要的格式将它们连接在一起并将它们写入第三个 txt 文件。

回答by dtech

Why do you read line by line if you want the entire file?

如果您想要整个文件,为什么要逐行阅读?

QString line = in.readAll();

ALso, your while loop is wrong, you need to while (!in.atEnd())for the text stream instead of checking if the string is null.

另外,你的while循环是错误的,你需要while (!in.atEnd())为文本流而不是检查字符串是否为空。

readLinewon't include the new line symbol.

readLine将不包括新行符号。

Anyway, it would be much easier to open both files at the same time and construct your string on the go instead of splitting and joining.

无论如何,同时打开两个文件并在旅途中构建您的字符串会容易得多,而不是拆分和加入。

QFile f1("h:/1.txt");
QFile f2("h:/2.txt");

f1.open(QIODevice::ReadOnly | QIODevice::Text);
f2.open(QIODevice::ReadOnly | QIODevice::Text);

QString s;

QTextStream s1(&f1);
QTextStream s2(&f2);

for (int i = 0; i < 3; ++i) {
    s.append(s1.readLine());
    s.append(s2.readLine());
    if (i != 2)s.append("\n");
}

回答by AngryDuck

If the file name does not contain full path, but you are very sure that the file is located in the same directory as your application, use the applications path like this:

如果文件名不包含完整路径,但您非常确定该文件与您的应用程序位于同一目录中,请使用如下所示的应用程序路径:

QString filename = QCoreApplication::applicationDirPath() + "/" + input;

回答by suspectus

Try this block-:

试试这个块-:

while(!in.atEnd())
{
   QString line = in.readLine();   
   ....
}

do you get output using this while loop?

你使用这个while循环获得输出吗?