Thread: writing to a char buffer as a FILE*

  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,817
    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
    28,413
    Is it feasible for you to modify the library code? What I have in mind is changing the fprintf call to a sprintf call.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    Hmm... in that case, I suspect that your best approach is to write to a temporary file, then read from it.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    38
    Not with standard C(++).

  8. #8
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    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)

  9. #9
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    257
    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
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    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)

  11. #11
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    257
    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
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    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.

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

  14. #14
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by antred View Post
    For fairness' sake, though, you should probably check the result of fmemopen().
    Corrected ..

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

    Cheers all!

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, 10:24 PM
  3. writing a file from buffer
    By mariano_donati in forum C Programming
    Replies: 8
    Last Post: 02-25-2008, 02:04 AM
  4. Writing to screen buffer.
    By h3ro in forum Game Programming
    Replies: 4
    Last Post: 01-15-2008, 07: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