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. How To Read a Text File Contents Located on a FTP Server

How To Read a Text File Contents Located on a FTP Server

Scheduled Pinned Locked Moved Solved General and Desktop
22 Posts 7 Posters 15.2k 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
    mrjj
    Lifetime Qt Champion
    wrote on 4 Mar 2018, 09:59 last edited by mrjj 3 Apr 2018, 10:02
    #2

    Hi
    Since you are using the QFtp class then the old samples can be useful.
    This list and downloads files from ftp
    http://doc.qt.io/archives/qt-4.8/qt-network-qftp-example.html

    Overall the process is.

    // connect finished signal
      ftp = new QFtp(this);
        connect(ftp, SIGNAL(commandFinished(int,bool)),   this, SLOT(ftpCommandFinished(int,bool)));
    // login etc
    // ask for file
     ftp->get(filename, file);
    

    The void FtpWindow::downloadFile() should be what you need.

    1 Reply Last reply
    4
    • R rezaMSLM
      4 Mar 2018, 05:08

      I have written a program to login to my ftp account:
      Here
      Now i want to read a text file Located on my FTP server and search for some special strings in it; if there is do something if not do another.
      how can I do this?

      A Offline
      A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on 4 Mar 2018, 10:10 last edited by aha_1980 3 Apr 2018, 10:12
      #3

      Hi @rezaMSLM,

      the short recipe is:

      • download the file by FTP with QFtp
      • store the file in a temp folder (make sure to override existing files or give them unique names): QTemporaryFile
      • open the file and search for your strings: QFile, QTextStream

      Regards

      Edit: @mrjj was faster once again ;)

      Qt has to stay free or it will die.

      1 Reply Last reply
      4
      • R Offline
        R Offline
        rezaMSLM
        wrote on 5 Mar 2018, 07:57 last edited by
        #4

        when using QFtp::get() method to download a file from server where is the file stored?

        A 1 Reply Last reply 5 Mar 2018, 08:02
        0
        • R rezaMSLM
          5 Mar 2018, 07:57

          when using QFtp::get() method to download a file from server where is the file stored?

          A Offline
          A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on 5 Mar 2018, 08:02 last edited by
          #5

          @rezaMSLM said in How To Read a Text File Contents Located on a FTP Server:

          when using QFtp::get() method to download a file from server where is the file stored?

          You provide a pointer to a QFile as second parameter of get(). So it's up to you where the file lands.

          Just have a deep look the the Example that was recommended you by @mrjj already.

          Qt has to stay free or it will die.

          R 1 Reply Last reply 6 Mar 2018, 12:44
          2
          • A aha_1980
            5 Mar 2018, 08:02

            @rezaMSLM said in How To Read a Text File Contents Located on a FTP Server:

            when using QFtp::get() method to download a file from server where is the file stored?

            You provide a pointer to a QFile as second parameter of get(). So it's up to you where the file lands.

            Just have a deep look the the Example that was recommended you by @mrjj already.

            R Offline
            R Offline
            rezaMSLM
            wrote on 6 Mar 2018, 12:44 last edited by rezaMSLM 3 Jun 2018, 12:57
            #6

            @aha_1980 said in How To Read a Text File Contents Located on a FTP Server:

            Just have a deep look the the Example that was recommended you by @mrjj already.

            I tried reading that example and some other codes on the internet But I'm a beginer and the examples were Confusing for me !
            for first step I just want to download a text file located on FTP server into my desktop.
            would any one please write a sample code for me doing that? thinking on simple sample codes will help me much.

            J 1 Reply Last reply 6 Mar 2018, 13:16
            0
            • R rezaMSLM
              6 Mar 2018, 12:44

              @aha_1980 said in How To Read a Text File Contents Located on a FTP Server:

              Just have a deep look the the Example that was recommended you by @mrjj already.

              I tried reading that example and some other codes on the internet But I'm a beginer and the examples were Confusing for me !
              for first step I just want to download a text file located on FTP server into my desktop.
              would any one please write a sample code for me doing that? thinking on simple sample codes will help me much.

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 6 Mar 2018, 13:16 last edited by
              #7

              @rezaMSLM @mrjj already provided simple example code

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

              R 1 Reply Last reply 6 Mar 2018, 14:04
              1
              • J jsulm
                6 Mar 2018, 13:16

                @rezaMSLM @mrjj already provided simple example code

                R Offline
                R Offline
                rezaMSLM
                wrote on 6 Mar 2018, 14:04 last edited by
                #8

                @jsulm
                I Didn't got it

                J 1 Reply Last reply 6 Mar 2018, 14:09
                0
                • R rezaMSLM
                  6 Mar 2018, 14:04

                  @jsulm
                  I Didn't got it

                  J Offline
                  J Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 6 Mar 2018, 14:09 last edited by
                  #9

                  @rezaMSLM What exactly you didn't get?

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

                  R 1 Reply Last reply 6 Mar 2018, 14:15
                  0
                  • J jsulm
                    6 Mar 2018, 14:09

                    @rezaMSLM What exactly you didn't get?

                    R Offline
                    R Offline
                    rezaMSLM
                    wrote on 6 Mar 2018, 14:15 last edited by
                    #10

                    @jsulm
                    how to specify download directory

                    J 1 Reply Last reply 6 Mar 2018, 14:17
                    0
                    • R rezaMSLM
                      6 Mar 2018, 14:15

                      @jsulm
                      how to specify download directory

                      J Offline
                      J Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on 6 Mar 2018, 14:17 last edited by
                      #11

                      @rezaMSLM It is up to you where to store the file. You can use http://doc.qt.io/qt-5/qstandardpaths.html#locate to get a directory (for example the temp directory).

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

                      R 1 Reply Last reply 6 Mar 2018, 14:33
                      2
                      • J jsulm
                        6 Mar 2018, 14:17

                        @rezaMSLM It is up to you where to store the file. You can use http://doc.qt.io/qt-5/qstandardpaths.html#locate to get a directory (for example the temp directory).

                        R Offline
                        R Offline
                        rezaMSLM
                        wrote on 6 Mar 2018, 14:33 last edited by rezaMSLM 3 Jun 2018, 15:06
                        #12

                        @jsulm
                        I want a sample Code not an english Link
                        I understand C++ much better than English

                        1 Reply Last reply
                        0
                        • R Offline
                          R Offline
                          rezaMSLM
                          wrote on 7 Mar 2018, 13:03 last edited by rezaMSLM 3 Jul 2018, 13:05
                          #13

                          Where i am wrong?

                          MainWindow::MainWindow(...)
                          {
                          my_file=new QFile("Qt_Test.txt");//create new file named Qt_Test.txt
                          QTemporaryFile(QDir::tempPath());//store that file in temp path
                          .
                          .
                          }
                          
                          void MainWindow::on_pushButton_Download_clicked()
                          {
                              ftp->get("Qt_Test.txt",my_file,QFtp::Ascii);//download Qt_Test.txt which is located on server 
                          }
                          

                          I Receive no error but no file is downloaded to temp folder

                          J 1 Reply Last reply 7 Mar 2018, 13:43
                          0
                          • R rezaMSLM
                            7 Mar 2018, 13:03

                            Where i am wrong?

                            MainWindow::MainWindow(...)
                            {
                            my_file=new QFile("Qt_Test.txt");//create new file named Qt_Test.txt
                            QTemporaryFile(QDir::tempPath());//store that file in temp path
                            .
                            .
                            }
                            
                            void MainWindow::on_pushButton_Download_clicked()
                            {
                                ftp->get("Qt_Test.txt",my_file,QFtp::Ascii);//download Qt_Test.txt which is located on server 
                            }
                            

                            I Receive no error but no file is downloaded to temp folder

                            J Offline
                            J Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on 7 Mar 2018, 13:43 last edited by
                            #14

                            @rezaMSLM Did you check the example? http://doc.qt.io/archives/qt-4.8/qt-network-qftp-example.html

                            MainWindow::MainWindow(...)
                            {
                            }
                            
                            void MainWindow::on_pushButton_Download_clicked()
                            {
                                QString path = QDir::tempPath() + '/' + "Qt_Test.txt";
                                my_file=new QFile(path);
                                my_file->open(QIODevice::WriteOnly);
                                ftp->get("Qt_Test.txt",my_file,QFtp::Ascii);//download Qt_Test.txt which is located on server 
                            }

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

                            R 1 Reply Last reply 7 Mar 2018, 14:03
                            2
                            • J jsulm
                              7 Mar 2018, 13:43

                              @rezaMSLM Did you check the example? http://doc.qt.io/archives/qt-4.8/qt-network-qftp-example.html

                              MainWindow::MainWindow(...)
                              {
                              }
                              
                              void MainWindow::on_pushButton_Download_clicked()
                              {
                                  QString path = QDir::tempPath() + '/' + "Qt_Test.txt";
                                  my_file=new QFile(path);
                                  my_file->open(QIODevice::WriteOnly);
                                  ftp->get("Qt_Test.txt",my_file,QFtp::Ascii);//download Qt_Test.txt which is located on server 
                              }
                              R Offline
                              R Offline
                              rezaMSLM
                              wrote on 7 Mar 2018, 14:03 last edited by rezaMSLM 3 Jul 2018, 14:10
                              #15

                              @jsulm

                              @jsulm said in How To Read a Text File Contents Located on a FTP Server:

                              Did you check the example? http://doc.qt.io/archives/qt-4.8/qt-network-qftp-example.html

                              yes

                              the file is created but is empty while the program is open!

                              J 1 Reply Last reply 7 Mar 2018, 15:29
                              0
                              • R rezaMSLM
                                7 Mar 2018, 14:03

                                @jsulm

                                @jsulm said in How To Read a Text File Contents Located on a FTP Server:

                                Did you check the example? http://doc.qt.io/archives/qt-4.8/qt-network-qftp-example.html

                                yes

                                the file is created but is empty while the program is open!

                                J Online
                                J Online
                                JonB
                                wrote on 7 Mar 2018, 15:29 last edited by JonB 3 Jul 2018, 15:42
                                #16

                                @rezaMSLM
                                I could be wrong, but in @jsulm's code immediately after the ftp->get() line I would add line:

                                my_file->close()
                                

                                I see that the example in the docs does not do that. Which seems a bit surprising, but the docs may know better than I do. However, assuming the file close is needed, you would get exactly the behaviour you describe (empty till exit program) if you do not close it. So I would give it a try and see....

                                EDIT My assumption here was wrong, see further post below.

                                R 1 Reply Last reply 7 Mar 2018, 15:33
                                0
                                • J JonB
                                  7 Mar 2018, 15:29

                                  @rezaMSLM
                                  I could be wrong, but in @jsulm's code immediately after the ftp->get() line I would add line:

                                  my_file->close()
                                  

                                  I see that the example in the docs does not do that. Which seems a bit surprising, but the docs may know better than I do. However, assuming the file close is needed, you would get exactly the behaviour you describe (empty till exit program) if you do not close it. So I would give it a try and see....

                                  EDIT My assumption here was wrong, see further post below.

                                  R Offline
                                  R Offline
                                  rezaMSLM
                                  wrote on 7 Mar 2018, 15:33 last edited by rezaMSLM 3 Jul 2018, 15:38
                                  #17

                                  @JonB
                                  I tried it but didn"t worked

                                  J 1 Reply Last reply 7 Mar 2018, 15:41
                                  0
                                  • R rezaMSLM
                                    7 Mar 2018, 15:33

                                    @JonB
                                    I tried it but didn"t worked

                                    J Online
                                    J Online
                                    JonB
                                    wrote on 7 Mar 2018, 15:41 last edited by JonB 3 Jul 2018, 15:44
                                    #18

                                    @rezaMSLM
                                    Hang on, I've just read more in the docs, as follows:

                                    On the other hand, if you only want to work with the complete data, you can connect to the commandFinished() signal and read the data when the get() command is finished.

                                    The function does not block and returns immediately. The command is scheduled, and its execution is performed asynchronously. The function returns a unique identifier which is passed by commandStarted() and commandFinished().

                                    When the command is started the commandStarted() signal is emitted. When it is finished the commandFinished() signal is emitted.

                                    So if I understand right, the get() merely initiates the transfer. I believe you need to write a commandFinished slot, and close the file there.

                                    Ah yes, the example page goes on to show that. The example code has:

                                        if (ftp->currentCommand() == QFtp::Get) {
                                            if (error) {
                                                statusLabel->setText(tr("Canceled download of %1.")
                                                                     .arg(file->fileName()));
                                                file->close();
                                                file->remove();
                                            } else {
                                                statusLabel->setText(tr("Downloaded %1 to current directory.")
                                                                     .arg(file->fileName()));
                                                file->close();
                                            }
                                    

                                    When a Get command is finished, a file has finished downloading (or an error occurred during the download).

                                    I do believe the reason your file is empty is that it must be closed, at the correct time, before you will see contents.

                                    I think you have to follow the whole example code, or perhaps download the source files from there and p,lay with the whole project if you want to see how to use it fully.

                                    R 1 Reply Last reply 9 Mar 2018, 11:24
                                    0
                                    • hskoglundH Offline
                                      hskoglundH Offline
                                      hskoglund
                                      wrote on 7 Mar 2018, 15:44 last edited by
                                      #19

                                      Hi, also you could check that the FTP username and password are correct.

                                      J 1 Reply Last reply 7 Mar 2018, 15:46
                                      0
                                      • hskoglundH hskoglund
                                        7 Mar 2018, 15:44

                                        Hi, also you could check that the FTP username and password are correct.

                                        J Online
                                        J Online
                                        JonB
                                        wrote on 7 Mar 2018, 15:46 last edited by JonB 3 Jul 2018, 15:51
                                        #20

                                        @hskoglund
                                        Yes, but isn't username/password checking performed/errored much earlier than when he gets to issuing the get()? It's performed during FTP connection open.

                                        The behaviour he describes --- file content not "visible" till exit application, then correct --- is explained exactly by failure to close output file, at least under Windows, which has a habit of marking files to be 0 length till closed by writer.

                                        1 Reply Last reply
                                        1
                                        • J JonB
                                          7 Mar 2018, 15:41

                                          @rezaMSLM
                                          Hang on, I've just read more in the docs, as follows:

                                          On the other hand, if you only want to work with the complete data, you can connect to the commandFinished() signal and read the data when the get() command is finished.

                                          The function does not block and returns immediately. The command is scheduled, and its execution is performed asynchronously. The function returns a unique identifier which is passed by commandStarted() and commandFinished().

                                          When the command is started the commandStarted() signal is emitted. When it is finished the commandFinished() signal is emitted.

                                          So if I understand right, the get() merely initiates the transfer. I believe you need to write a commandFinished slot, and close the file there.

                                          Ah yes, the example page goes on to show that. The example code has:

                                              if (ftp->currentCommand() == QFtp::Get) {
                                                  if (error) {
                                                      statusLabel->setText(tr("Canceled download of %1.")
                                                                           .arg(file->fileName()));
                                                      file->close();
                                                      file->remove();
                                                  } else {
                                                      statusLabel->setText(tr("Downloaded %1 to current directory.")
                                                                           .arg(file->fileName()));
                                                      file->close();
                                                  }
                                          

                                          When a Get command is finished, a file has finished downloading (or an error occurred during the download).

                                          I do believe the reason your file is empty is that it must be closed, at the correct time, before you will see contents.

                                          I think you have to follow the whole example code, or perhaps download the source files from there and p,lay with the whole project if you want to see how to use it fully.

                                          R Offline
                                          R Offline
                                          rezaMSLM
                                          wrote on 9 Mar 2018, 11:24 last edited by
                                          #21

                                          @JonB said in How To Read a Text File Contents Located on a FTP Server:

                                          I do believe the reason your file is empty is that it must be closed, at the correct time, before you will see contents

                                          you were right!
                                          this code solved the problem

                                          void MainWindow::ftpCommandFinished(int id, bool error)
                                          {
                                          .
                                          .
                                              if(my_file->isOpen())
                                              {
                                                   my_file->close();
                                              }
                                          .
                                          .
                                          }
                                          
                                          1 Reply Last reply
                                          1

                                          11/22

                                          6 Mar 2018, 14:17

                                          • Login

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