Questions tagged [c-str]

Anything related to `c_str` method of class `std::basic_string` of C++ standard library.

Anything related to c_str method of class std::basic_string of C++ standard library.

See documentation of c_str on CPPreference.com.

73 questions
2
votes
1 answer

Boost.Program_Options: Why does options_description_easy_init::operator() not have an overload for std::string?

Consider this MCVE: #include #include #include namespace po = boost::program_options; using namespace std; po::options_description createOptions(const std::string& description, const map
andreee
  • 4,459
  • 22
  • 42
2
votes
2 answers

Using String c_str() to return char*

I am working on some legacy code where I have to make some changes in the cpp file.The cpp file contains entire code in extern "c" block - I updated a function that returns a char* .The code looks something like func1() below. Since I use…
Anil
  • 111
  • 6
2
votes
2 answers

Printf and C++ strings

So I have the following code: #include #include #include using namespace std; int main() { vector strs; strs.push_back("happy"); const string& a1 = strs[0]; strs.push_back("birthday"); …
Bob Sacamano
  • 699
  • 15
  • 39
2
votes
2 answers

Value equality of c_str == string vs c_str == c_str

I'm trying to compare libpqxx c_str values. If I try to compare them directly, result1[0][0].c_str() == result2[0][0].c_str(), for example, they do not return true when they have the same string values. The only way to compare them properly is to…
user1382306
2
votes
4 answers

What does eof() returns?

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…
Roman
  • 124,451
  • 167
  • 349
  • 456
2
votes
3 answers

C++ c_str doesn't return entire string

I've tried the following code with both normal ifstreams and the current boost:iostream I'm using, both have the same result. It is intended to load a file from physfs into memory then pass it to a handler to process (eg Image, audio or data).…
Simon Moles
  • 359
  • 1
  • 5
  • 11
2
votes
2 answers

C++ STL: will c_str() be changed when creating const copy of std::string?

This is question about difference between STL implementations in handling const copy of std::string. I have such short test, which does 2 const copies and prints addresses returned by c_str(): #include #include using namespace…
osgx
  • 90,338
  • 53
  • 357
  • 513
2
votes
5 answers

C++ toString() with sprintf()

With C++ I am trying to implement a toString() function for my class: void ClassName::toString(string& returnString) { sprintf(returnString.c_str(), "Position: (%f, %f, %f)\n", position.x, position.y, position.y); } However I keep getting this…
CodeKingPlusPlus
  • 15,383
  • 51
  • 135
  • 216
2
votes
3 answers

c++ c_str adding strange characters to the end of the string

let me first thank you for taking the time to read this. I'm trying to read a file in c++. Currently I have a method that allows the user to select a file in explorer and returns this as an 'std::string'. I then have to open this file, but the…
planty182
  • 57
  • 1
  • 9
1
vote
0 answers

fstream and c_str() : c_str is not working

I have been recently working on a program and i tried to use System::String^ to use it for an ofstream file... I always get an Error... Note This is a Form so it is a Header File. This is my Code...(one part) System::String^ txtPath; #pragma…
1
vote
0 answers

Use of c_str() for wstrings in c++

When I have a: std::string s I can do: s.c_str() to convert it to a char*. As I understand it only adds a '\0' to the end of s. Can I then conclude that just adding '\0' converts a string to a char*? Is doing something like that converting it to…
user3443063
  • 1,455
  • 4
  • 23
  • 37
1
vote
1 answer

Unexpected behavior when converting a character array to a string

So I have this simple class, it takes a character array and parses it into a JSON object. It then stores that object internally and provides a getter. class JSONContainer { public: explicit JSONContainer(const char* const json) { …
Tyler Weiss
  • 139
  • 1
  • 15
1
vote
0 answers

Weird behaviour of string size function or am I doing something wrong?

I am getting the wrong std::string size value. statsd_link* statsd_init(std::string& host, int port) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "statsd_init print data to %s:%d and length: %ld, %ld\n", …
Shravan40
  • 8,922
  • 6
  • 28
  • 48
1
vote
1 answer

format float in C++ mfc

I used VS2010 to build a MFC program in windows 7. I want to format the float data in the report generated. I have tried to use %f but the "%f" is showed in the report. CString m_Iwork_min; struct m_report_info; m_Iwork_min =…
Bill
  • 33
  • 7
1
vote
1 answer

After recompiling our FORTRAN-code and using it in C++ our system() or c_str() command don`t work properly

Hi I have a big problem: We created a program in C++/Qt 4.8.4 /Qt Creator 2.8.1 years ago that while executing runs another executable (written and compiled in FORTRAN). Everything worked well. We recompiled our Fortran-Code with the new version of…
user3443063
  • 1,455
  • 4
  • 23
  • 37