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?

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.
  • SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on 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
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on 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
      • SPlattenS Offline
        SPlattenS Offline
        SPlatten
        wrote on 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
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on 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
          • SPlattenS Offline
            SPlattenS Offline
            SPlatten
            wrote on 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
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 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
              • SPlattenS Offline
                SPlattenS Offline
                SPlatten
                wrote on 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
                • Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on 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
                  • SPlattenS Offline
                    SPlattenS Offline
                    SPlatten
                    wrote on last edited by
                    #9

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

                    Kind Regards,
                    Sy

                    1 Reply Last reply
                    0

                    • Login

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