Creating/writing into a new file in Qt

前端 未结 6 1606
陌清茗
陌清茗 2021-01-31 14:31

I am trying to write into a file and if the file doesn\'t exist create it. I have searched on the internet and nothing worked for me.

My code looks currently like this:<

6条回答
  •  有刺的猬
    2021-01-31 15:00

    QFile file("test.txt");
    /*
     *If file does not exist, it will be created
     */
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text | QIODevice::ReadWrite))
    {
        qDebug() << "FAILED TO CREATE FILE / FILE DOES NOT EXIST";
    }
    
    /*for Reading line by line from text file*/
    while (!file.atEnd()) {
        QByteArray line = file.readLine();
        qDebug() << "read output - " << line;
    }
    
    /*for writing line by line to text file */
    if (file.open(QIODevice::ReadWrite))
    {
        QTextStream stream(&file);
        stream << "1_XYZ"<

提交回复
热议问题