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
0
votes
1 answer

c_str() is only reading half of my string, why? How can I fix this? Is it a byte issue?

I am writing a client program and server program. On the server program, in order to write the result back to the client, I have to convert the string to const char* to put it in a const void* variable to use the write() function. The string itself…
Prinks
  • 33
  • 3
0
votes
2 answers

C++: Why does this string input fail while the other does not

I got this problem from a friend #include #include #include void riddle(std::string input) { auto strings = std::vector{}; strings.push_back(input); auto raw = strings[0].c_str(); …
Wolfy
  • 548
  • 2
  • 9
  • 29
0
votes
1 answer

How does std::string's c_str() function return null-terminating string?

I cannot understand if char * returned from string.c_str() points to the same buffer (and not making any copy) how is it null terminated? does the function add null-terminator in the end of string? and what happens if buffer doesn't have any extra…
0
votes
1 answer

Char pointer doesn't initialize as expected

MyClass.h #pragma once class MyClass { public: const char* m_var; MyClass(); }; MyClass.cpp #include #include #include "MyClass.h" MyClass::MyClass() : m_var(("Text_" +…
krsi
  • 1,045
  • 2
  • 14
  • 24
0
votes
2 answers

Converting strstream to sstream conflict about c_str()

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,…
0
votes
3 answers

ifstream + opening random txt file (c_str)

I want to open a random .txt file and put the data into some strings. It works if I write the path into the code. I don't get it why this doesn't work. #include #include using namespace std; int main() { string file; …
TF107
  • 11
  • 1
0
votes
3 answers

c++ string formed by copying index wise characters from other already initialised string. Not able to print newly formed string using cout

In C++, I created a new string (string class) by copying characters index wise from another string (string class) which was already initialized. But I am unable to print this new string to the screen using cout. Using c_str() I am able to print it…
Zag
  • 693
  • 1
  • 5
  • 9
0
votes
2 answers

using method on member of struct from global function in C++

I'm very new to C++ and I'm always running into little things here and there. The most recent appears to be a problem with a struct. struct student_record{ student_record(std::string name, int ident, double gpa){ for (int i = 0; i <…
0
votes
1 answer

How to look up the ENUM type given a string?

I have an enum class like this: class ContentTypeEnum { public: // it might have more types enum Code { TEXT, XML, APPLICATION_JSON}; static const char* to_c_str(unsigned); }; I was using it like this in my code as of…
john
  • 11,311
  • 40
  • 131
  • 251
0
votes
1 answer

Using String's c_str() and assigning to char const*: assignment of read-only location

My problem is this: I have a constant pointer to a constant char pointer (2D char array where both dimensions are const). I need to assign C-strings to this array. I have a std::vector of std::strings which I used c_str() on to create a vector of…
Daniel Handojo
  • 612
  • 5
  • 19
0
votes
1 answer

Error trying to convert string char into int

I have a simple program where I want to store the input into matrices for easy access. I am having trouble converting a simple string character into an int, can someone explain why my code is giving me this message when I try and…
mudejar
  • 177
  • 1
  • 2
  • 11
0
votes
3 answers

How to cast a std::stringbuf into an array of char?

What I'm trying to do here is to cast a stringbuf object into an array of char. I do this to send the array of char to a C interface which doesn't understand the type std::stringbuf. Here's a part of my code to illustrate the problem…
kidz55
  • 333
  • 4
  • 26
0
votes
1 answer

How to resolve " invalid operands of types 'const char [ ]' and 'const char*' to binary 'operator+' "

I use Qt Creator and this code : string execpath = ""; execpath += (QCoreApplication::applicationDirPath()).toStdString(); WinExec("ffmpeg -f dshow -t 32 -i audio=\"virtual-audio-capturer\" -y "+(execpath.c_str())+"\\sound.mp3", SW_HIDE); //…
AmirH
  • 57
  • 11
0
votes
1 answer

How to make String to const wchar_t* conversion function work under Windows and Linux

I work on a project written for MSVCC / Windows, that I have to port to GCC / Linux. The Project has its own String Class, which stores its Data in a QString from Qt. For conversion to wchar_t* there was originally this method (for Windows): const…
weitho
  • 80
  • 1
  • 9
0
votes
2 answers

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

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…