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. fopen in C++ application?
QtWS25 Last Chance

fopen in C++ application?

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 3 Posters 4.4k 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.
  • S Offline
    S Offline
    SPlatten
    wrote on 17 Feb 2019, 17:51 last edited by
    #1

    I want to use fopen, I've included the headers:

    #include <stdio.h>
    #include <stdlib.h>
    

    In my source I have declared:

    FILE* fp = fopen("file.txt", "rt");
    

    I'm getting:

    no matching function for call to 'fopen'
    

    Why? I thought fopen was part of the standard C library?

    I need a file handing routine that I can translate the returned file pointer into a handle and then back from a handle to a file pointer so I can pass the handle back and forth between the application and another module.

    Kind Regards,
    Sy

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 17 Feb 2019, 17:53 last edited by
      #2

      Hi,

      The include is <cstdio>.

      Since you are using Qt, why not use QFile ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      3
      • S Offline
        S Offline
        SPlatten
        wrote on 17 Feb 2019, 17:55 last edited by SPlatten
        #3

        @SGaist , Thank you, is it possible to get a file handle from QFile so that when the object goes out of scope I can pass the handle back in on another call and get access to the file without having to open it again?

        Kind Regards,
        Sy

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 17 Feb 2019, 17:57 last edited by
          #4

          Yes you can get it but if the QFile goes out of scope, the handle won't be valid anymore.

          What is your exact use case ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • S Offline
            S Offline
            SPlatten
            wrote on 17 Feb 2019, 17:59 last edited by
            #5

            I'm writing an application that will provide native functions and access to the OS GUI it includes services such as file access and database access. I've completed the database service which works well. I want to provide file access which is as efficient a possible, so once the file is opened I want it to stay opened until a request is sent to close the file.

            Kind Regards,
            Sy

            1 Reply Last reply
            0
            • C Online
              C Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 17 Feb 2019, 18:18 last edited by
              #6

              So what's wrong with a QFile object then?

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              2
              • S Offline
                S Offline
                SPlatten
                wrote on 17 Feb 2019, 18:19 last edited by
                #7

                If I declare it as global then nothing, but if it's declared in a function scope the file will automatically be closed when the object goes out of scope.

                Kind Regards,
                Sy

                1 Reply Last reply
                0
                • C Online
                  C Online
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on 17 Feb 2019, 18:22 last edited by
                  #8

                  You're aware you can create any object on the heap?
                  QFile *f = new QFile ...
                  You only have to make sure to delete it once you don't need it anymore.

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  1 Reply Last reply
                  4
                  • S Offline
                    S Offline
                    SPlatten
                    wrote on 17 Feb 2019, 18:23 last edited by
                    #9

                    Yes, sorry, juggling lots of things atm, not thinking clearly.

                    Kind Regards,
                    Sy

                    1 Reply Last reply
                    0

                    8/9

                    17 Feb 2019, 18:22

                    • Login

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