Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. convert char to string

convert char to string

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorial
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nuttynibbles
    wrote on last edited by
    #1

    hi i noe this may sound simple but im having problem in converting char to string.

    string get_currenttime(){
    time_t rawtime;
    string s1,
    stringstream ss;
    struct tm * timeinfo;
    char buffer [80];

    time ( &rawtime );
    timeinfo = localtime ( &rawtime );
    strftime (buffer,80,"%c",timeinfo); // result: Mon Nov 30 22:08:52 2009

    ss << buffer;
    ss >> s1; //result: Mon
    }

    From the above code which i found online, i need to convert the data in buffer to string. i tried using stringstream but results are not complete. See above for example

    L _ 2 Replies Last reply
    0
    • N nuttynibbles

      hi i noe this may sound simple but im having problem in converting char to string.

      string get_currenttime(){
      time_t rawtime;
      string s1,
      stringstream ss;
      struct tm * timeinfo;
      char buffer [80];

      time ( &rawtime );
      timeinfo = localtime ( &rawtime );
      strftime (buffer,80,"%c",timeinfo); // result: Mon Nov 30 22:08:52 2009

      ss << buffer;
      ss >> s1; //result: Mon
      }

      From the above code which i found online, i need to convert the data in buffer to string. i tried using stringstream but results are not complete. See above for example

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You don't need the stringstream variable. Just use

      s1 = buffer;

      and it will create a string from the contents of buffer.

      1 Reply Last reply
      0
      • N nuttynibbles

        hi i noe this may sound simple but im having problem in converting char to string.

        string get_currenttime(){
        time_t rawtime;
        string s1,
        stringstream ss;
        struct tm * timeinfo;
        char buffer [80];

        time ( &rawtime );
        timeinfo = localtime ( &rawtime );
        strftime (buffer,80,"%c",timeinfo); // result: Mon Nov 30 22:08:52 2009

        ss << buffer;
        ss >> s1; //result: Mon
        }

        From the above code which i found online, i need to convert the data in buffer to string. i tried using stringstream but results are not complete. See above for example

        _ Offline
        _ Offline
        _Superman_
        wrote on last edited by
        #3

        The difference in result that you see is because the >> operator will get characters only till a newline or space character. You could use the getline function to get the entire result. But the solution suggested by Richard MacCutchan is simple and the best.

        «_Superman_» I love work. It gives me something to do between weekends.
        Microsoft MVP (Visual C++)

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups