c-str

Store c_str() as char *

人走茶凉 提交于 2021-02-07 19:54:18
问题 I'm trying to use the function with the following declaration: extern int stem(struct stemmer * z, char * b, int k)1 I'm trying to pass a C++ string to it, so I thought I'd use the c_str() function. It returns const char * . When I try to pass it to the stem() function, I get this error: error: invalid conversion from 'const char*' to 'char*' [-fpermissive] . How can I store the result of c_str() such that I can use it with the stem function? Here is the code I'm running: struct stemmer * z =

Store c_str() as char *

故事扮演 提交于 2021-02-07 19:52:06
问题 I'm trying to use the function with the following declaration: extern int stem(struct stemmer * z, char * b, int k)1 I'm trying to pass a C++ string to it, so I thought I'd use the c_str() function. It returns const char * . When I try to pass it to the stem() function, I get this error: error: invalid conversion from 'const char*' to 'char*' [-fpermissive] . How can I store the result of c_str() such that I can use it with the stem function? Here is the code I'm running: struct stemmer * z =

c_str() vs. data() when it comes to return type

房东的猫 提交于 2020-01-12 04:12:11
问题 After C++11, I thought of c_str() and data() equivalently. C++17 introduces an overload for the latter, that returns a non-constant pointer (reference, which I am not sure if it's updated completely w.r.t. C++17): const CharT* data() const; (1) CharT* data(); (2) (since C++17) c_str() does only return a constant pointer: const CharT* c_str() const; Why the differentiation of these two methods in C++17, especially when C++11 was the one that made them homogeneous? In other words, why only the

c_str() vs. data() when it comes to return type

本小妞迷上赌 提交于 2020-01-12 04:12:08
问题 After C++11, I thought of c_str() and data() equivalently. C++17 introduces an overload for the latter, that returns a non-constant pointer (reference, which I am not sure if it's updated completely w.r.t. C++17): const CharT* data() const; (1) CharT* data(); (2) (since C++17) c_str() does only return a constant pointer: const CharT* c_str() const; Why the differentiation of these two methods in C++17, especially when C++11 was the one that made them homogeneous? In other words, why only the

error: could not convert from 'std::string* {aka std::basic_string<char>*}' to 'std::string {aka std::basic_string<char>}'|

核能气质少年 提交于 2019-12-25 18:32:48
问题 I'm trying to make a function that writes a file but i'm having issues passing a string as a parameter. void writeFile(string filename, string letters, int size) { ofstream outputfile("output.txt"); outputfile << letters; outputfile.close(); } int main() { string letters[] = {"u", "l", "s", "n","m", "z", "a", "p", "b"}; int size = 9; string filename = "Inputfile.txt"; writeFile(inputfilename.c_str(),letters,size); } And having this error. error: could not convert from 'std::string* {aka std:

What does eof() returns?

我怕爱的太早我们不能终老 提交于 2019-12-24 03:31:04
问题 Here is the code: string fname = "/home/jack/example.csv"; ifstream csvin(fname.c_str()); if (csvin.eof()) { do_something; } My question is: In what case eof() returns true. I have the following options: File does not exist. File is empty. File either does not exist or it is empty. Unfortunately documentation does not help since I do not know eofbit error state flag means. I also do not understand what End-of-File is reached in the sequence associated with the stream mean. I assume that c_str

VC++ function string::c_str(): the address of the first byte was set to 0 (compare to g++)

為{幸葍}努か 提交于 2019-12-13 05:21:31
问题 I met a strange problem when trying to get the result of a string’s function c_str() whose result is inconsistent with g++. There is a function called Test to return a string instance. And I want to use a char* type to store the result (it’s needed). As you can see the function is simple return a string “resultstring”. But when I try to get the result something strange happened. The result I got is “” in part two. The part one and part three both return the “resultstring”. While that’s in

Converting strstream to sstream conflict about c_str()

蹲街弑〆低调 提交于 2019-12-04 06:02:08
问题 I have this code block as written with strstream . And I converted it to sstream as below. I'm not sure, but I think printStream->str() is returning a string object with a copy (temporary) of the contents in the stream buffer pointed by printStream , and then then you are invoking c_str() on it and getting a const char * , and then casting away the const-ness, and then returning the pointer outside the function scope. I think since its a temporary value you are getting back from printStream-

What's the difference between std::string::c_str and std::string::data? [duplicate]

元气小坏坏 提交于 2019-12-03 12:27:54
问题 This question already has answers here : string c_str() vs. data() (6 answers) Closed 5 years ago . Why would I ever want to call std::string::data() over std::string::c_str()? Surely there is some method to the standard's madness here... 回答1: c_str() guarantees NUL termination. data() does not. 回答2: c_str() return a pointer to the data with a NUL byte appended so you can use the return value as a "C string". data() returns a pointer to the data without any modifications. Use c_str() if the

c_str() vs. data() when it comes to return type

半腔热情 提交于 2019-12-03 04:13:48
After C++11, I thought of c_str() and data() equivalently . C++17 introduces an overload for the latter, that returns a non-constant pointer ( reference , which I am not sure if it's updated completely w.r.t. C++17): const CharT* data() const; (1) CharT* data(); (2) (since C++17) c_str() does only return a constant pointer: const CharT* c_str() const; Why the differentiation of these two methods in C++17, especially when C++11 was the one that made them homogeneous? In other words, why only the one method got an overload, while the other didn't? Kerrek SB The new overload was added by P0272R1