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
Forum Updated to NodeBB v4.3 + New Features

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.4k Views 2 Watching
  • 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.
  • R Offline
    R Offline
    rezaMSLM
    wrote on last edited by
    #1

    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?

    aha_1980A 1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #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

        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?

        aha_1980A Offline
        aha_1980A Offline
        aha_1980
        Lifetime Qt Champion
        wrote on last edited by aha_1980
        #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 last edited by
          #4

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

          aha_1980A 1 Reply Last reply
          0
          • R rezaMSLM

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

            aha_1980A Offline
            aha_1980A Offline
            aha_1980
            Lifetime Qt Champion
            wrote on 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
            2
            • aha_1980A aha_1980

              @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 last edited by rezaMSLM
              #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.

              jsulmJ 1 Reply Last reply
              0
              • R rezaMSLM

                @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.

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on 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
                1
                • jsulmJ jsulm

                  @rezaMSLM @mrjj already provided simple example code

                  R Offline
                  R Offline
                  rezaMSLM
                  wrote on last edited by
                  #8

                  @jsulm
                  I Didn't got it

                  jsulmJ 1 Reply Last reply
                  0
                  • R rezaMSLM

                    @jsulm
                    I Didn't got it

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on 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
                    0
                    • jsulmJ jsulm

                      @rezaMSLM What exactly you didn't get?

                      R Offline
                      R Offline
                      rezaMSLM
                      wrote on last edited by
                      #10

                      @jsulm
                      how to specify download directory

                      jsulmJ 1 Reply Last reply
                      0
                      • R rezaMSLM

                        @jsulm
                        how to specify download directory

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on 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
                        2
                        • jsulmJ jsulm

                          @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 last edited by rezaMSLM
                          #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 last edited by rezaMSLM
                            #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

                            jsulmJ 1 Reply Last reply
                            0
                            • R rezaMSLM

                              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

                              jsulmJ Offline
                              jsulmJ Offline
                              jsulm
                              Lifetime Qt Champion
                              wrote on 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
                              2
                              • jsulmJ jsulm

                                @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 last edited by rezaMSLM
                                #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!

                                JonBJ 1 Reply Last reply
                                0
                                • R rezaMSLM

                                  @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!

                                  JonBJ Offline
                                  JonBJ Offline
                                  JonB
                                  wrote on last edited by JonB
                                  #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
                                  0
                                  • JonBJ JonB

                                    @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 last edited by rezaMSLM
                                    #17

                                    @JonB
                                    I tried it but didn"t worked

                                    JonBJ 1 Reply Last reply
                                    0
                                    • R rezaMSLM

                                      @JonB
                                      I tried it but didn"t worked

                                      JonBJ Offline
                                      JonBJ Offline
                                      JonB
                                      wrote on last edited by JonB
                                      #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
                                      0
                                      • hskoglundH Online
                                        hskoglundH Online
                                        hskoglund
                                        wrote on last edited by
                                        #19

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

                                        JonBJ 1 Reply Last reply
                                        0
                                        • hskoglundH hskoglund

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

                                          JonBJ Offline
                                          JonBJ Offline
                                          JonB
                                          wrote on last edited by JonB
                                          #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

                                          • Login

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