Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7776699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:56:55+00:00 2026-06-01T17:56:55+00:00

Is it possible to use an std::string for read() ? Example : std::string data;

  • 0

Is it possible to use an std::string for read() ?

Example :

std::string data;

read(fd, data, 42);

Normaly, we have to use char* but is it possible to directly use a std::string ? (I prefer don’t create a char* for store the result)

Thank’s

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T17:56:56+00:00Added an answer on June 1, 2026 at 5:56 pm

    Well, you’ll need to create a char* somehow, since that’s what the
    function requires. (BTW: you are talking about the Posix function
    read, aren’t you, and not std::istream::read?) The problem isn’t
    the char*, it’s what the char* points to (which I suspect is what
    you actually meant).

    The simplest and usual solution here would be to use a local array:

    char buffer[43];
    int len = read(fd, buffer, 42);
    if ( len < 0 ) {
        //  read error...
    } else if ( len == 0 ) {
        //  eof...
    } else {
        std::string data(buffer, len);
    }
    

    If you want to capture directly into an std::string, however, this is
    possible (although not necessarily a good idea):

    std::string data;
    data.resize( 42 );
    int len = read( fd, &data[0], data.size() );
    //  error handling as above...
    data.resize( len );  //  If no error...
    

    This avoids the copy, but quite frankly… The copy is insignificant
    compared to the time necessary for the actual read and for the
    allocation of the memory in the string. This also has the (probably
    negligible) disadvantage of the resulting string having an actual buffer
    of 42 bytes (rounded up to whatever), rather than just the minimum
    necessary for the characters actually read.

    (And since people sometimes raise the issue, with regards to the
    contiguity of the memory in std:;string: this was an issue ten or more
    years ago. The original specifications for std::string were designed
    expressedly to allow non-contiguous implementations, along the lines of
    the then popular rope class. In practice, no implementor found this
    to be useful, and people did start assuming contiguity. At which point,
    the standards committee decided to align the standard with existing
    practice, and require contiguity. So… no implementation has ever not
    been contiguous, and no future implementation will forego contiguity,
    given the requirements in C++11.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use std::string instead of char* whenever possible, but I worry I
Possible Duplicate: C++ char* vs std::string Is there any advantage to using char*'s instead
Possible Duplicate: When to use std::size_t? I have a lot of constants in my
I have a similar data structure like this: struct Data { std::string id; Blob
Possible Duplicate: Case insensitive std::string.find() I want to make use of the std::string::find() method
I have a program that load data from a file using std::ifstream and store
Is it possible to somehow adapt a c-style string/buffer ( char* or wchar_t* )
Possible Duplicate: Printing a string to a temporary stream object in C++ std::ostringstream printing
Possible Duplicate: C++ invoke explicit template constructor First imagine I have a class Data
I'm aware that I could use something called std::vector , but I'm afraid it's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.