[C++]Qt文本操作(按行读写)

资料来源:https://blog.csdn.net/flyfish1986/article/details/79487104 

 1 #include <QDebug>
 2 #include <QFile>
 3 
 4 void ReadLine()
 5 {
 6 
 7     QFile file("要读的文件路径");
 8     if (file.open(QIODevice::ReadOnly | QIODevice::Text))
 9     {
10         while (!file.atEnd())
11         {
12             QByteArray line = file.readLine();
13             QString str(line);
14             qDebug() << str;
15             displayString << str;
16         }
17         file.close();
18 
19     }
20 }
21 
22 void WriteLine()
23 {
24 
25     QFile file("要写的文件路径");
26     if (file.open(QIODevice::ReadWrite | QIODevice::Text))
27     {
28         QTextStream stream(&file);
29         stream.seek(file.size());
30         for (auto& i : displayString)
31         {
32             QString qs;
33             qs.append("Content:");
34             qs.append(i);
35             qs.remove("\n");
36             stream << qs << "\n";
37         }
38         file.close();
39     }
40 }

猜你喜欢

转载自www.cnblogs.com/FKdelphi/p/10310435.html