Stringstream

This is a discussion on Stringstream within the C Programming forums, part of the General Programming Boards category; Is there a way to create a stream from a string? I know there is a way to do it ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    266

    Stringstream

    Is there a way to create a stream from a string? I know there is a way to do it using gnu's c library but I would rather not depend on that.
    Fried chicken for everybody!
    -Kernel Sanders

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    Quote Originally Posted by lruc View Post
    Is there a way to create a stream from a string? I know there is a way to do it using gnu's c library but I would rather not depend on that.
    Yes there is, but first you need to provide more specifics.

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    266
    Something similar to C++'s stringstream. What other information do you need?
    Fried chicken for everybody!
    -Kernel Sanders

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    Are you reading from a file or stdin or is it just a buffer-full of characters?

  5. #5
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    If you're looking for something an append function for a string, go something like this.

    Code:
    int append(char *str, size_t strsize, char *toappend) {
        if(str == NULL || toappend == NULL) return 1;
        if(strlen(str) + strlen(toappend) >= strsize) return 1;
        strncpy(str[strlen(str)-1], toappend, strlen(toappend)+1);
        return 0;
    }
    . . . for a buffer of characters, for an append function. If, on the other hand, you're looking for something more along the lines of a snprintf()-style function, (other than snprintf() . . .), well, take a look at va_arg.

    EDIT: the code for something to append the contents of a filestream onto the string is much the same. Tip: use fgets.
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by lruc View Post
    Is there a way to create a stream from a string? I know there is a way to do it using gnu's c library but I would rather not depend on that.
    What kind of stream are you talking about?
    The C equivalents of std::stringstream are sprintf() & sscanf().
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  7. #7
    Registered User
    Join Date
    Jun 2008
    Posts
    266
    Quote Originally Posted by cpjust View Post
    What kind of stream are you talking about?
    The C equivalents of std::stringstream are sprintf() & sscanf().
    The problem with those functions is they don't store the position in a string so when they are called twice, it would just read from the beginning of the string again which is not what I need. Also, I'm reading from a buffer full of characters.
    Last edited by lruc; 03-23-2009 at 01:55 PM.
    Fried chicken for everybody!
    -Kernel Sanders

  8. #8
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by lruc View Post
    The problem with those functions is they don't store the position in a string so when they are called twice, it would just read from the beginning of the string again which is not what I need. Also, I'm reading from a buffer full of characters.
    do it yourself - sprintf returns number of bytes printed
    scanf has %n format that could notify number of bytes parsed
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  9. #9
    Registered User
    Join Date
    Jun 2008
    Posts
    266
    Quote Originally Posted by vart View Post
    do it yourself - sprintf returns number of bytes printed
    scanf has %n format that could notify number of bytes parsed
    Thanks for the info. Didn't know that. I will.
    Fried chicken for everybody!
    -Kernel Sanders

  10. #10
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    You could get creative with strncpy() or strncat() since C does not provide an automatically-managed string type.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stringstream problem
    By black_spot1984 in forum C Programming
    Replies: 3
    Last Post: 10-28-2008, 05:09 PM
  2. stringstream clear
    By misterowakka in forum C++ Programming
    Replies: 3
    Last Post: 01-01-2008, 01:03 PM
  3. Socket: send stringstream
    By crisis in forum C++ Programming
    Replies: 5
    Last Post: 11-22-2007, 10:50 AM
  4. Length of a stringstream
    By nickname_changed in forum C++ Programming
    Replies: 1
    Last Post: 10-24-2003, 08:30 PM
  5. Flushing a stringstream
    By Eibro_Guest in forum C++ Programming
    Replies: 1
    Last Post: 01-06-2003, 03:21 PM

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