feed printf output into buffer

This is a discussion on feed printf output into buffer within the C Programming forums, part of the General Programming Boards category; Here's a simple question from a newbie: Can I redirect the output of printf into an int buffer? Eg. Code: ...

  1. #1
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300

    Arrow feed printf output into buffer

    Here's a simple question from a newbie:
    Can I redirect the output of printf into an int buffer? Eg.

    Code:
    printf ("%d", (inmax_t)statbuf.st_size);
    Gives a number whereas

    Code:
    int mycount = (intmax_t)statbuf.st_size;
    Does nothing, meaning I can't do further arithmetic with (intmax_t)statbuf.st_size UNLESS I can collect the output of printf into "int mycount" somehow.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I am COMPLETELY at loss as to what you are actually asking for here.

    printf() will display a string on the standard output (traditionally your terminal/shell).

    Assigning an integer from a intmax_t will copy (parts of) the value from one variable to another...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300

    Question to clarify...

    I'm at a loss as to how you can be at a loss, but here goes:

    I need the value of (intmax_t)statbut.st_size. Call it an "int thevalue", so

    Code:
    myvalue = (intmax_t)statbuf.st_size;
    Should give the same answer as

    [code]
    printf ("%d", (intmax_)statbuf.st_size);

    The difference being that the first statement yields a manipulable integer whereas the second only prints something on the screen. Unfortunately, in particular instance X (let's say that's mine), only the second gives the correct answer instead of zero. That is, I can swap the two lines WITHOUT CHANGING ANYTHING ELSE but only the printf will work (?!!#?!).

    So, can I redirect stdout (ie., the answer from printf) into a normal int buffer? Or do I have to write two executables and pipe the output from one into the other?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Did you read my reply in your other thread, relating to printf formatting and 64-bit numbers?

    Can you post a whole program that we can test?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    The value of 'myvalue' will only be able to hold enough data for the storage of an int, 4 bytes on most systems. With your use of 64-bit integers by casting with 'intmax_t', are you sure you are not stepping out of bounds? What is the value of st_size?

    I can not really add further to what matsp has already stated to you. In this case you will need to be more mindful of the size of your values when you cast or downcast variables. Also know how to print 64-bit variables with printf, you probably are not using the correct specifier.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newb Help: Full Arrays and Functions
    By LycanGalen in forum C Programming
    Replies: 5
    Last Post: 01-31-2008, 08:35 PM
  2. menu problem!!! need U R G E N T help!!!
    By catcat28 in forum C Programming
    Replies: 16
    Last Post: 11-19-2007, 01:32 PM
  3. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  4. error in program????
    By SpEkTrE in forum C Programming
    Replies: 5
    Last Post: 11-24-2003, 06:16 PM
  5. Azbia - a simple RPG game code
    By Unregistered in forum Game Programming
    Replies: 11
    Last Post: 05-03-2002, 07:59 PM

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