Register and join over 40,000 other developers!
Recent Topics
-
Print specific values from dictionary with a specific key name
Siten0308 - Jun 20 2019 01:43 PM
-
Learn algorithms and programming concepts
johnnylo - Apr 23 2019 07:49 AM
-
Job Gig PHP Form Needed
PJohnson - Apr 18 2019 03:55 AM
-
How to make code run differently depending on the platform it is running on?
xarzu - Apr 05 2019 09:17 AM
-
How do I set a breakpoint in an attached process in visual studio
xarzu - Apr 04 2019 11:47 AM
Recent Blog Entries
Recent Status Updates
Popular Tags
- networking
- Managed C++
- stream
- console
- database
- authentication
- Visual Basic 4 / 5 / 6
- session
- Connection
- asp.net
- import
- syntax
- hardware
- html5
- array
- mysql
- java
- php
- c++
- string
- C#
- html
- loop
- timer
- jquery
- ajax
- javascript
- programming
- android
- css
- assembly
- c
- form
- vb.net
- xml
- linked list
- login
- encryption
- pseudocode
- calculator
- sql
- python
- setup
- help
- game
- combobox
- binary
- hello world
- grid
- innerHTML

7 replies to this topic
#1
Posted 19 September 2011 - 02:26 PM
Hi
I am trying to find a way to convert from char * to string.
I have:
char * buf = "test";
and I want to look for a substring in this. I tried using strcpy:
string s;
strcpy (s, buf);
but it keeps telling me: "No instance of function "strcpy" matches the argument list.
Anybody knows a way of doing this?
Thanks
I am trying to find a way to convert from char * to string.
I have:
char * buf = "test";
and I want to look for a substring in this. I tried using strcpy:
string s;
strcpy (s, buf);
but it keeps telling me: "No instance of function "strcpy" matches the argument list.
Anybody knows a way of doing this?
Thanks
#2
Posted 19 September 2011 - 03:50 PM
IDK about that error, but first I must tell you, strcpy is NOT compatible with C++ strings. If you want to convert from char * to string, you would do something like this:
BTW, use the code tags for you code. In the editor, it is the # symbol.
char *but = "test"; string s; s = buf;If you want to use strcpy, however, for copying one C string to another, you must make sure you have the library included:
#include <cstring> //For C++ #include <string.h> //For C
BTW, use the code tags for you code. In the editor, it is the # symbol.
Latinamne loqueris?
#3
Posted 19 September 2011 - 04:57 PM
You can simply use the (overloaded) assignment operator to assign a char * to a std::string. This will copy all the data over automatically.
If you're trying to copy from a std::string to a char *, the best way to do that is with strncpy(), but you have to get the char data from the string first!
If you're trying to copy from a std::string to a char *, the best way to do that is with strncpy(), but you have to get the char data from the string first!
size_t my_str_size_with_null_end = my_str->size() + 1; char *buffer = malloc(my_str_size_with_null_end); strncpy(buffer, my_str->c_str(), my_str_size_with_null_end);
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?
#4
Posted 19 September 2011 - 05:01 PM
In 'char * but= "test";' , I suppose you meant 'char * buf= "test";' ....
char *but = "test"; string s; s = buf;...
This editor? I thought it was [CODE] and [/CODE] that make code tags....In the editor, it is the # symbol.
#5
Posted 19 September 2011 - 05:37 PM
@RhetoricalRuvim: Both of them work. I prefer [noparse][code=auto:0][/noparse] tags myself, along with any other tags I can use. The only real trouble I have with the quick reply window here is that it keeps the formatting of pasted text, which forces me to put it through a text editor first.

If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?
#6
Posted 19 September 2011 - 06:15 PM
LOL typo, sorry.
Latinamne loqueris?
#8
Posted 29 October 2012 - 12:43 AM
don't forget the size, if you don't specify the buf size in the constructor, you could read garbage:
std::string s(buf, buf_size);
I'd use this, i had problems reading input files in a game and that was the cause
std::string s(buf, buf_size);
I'd use this, i had problems reading input files in a game and that was the cause
Also tagged with one or more of these keywords: string
Language Forums →
C and C++ →
Why is similar string comparison giving different result?Started by nick112, 29 May 2017 ![]() |
|
![]() |
||
Language Forums →
C and C++ →
converting string to floatStarted by JonElias, 14 May 2016 ![]() |
|
![]() |
||
Language Forums →
HTML, CSS and Javascript →
How to find location of all the times a specific letter appears in stringStarted by 4ringsa6, 02 Mar 2016 ![]() |
|
![]() |
||
Language Forums →
Java →
How do I remove a character from a String?Started by dreddooo, 01 Oct 2015 ![]() |
|
![]() |
||
Language Forums →
Java →
Help needed making a String, which has all the letters in the rack (Scrabble game)Started by BobbyRedSox28, 25 May 2015 ![]() |
|
![]() |
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download