简体   繁体   中英

How to convert string to char* using stringstream?

I have a function like this:

template <typename T>
void parse_to_T(const std::string& str, T* result) {
    std::stringstream ss;
    ss << str;
    ss >> *result;
}

this function is mean to convert the string to the specified type.

it is work to parse the string to int, float or char.

parse_to_T<int>(...);
parse_to_T<float>(...);
parse_to_T<char>(...);

but when meet char*, segment fault ...

I use the function like this:

int int_val;
string m = "1";
parse_to_T<int>(m, &int_val); // works
char* str_val = NULL;
parse_to_T<char*>(m, &str_val); // segmentfault

How to imply this function to make it work?

(convert the string to specified type like int, double, char, char*)?

Hey, I don't know how to explain my use case, but I will try:

To simple, the question is, given a file, for each line, data may have these types :

  • int
  • float
  • char
  • char*
  • an array T[num] (T is int, float, char or char*, or any build_in type.)

imple a parse function to parse this file.

and this problem is an exam ...


thanks and I found the error now.

char* str_val = NULL;
parse_to_T<char*>(m, &str_val); // segmentfault;
char* str_val;
parse_to_T<char*>(m, &str_val); // segmentfault;
char* str_val = new char[256];
parse_to_T<char*>(m, &str_val); // works !!

then the error is I didn't allocate memory to the ptr...

This segfault is because stringstream does not allocate memory to hold the result of the operation when extracting values into a char* array. It tries to put the values into the memory pointed at by the lhs operand. You must allocate memory yourself.

Here is a simple example:

#include <string>
#include <sstream>
#include <iostream>

template <typename T>
void parse_to_T(const std::string& str, T* result) {
    std::stringstream ss;
    ss << str;
    std::cout << ss.str() << std::endl;
    ss >> *result;
    std::cout << *result << std::endl;
}

int main() {
   char* buffer = new char[256];
   /* Don't do this in real code. If the input is larger than
      the size of the buffer it will end very, very badly.
      always have a way of allocating the correct amount
      of memory.
   */
   int int_val;
   std::string m = "1";
   parse_to_T<int>(m, &int_val);
   char* str_val = NULL;
   parse_to_T<char*>(m, &buffer);
   delete[] buffer;
   return 0;
}

You could include a template specialization for the char* datatype that does the allocation based on the amount of data in the stream (calling stringstream.str().size() should work) but the user would have to free the returned memory.

First, your destination variable str_val has no allocated memory, so it is normal you have a segmentation fault.

On the other hand, it would be better if you use std::string instead. If you need the raw pointer const char* then you can use std::string::c_str for so. If you have to modify it then just create a copy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM