File zu fstream



  • Ich habe folgenden Quellcode:

    FILE* oggFile;
    
    void ogg_stream::open(string path)
    {
        int result;
    
        if(!(oggFile = fopen(path.c_str(), "rb")))
            throw string("Could not open Ogg file.");
    
        if((result = ov_open(oggFile, &oggStream, NULL, 0)) < 0)
        {
            fclose(oggFile);
    
            throw string("Could not open Ogg stream. ") + errorString(result);
        }
    ..
    }
    

    gibt es eine Möglichkeit, womit ich einen fstream in FILE konvertieren kann?

    Denn:

    ov_open
    

    braucht FILE* und ich würde leiber fstream verwenden

    mfg



  • Oder gibt es eine Methode, die mir FILE* zurücklifert?
    (wie bei std::basic_string c_str();)



  • Gibt es keine alternative?



  • du köntest ein globales static array benuzen mit den ogg werten drin^^
    dann mit einer if schliefe durch laufen und die werte "oggen"



  • Tc++H schrieb:

    gibt es eine Möglichkeit, womit ich einen fstream in FILE konvertieren kann?

    Der C++-Standard bietet keine Möglichkeit aus einem fstream einen FILE* zu holen. Was Du machen kannst ist einen eigenen streambuf zu schreiben, der einen FILE* als Device hat.
    Anbei ein Vorschlag für die Ausgabe in ein 'FILE' - wenn Du aus einem 'FILE' via std::istream lesen willst, so frag' noch mal nach.

    #include <iostream>
    #include <complex>
    #include <cstdio>
    
    // --   (output)streambuf mit FILE* als Device
    class FILE_streambuf : public std::streambuf
    {
    public:
        explicit FILE_streambuf( FILE* fp )
            : m_fp( fp )
        {}
        ~FILE_streambuf() { if( m_fp ) std::fclose( m_fp ); }
        bool is_open() const { return m_fp != 0; }
    
    protected:
        // --   im einfachsten Fall reicht es, 'overflow' zu überschreiben
        virtual int_type overflow( int_type c_ = traits_type::eof() )
        {
            if( traits_type::eq_int_type( c_, traits_type::eof() ) )
                return traits_type::not_eof( c_ );
            const char c = traits_type::to_char_type( c_ );
            if( std::fwrite( &c, 1, 1, m_fp ) == 1 ) // zeichenweise wegschreiben
                return c_;  // ok
            return traits_type::eof(); // Fehler
        }
    private:
        FILE* m_fp;
    };
    
    // --   Wrapper zum Verbinden von std::ostream mit FILE_streambuf
    class OFStream : public std::ostream
    {
    public:
        explicit OFStream( FILE* fp )
            : std::ostream( &m_sb )
            , m_sb( fp )
        {}
        bool is_open() const { return m_sb.is_open(); }
    private:
        FILE_streambuf m_sb;
    };
    
    // --  Anwendungs-Beispiel
    int main()
    {
        using namespace std;
        FILE* fp = fopen("output.txt", "w");
        OFStream file( fp );
        if( file.is_open() )
        {
            // Beispiel
            float pi = float(3.14);
            complex< double > c( 9.3, -0.7 );
            file << "Text " << pi << ' ' << c << endl;
        }
        // Schließen des FILE's geschieht im Destruktor
    
        return 0;
    }
    

    Den obigen OFStream kannst Du genau wie einen std::ofstream benutzen. Der std::ofstream ist genauso von std::ostream abgeleitet.

    Gruß
    Werner


Anmelden zum Antworten