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. csv file
QtWS25 Last Chance

csv file

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 333 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.
  • M Offline
    M Offline
    micheal15
    wrote on last edited by
    #1

    I am writing each time on filetmp1 the problem is that when I close the application and I open it again the code will delete the old data

    code :

    //open csv1
            if(!filetmp.open(QFile::ReadOnly | QFile::Text))
                {
                qDebug()<<"error opening:"<<filetmp.fileName();
                return;
                }
    
            //open csv2
            QFile filetmp1("c:/Users/LattePanda/Desktop/MOBILTOX/Paramétre_tmp1.csv"); // Vérifiaction l'exsitance de fichier d'indentification
            if(!filetmp1.open(QFile::ReadWrite | QFile::Text))
                {
                qDebug()<<"error opening:"<<filetmp1.fileName();
                return;
                }
    
            // read header csv1
            QString h1=filetmp.readLine();
            h1=h1.trimmed();  // get rid of ending /n
            qDebug()<<h1;
            // read header csv2
            QString h2=filetmp1.readLine();
            h2=h2.trimmed();
            qDebug()<<h2;
    
            // get field lists
            QStringList h1List=h1.split(';');
            QStringList h2List=h2.split(';');
    
            while(!filetmp.atEnd())   // read lines csv1
                {
                QString line=filetmp.readLine();
                line=line.trimmed();
                QStringList val1List=line.split(';');
                QString val2;
    
                for(const QString h : h2List)   // browse all csv2 fields
                    {
                    if(h1List.contains(h))  // does it exist in csv1 ?
                        {
                        int index=h1List.indexOf(h); // at what index
                        val2.append(val1List.at(index)); // write value
                        }
                    if(h!=h2List.last())
                        val2.append(';');
                    }
                val2.append("\n");
                qDebug()<<val2;
                filetmp1.write(val2.toUtf8());
                }
            filetmp.close();
            filetmp1.close();
    
    jsulmJ 1 Reply Last reply
    0
    • M micheal15

      I am writing each time on filetmp1 the problem is that when I close the application and I open it again the code will delete the old data

      code :

      //open csv1
              if(!filetmp.open(QFile::ReadOnly | QFile::Text))
                  {
                  qDebug()<<"error opening:"<<filetmp.fileName();
                  return;
                  }
      
              //open csv2
              QFile filetmp1("c:/Users/LattePanda/Desktop/MOBILTOX/Paramétre_tmp1.csv"); // Vérifiaction l'exsitance de fichier d'indentification
              if(!filetmp1.open(QFile::ReadWrite | QFile::Text))
                  {
                  qDebug()<<"error opening:"<<filetmp1.fileName();
                  return;
                  }
      
              // read header csv1
              QString h1=filetmp.readLine();
              h1=h1.trimmed();  // get rid of ending /n
              qDebug()<<h1;
              // read header csv2
              QString h2=filetmp1.readLine();
              h2=h2.trimmed();
              qDebug()<<h2;
      
              // get field lists
              QStringList h1List=h1.split(';');
              QStringList h2List=h2.split(';');
      
              while(!filetmp.atEnd())   // read lines csv1
                  {
                  QString line=filetmp.readLine();
                  line=line.trimmed();
                  QStringList val1List=line.split(';');
                  QString val2;
      
                  for(const QString h : h2List)   // browse all csv2 fields
                      {
                      if(h1List.contains(h))  // does it exist in csv1 ?
                          {
                          int index=h1List.indexOf(h); // at what index
                          val2.append(val1List.at(index)); // write value
                          }
                      if(h!=h2List.last())
                          val2.append(';');
                      }
                  val2.append("\n");
                  qDebug()<<val2;
                  filetmp1.write(val2.toUtf8());
                  }
              filetmp.close();
              filetmp1.close();
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @micheal15 said in csv file:

      filetmp.open(QFile::ReadOnly | QFile::Text)

      Basics, open in append mode:

      filetmp.open(QIODeviceBase::ReadOnly | QIODeviceBase::Text | QIODeviceBase::Append)
      

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

      M 1 Reply Last reply
      0
      • jsulmJ jsulm

        @micheal15 said in csv file:

        filetmp.open(QFile::ReadOnly | QFile::Text)

        Basics, open in append mode:

        filetmp.open(QIODeviceBase::ReadOnly | QIODeviceBase::Text | QIODeviceBase::Append)
        
        M Offline
        M Offline
        micheal15
        wrote on last edited by
        #3

        @jsulm in fact I will read each time the filetmp and write on filetmp1 i have tried
        if(!filetmp1.open(QFile::ReadWrite | QFile::Text | QFile::Append )) but it write nothing on this file

        JonBJ jsulmJ 2 Replies Last reply
        0
        • M micheal15

          @jsulm in fact I will read each time the filetmp and write on filetmp1 i have tried
          if(!filetmp1.open(QFile::ReadWrite | QFile::Text | QFile::Append )) but it write nothing on this file

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @micheal15
          Your code does a read from filetmp1 and later writes to it. Personally I do not like reading from a file and then appending to it without re-opening. If you must I would call seek() before writing to it, a common restriction of read/write IO is that you must seek after changing between reading and writing.

          I do not know whether Qt's QFile::Append file open flag guarantees to do this for you. Nor how it interacts with QFile::ReadWrite. Nor what you expect/want to happen if the destination file already has anything more in it than the header line you read.

          1 Reply Last reply
          1
          • M micheal15

            @jsulm in fact I will read each time the filetmp and write on filetmp1 i have tried
            if(!filetmp1.open(QFile::ReadWrite | QFile::Text | QFile::Append )) but it write nothing on this file

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

            @micheal15 said in csv file:

            but it write nothing on this file

            Then debug your code, there is a reason why there are debugers. What does open(...) return?

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

            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