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
1
vote
1 answer

c_str() equivalent in C

I've just started using C programming language for coding my course project and I'm not much knowledgeable about C. I've been using C++ for a while now and I need to find an alternative to c_str() function in C. I'm trying to code (in C) similar to…
Pranav
  • 560
  • 1
  • 8
  • 21
1
vote
3 answers

C++ c_str() doesn't return complete string

I'm doing a C++ assignment that requires taking user input of an expression (eg: 2 * (6-1) + 2 ) and outputting the result. Everything works correctly unless a space is encountered in the user input. It is a requirement to pass the user input to the…
AgentOrange
  • 65
  • 2
  • 11
1
vote
1 answer

assignment of c_str() to a string

I have a problem which i cannot fix on my own. string filenameRaw; filenameRaw= argv[1]; function(filenameRaw.c_str(),...); function(const char* rawDataFile,const char* targetfieldFile,const char* resultFile,const char*…
raspiede
  • 179
  • 1
  • 1
  • 11
1
vote
1 answer

avoiding to use temp strings with .c_str command?

I'm wondering if there is a way to avoid using a temporary string in this case: tempString = inputString.substr(commaLast,commaCurrent-1); yPos = strtod(tempString.c_str(), NULL); There is no way to use the substr command and returning a c_str…
0
votes
1 answer

Why doesn't string class take in two words separated with space?

I want to take in persons name using string object. But in my code if I put two part name separated with a space, only first part is displayed. My understanding is .c_str() returns a pointer to stored string with terminal null. Why is there a…
0
votes
1 answer

Accidentally linking c_str() strings

I'm trying to assign a string to a struct value, which works, but the value somehow links to the variable: string tmp; struct test_struct{ const char *server, *user; }; test_struct test; tmp = "foo"; test.server = tmp.c_str(); cout <<…
0
votes
0 answers

Implementing string to int function in c

I am trying to implement a function as stated in the title. I think I am very close to solution but a problem. input: 51% are admitted. output: x:51 (null) but output should have been: s:% are admitted. My try is here: #include…
akoluacik
  • 33
  • 5
0
votes
3 answers

Curious behaviour of c_str() and strings when passed to class

I came across a curious behaviour (and I am sure it is just curious to me and there exists a perfectly valid c++ answer) when playing around with c-strings and std::string. Typically, when I pass a string to a class' constructor, I do something like…
tom
  • 361
  • 3
  • 11
0
votes
3 answers

Getting char * or const char * data from a string breaks for 16 character strings or longer

I have a function string_to_char() which attempts to give me a form of a string which I can pass into a library I am using, which wants char * (but I think works with const char *, so I've been trying both). The code I wrote to test my…
Joss
  • 67
  • 6
0
votes
2 answers

c_str() is terminating my encrypted message is there a way to bypass this?

I am encrypting a message using RSA. Everything works fine if I try to encrypt/decrypt the message locally, however, when converting the string to void constant* in order to use the socket::send method like so, int sendRes = send(socket,…
0
votes
0 answers

single vs double quotes C++ - interesting, unexpected behaviour

I found some pretty weird behaviour when I was trying to debug my C++ code for an unrelated problem. I am printing a result at a stage in the program at which I have the integer variables 'amount' and 'coins[i]' as: std::cout << "Found, 1! n = " << …
tam63
  • 191
  • 10
0
votes
0 answers

Why is c_str() not reading the appended string?

I am currently working on a project where I take an ASCII image .txt file and have it sent to a mySQL database. I have the program read each row of the .txt file and have it appended to a mySQL command string. All is well until I have the string…
katsurice
  • 1
  • 1
0
votes
3 answers

The lifetime of pointer that point to c_str function in std::string

Firstly the code listed as follow. #include #include int main(){ const char *cs; { std::string s("123456"); cs = s.c_str(); printf("cs = %s\n",cs); } printf("cs = %s\n",cs); return…
Andy Cong
  • 109
  • 1
  • 10
0
votes
2 answers

Strchr and strrchr returning same result

For some reason, whether I try strchr or strrchr, I get the same value returned. I have no idea why. Here is the segment of code giving me trouble: printf("Enter a data point (-1 to stop input):\n"); fgets(input, 50, stdin); char…
mperic
  • 118
  • 9
0
votes
4 answers

Can you safely get a pointer to a string from its c_str() const char*?

I have a const char pointer which I know for sure came from a string. For example: std::string myString = "Hello World!"; const char* myCstring = myString.c_str(); In my case I know myCstring came from a string, but I no longer have access to that…
The Mawg
  • 13
  • 5