Thread: converting an ifstream to a file

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    110

    converting an ifstream to a file

    Okay,

    As some of you may know, the Ogg Vorbis functions require the use of a FILE *.
    This is the C way of doing things, though in c++ files are handled by a fstream.

    My question is this,
    is there a way of converting between a fstream and a file, similar to the conversion between a string and a c string using stringname.c_str(); ????

    Thanks for your help,
    WebmasterMattD
    WebmasterMattD.NET

  2. #2
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    i don't know of any way to convert an fstream to a FILE *, but you can use FILE *'s in C++, so why not just sidestep the problem by using them instead of fstreams? Easy is good :-)
    *Cela*

  3. #3
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    As some of you may know, the Ogg Vorbis functions require the use of a FILE *.
    That seems flawed, how could it possibly require the usage of the file handles you speak of??? Just as C++ encompases all of the functionality in C, fstreams encompass all of the functions that you'll find in stdio.h

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    110
    Not being involved in the development of Ogg Vorbis I can't say exactly why they chose to use FILE pointers, though I would presume that as it is a collection of fucntions and libraries, and not classes, it would be safe to asume that they are writting for the C populus and not the C++.

    By saying that an fstream has all the functionality of a FILE *, then how could I pass a fstream to a function that requires a FILE *??

    Thanks for your input,
    WebmasterMattD
    WebmasterMattD.NET

  5. #5
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    You can call fstream's fd member function to get the underlying file descriptor for a stream. Then you can call _fdopen, passing the fd, and it will return a FILE*. However, I do not think you want to do this. I haven't tried it, but I would guess that at that point you have two separate streams open to the same file: the FILE* you just aquired and the fstream you already had.

    I know there is also an fstream constructor that takes a FILE* and makes an fstream out of it, but I am not sure what the consequences are of using both of those at the same time: I imagine the idea is that you construct an fstream with a FILE* and then only use the fstream from there on out. Of course, I haven;t done any of this before, so I'm just pointing out various ways to build a fire or burn your house down.

    Here is a link that may also be useful:
    http://www.codeproject.com/file/handles.asp
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  3. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  4. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  5. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM