Like Tree2Likes

writing to a char buffer as a FILE*

This is a discussion on writing to a char buffer as a FILE* within the C++ Programming forums, part of the General Programming Boards category; I am trying to use an old library I found that I am unable to modify. The function I am ...

  1. #1
    Registered User
    Join Date
    Jun 2012
    Location
    Norway
    Posts
    9

    writing to a char buffer as a FILE*

    I am trying to use an old library I found that I am unable to modify.

    The function I am trying to use takes in a FILE* as a parameter, and writes to this stream using the fprintf function.

    Useally this function has only been used to print stuff to stdout, and this works well. What I need to do is to get this to write to a char buffer.

    The function looks something like this:

    Code:
    void print( FILE* fileStream, int value )
    {
        fprintf( fileStream, "value: %d", value );
    }
    what I want to do is to init a char buffer

    Code:
    char characterBuffer[ 1024 ];

    and with the use of some tool get the print function to write to this characterBuffer. I' ve tried to use stringstream and a few other things, but I am unable to get this to work properly (so that is I toss this characterBuffer into a printf, the output is readable).

    Anyone know of a way to do this?

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,802
    sprintf?

    Show us your attempt using stringstreams and we'll help you out.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Is it feasible for you to modify the library code? What I have in mind is changing the fprintf call to a sprintf call.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Jun 2012
    Location
    Norway
    Posts
    9
    If I could change the function, I would, but it is a part of a bigger system and I am not allowed to modify it..

    I've tried alot of stuff with the stringstream, some things even compile and does not end up in a core dump.. :P
    For example I tried:

    Code:
    int main( int argc, const char* argv )
    {
        std::stringstream sStream( std::stringstream::out );
    
        print( (FILE*)&sStream, 54321 ); //bad attemt to get it to work, I know.. reaching for straws at this point
    
        std::string str = sStream.str();
        printf( "str: %s", str.c_str() );
        return 0;
    }

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Hmm... in that case, I suspect that your best approach is to write to a temporary file, then read from it.
    antred likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jun 2012
    Location
    Norway
    Posts
    9
    This is ofc a way, but I would rather not add file access unless it is really needed.

    I was thinking, is it not possible to override FILE in some way?

  7. #7
    Registered User
    Join Date
    Jul 2008
    Posts
    36
    Not with standard C(++).

  8. #8
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    If you're willing to go into a lower level, you could make a pipe (or shared memory...or literally anything in a *nix system) using native functions and use fdopen to construct a FILE* from it.

    (I've no idea about the feasibility of this, though)
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  9. #9
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    254
    Quote Originally Posted by manasij7479 View Post
    If you're willing to go into a lower level, you could make a pipe (or shared memory...or literally anything in a *nix system) using native functions and use fdopen to construct a FILE* from it.

    (I've no idea about the feasibility of this, though)
    At that point, though, it would be MUCH easier (and more portable) to just follow laserlight's suggestion and have the function write its data to a file and then read that file back in.

  10. #10
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by antred View Post
    At that point, though, it would be MUCH easier (and more portable) to just follow laserlight's suggestion and have the function write its data to a file and then read that file back in.
    Portable, agreed.
    But..easier not.
    I am talking about writing to a file too...in every sense.
    But the why tell the filesystem to manage it and incur the disk overhead when the kernel can create the object essentially for free (afaik)?
    After that normal File IO can be used, but the program doesn't need to know that the file does not really physically exist on the disk.

    (I'm talking from a Linux(or any other *nix) perspective though and don't know how these are done on Windows)
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  11. #11
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    254
    Quote Originally Posted by manasij7479 View Post
    But..easier not.
    I'd wager that in terms of code written to make each respective solution work, your approach would be a lot more complex than doing this:

    Code:
    std::ifstream ifs( "TheFile.txt", std::ios::in );
    
    if ( ! ifs )
    {
        std::cerr << "Dang! Didn't work!\n";
    }
    else
    {
        const std::string content( ( std::istreambuf_iterator< char >( ifs ) ), std::istreambuf_iterator< char >() );
    
        // Now use the data to do what ever you want to do with it.
    }

  12. #12
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by antred View Post
    I'd wager that in terms of code written to make each respective solution work, your approach would be a lot more complex than doing this:
    Found a much much simpler solution..
    Code:
    char buf[1024];
    File* foo = fmemopen(buf,1024,"w+");
    if(!foo) return 42;
    
    print(foo,1000); //OP's function 
    
    fflush(foo);
    Done!
    Last edited by manasij7479; 06-13-2012 at 12:57 PM.
    laserlight likes this.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  13. #13
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    254
    Well, alright. That's fairly simple I suppose. For fairness' sake, though, you should probably check the result of fmemopen().

  14. #14
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by antred View Post
    For fairness' sake, though, you should probably check the result of fmemopen().
    Corrected ..
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  15. #15
    Registered User
    Join Date
    Jun 2012
    Location
    Norway
    Posts
    9
    Awesome, I rly liked that solution!

    Cheers all!

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I am writing a string to an excel file a char per cell
    By jeremy duncan in forum C++ Programming
    Replies: 10
    Last Post: 05-12-2012, 02:09 AM
  2. Replies: 8
    Last Post: 12-22-2011, 09:24 PM
  3. writing a file from buffer
    By mariano_donati in forum C Programming
    Replies: 8
    Last Post: 02-25-2008, 01:04 AM
  4. Writing to screen buffer.
    By h3ro in forum Game Programming
    Replies: 4
    Last Post: 01-15-2008, 06:23 AM
  5. writing char arrays to a file
    By cpluspluser in forum C++ Programming
    Replies: 2
    Last Post: 05-26-2003, 11:17 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21