Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Write to many files at once
Forum Updated to NodeBB v4.3 + New Features

Write to many files at once

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 1.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    t021
    wrote on last edited by
    #1

    Hi. I'd like to write "hello" to 1000 files so that the filenames are generated automatically. So far I have this:

    QString Filename = "filename";
    QFile **db = new QFile*[100];
    
    for(i=0; i<100; i++)
    {
    db[i]->setFileName(Filename.append(i.toString() ));
    if(!db[i]->open(QIODevice::Append | QIODevice::Text)) {exit(1);}
      QTextStream out_stream(&db[i]);
      out_stream << "hello";
    db[i]->close();
    

    However this doesn't work. May I ask someone to help me with this a bit. Thanks!

    VRoninV jsulmJ 2 Replies Last reply
    0
    • T t021

      Hi. I'd like to write "hello" to 1000 files so that the filenames are generated automatically. So far I have this:

      QString Filename = "filename";
      QFile **db = new QFile*[100];
      
      for(i=0; i<100; i++)
      {
      db[i]->setFileName(Filename.append(i.toString() ));
      if(!db[i]->open(QIODevice::Append | QIODevice::Text)) {exit(1);}
        QTextStream out_stream(&db[i]);
        out_stream << "hello";
      db[i]->close();
      

      However this doesn't work. May I ask someone to help me with this a bit. Thanks!

      VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #2

      @t021 said in Write to many files at once:

      this doesn't work

      Think about what you are doing here: QFile **db = new QFile*[100]; here lies the problem.

      BTW:

      for(i=0; i<100; ++i){
      QFile tempFile(QStringLiteral("filename%1").arg(i));
      if(!tempFile.open(QIODevice::WriteOnly| QIODevice::Text)) continue;
      QTextStream out_stream(&tempFile);
      out_stream << "hello";
      }
      

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      T 1 Reply Last reply
      5
      • T t021

        Hi. I'd like to write "hello" to 1000 files so that the filenames are generated automatically. So far I have this:

        QString Filename = "filename";
        QFile **db = new QFile*[100];
        
        for(i=0; i<100; i++)
        {
        db[i]->setFileName(Filename.append(i.toString() ));
        if(!db[i]->open(QIODevice::Append | QIODevice::Text)) {exit(1);}
          QTextStream out_stream(&db[i]);
          out_stream << "hello";
        db[i]->close();
        

        However this doesn't work. May I ask someone to help me with this a bit. Thanks!

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @t021 No need to have 1000 QFile instances - this is just memory wasting! Just use one like @VRonin does.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • VRoninV VRonin

          @t021 said in Write to many files at once:

          this doesn't work

          Think about what you are doing here: QFile **db = new QFile*[100]; here lies the problem.

          BTW:

          for(i=0; i<100; ++i){
          QFile tempFile(QStringLiteral("filename%1").arg(i));
          if(!tempFile.open(QIODevice::WriteOnly| QIODevice::Text)) continue;
          QTextStream out_stream(&tempFile);
          out_stream << "hello";
          }
          
          T Offline
          T Offline
          t021
          wrote on last edited by
          #4

          @VRonin said in Write to many files at once:

          for(i=0; i<100; ++i){
          QFile tempFile(QStringLiteral("filename%1").arg(i));
          if(!tempFile.open(QIODevice::WriteOnly| QIODevice::Text)) continue;
          QTextStream out_stream(&tempFile);
          out_stream << "hello";
          }

          Thank you very much. I cannot use QStringLiteral because I'm using Qt4.

          My actual problem is more complicated. I don't just have to write "hello" to each file and forget about them. I need to periodically visit these 1000 files and write more to them. In the next step I may need to write "good bye" to files 217 and 389 only. This is why I need an array of pointers to handle my files. Thanks again!

          1 Reply Last reply
          0
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #5
            QFile* db[100];
            for(int i=0;i<100;++i){
            db[i]=new QFile(QString::fromLatin1("filename%1").arg(i));
            if(!db[i]->open(QIODevice::WriteOnly| QIODevice::Text)) Q_ASSERT(false);
            }
            for(i=0; i<100; ++i){
            QTextStream out_stream(db[i]);
            out_stream << "hello";
            }
            

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            T 1 Reply Last reply
            1
            • VRoninV VRonin
              QFile* db[100];
              for(int i=0;i<100;++i){
              db[i]=new QFile(QString::fromLatin1("filename%1").arg(i));
              if(!db[i]->open(QIODevice::WriteOnly| QIODevice::Text)) Q_ASSERT(false);
              }
              for(i=0; i<100; ++i){
              QTextStream out_stream(db[i]);
              out_stream << "hello";
              }
              
              T Offline
              T Offline
              t021
              wrote on last edited by
              #6

              @VRonin said in Write to many files at once:

              QFile* db[100];
              for(int i=0;i<100;++i){
              db[i]=new QFile(QString::fromLatin1("filename%1").arg(i));
              if(!db[i]->open(QIODevice::WriteOnly| QIODevice::Text)) Q_ASSERT(false);
              }
              for(i=0; i<100; ++i){
              QTextStream out_stream(db[i]);
              out_stream << "hello";
              }

              Thank you very much VRONIN! This is exactly what I need, it solved my problem!

              1 Reply Last reply
              0
              • VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by
                #7

                If the files should not persist once the program is over you might want to consider QTemporaryFile instead of QFile

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                1 Reply Last reply
                1

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved