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
6 answers

Why pointer returns value and not address?

This code: string str1 ( "Hello world" ); const char *c_str1 = str1.c_str ( ); cout << "The C-style string c_str1 is: " << c_str1 generates this output: The C-style string c_str1 is: Hello world and I do not understand it. c_str1 is a pointer,…
Roman
  • 124,451
  • 167
  • 349
  • 456
0
votes
1 answer

change value of element in list c++

this is what im doing, im getting some info from a .txt file those are numbers of course when im getting them i get them as strings in a list of strings...(i know it could be chars but in this example is strings) so when im inserting them on another…
Makenshi
  • 993
  • 3
  • 15
  • 28
-1
votes
3 answers

Is there a dangling pointer problem in this code?

string str; char *a=str.c_str(); This code is working fine for me but every place else I see this code instead string str; char *a=new char[str.length()]; strcpy(a,str.c_str()); I wonder which one is correct and why?
theebugger
  • 77
  • 9
-1
votes
3 answers

Function call to c_str() vs const char* in hash function

I was looking at hash functions on stackoverflow when I found one that was pretty interesting. It involves casting a const char* to a size_t* and then de-referencing the size_t. This is then bit shifted to a certain precision. This works for const…
piiohpii
  • 11
  • 1
-1
votes
2 answers

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

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; …
SaltyCode
  • 67
  • 1
  • 2
  • 11
-1
votes
3 answers

fstream .open and appending .txt or .dat to filename

This has worked fine on some compilers... Is there a way of doing this were it will just work without it being a problem with different compilers on c++11 or c++14? #include #include #include using namespace…
StackAttack
  • 1,139
  • 1
  • 9
  • 15
-1
votes
1 answer

C++ String & Char*

I am trying to convert a C++ string to a cstring, or char*. I want the hash function that takes a char* to always be called(after converting from a string). I've been looking at this code for an hour and also searching. No luck. char* a =…
Josh
  • 91
  • 8
-1
votes
1 answer

Why is c_str not working in this open(filename) case

I thought I understood having to cast a std::string as a *char when opening a file, but I am missing something. It compiles fine but does not open. Tried a number of variations but so far only hardcoding the name in the file is working: // const…
-2
votes
2 answers

Is c_str() or reinterpret_cast better for working with binary files?

I need to work with binary files in a program and I've seen reinterpret_cast used, as well as c_str(). Here is a code snippet using c_str(): fstream aFile; string sample = "hello this is a line of code"; aFile.open("newFile.bin", ios::out |…
V B
  • 45
  • 1
  • 2
-2
votes
1 answer

Please explain the output of below program

Just run this program and explain me output of last line why it prints "g" instead of "f". Here my intention is to know why it is showing previous functions return value? #include #include std::string f() { return…
-2
votes
1 answer

c_str adds rubbish in the end of char*

I'm building a ftp server and this is a message sent from the client to the server to check whether the file size is approved by the server. I need to use char* in function write so I want to convert string to char*. The string structure is…
itzikos
  • 375
  • 1
  • 5
  • 13
-3
votes
2 answers

cpp-how to get only the value of c_str()

I have a function which return const char*. In this function I have the following params: string str; For converting str to const char* I use str.c_str(). When I debug I notice that str.c_str() contains something (I guess its address) before the…
sara8
  • 199
  • 3
  • 11
-6
votes
1 answer

Do I need to include and for c_str(), atoi and atof functions?

I'm using c_str(), atoi and atof functions for converting string variables to integer or float/double. For example, val = atoi(val1.c_str()); val = atof(val1.c_str()); So, I would like to know if I need to include and . Thanks.
1 2 3 4
5