使用QTextStream c++读取一个txt文件。

时间:2022-10-24 21:54:39

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:

file1:

a
b
c

file2:

file2:

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.

然而,由于某种原因,文件根本没有加载任何东西。我通过打印“行”来证明这一点,但没有得到任何结果。

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文件中。

3 个解决方案

#1


5  

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 (! atend())用于文本流,而不是检查字符串是否为空。

readLine won'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.

无论如何,在同一时间打开两个文件并在go上构建字符串,而不是拆分和连接,将会容易得多。

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");
}

#2


1  

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;

#3


0  

Try this block-:

试试这个块-:

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

do you get output using this while loop?

您是否使用while循环获得输出?

#1


5  

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 (! atend())用于文本流,而不是检查字符串是否为空。

readLine won'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.

无论如何,在同一时间打开两个文件并在go上构建字符串,而不是拆分和连接,将会容易得多。

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");
}

#2


1  

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;

#3


0  

Try this block-:

试试这个块-:

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

do you get output using this while loop?

您是否使用while循环获得输出?