以下是使用Qt在当前工作路径下生成一个txt文件的示例代码:
#include <QFile>
#include <QTextStream>
int main()
{
// 创建文件对象
QString fileName = "test.txt";
QFile file(fileName);
// 打开文件
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
return 1;
// 创建文本流对象
QTextStream out(&file);
// 写入内容
out << "Hello, World!" << endl;
// 关闭文件
file.close();
return 0;
}
这段代码创建了一个名为”test.txt”的文件,写入了”Hello, World!“这个字符串,并将文件关闭。生成的文件会保存在当前工作路径下。