473,394 Members | 1,371 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Convert string data type to char type ?

Hi,

I tried to convert string data type to char type so
that I can use strtok.

I have used c_str(). However, still getting error message
when compile .

Any help would be appreciate.

Thanks,

Oct 20 '05 #1
6 4977
tv****@hotmail.com wrote:
Hi,

I tried to convert string data type to char type so
that I can use strtok.

I have used c_str(). However, still getting error message
Which errors?
when compile .


When you compile what?

My crystal ball came just back from repair.
It says that your errors might have to do with the fact that strtok()
modifies the data which is not allowed on what c_str() returns.

Oct 20 '05 #2
<tv****@hotmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
I tried to convert string data type to char type so
that I can use strtok.

I have used c_str(). However, still getting error message
when compile .

Any help would be appreciate.


It is very difficult to help without seeing your code. In this case, my
guess is that you are passing what c_str() returns to strtok.

If so, look more closely and realize that what c_str() returns is not
compatible with what strtok takes: 'char const *' vs. 'char *'.

Ali

Oct 20 '05 #3
ptr =strtok(answer.c_str()," \t\n,()");
Error message:
invalid conversion from `const char*' to `char*'

Oct 20 '05 #4
<tv****@hotmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
ptr =strtok(answer.c_str()," \t\n,()");
Error message:
invalid conversion from `const char*' to `char*'


Please quote what you are replying to.

In this case, my guess is that you are showing us some code. Thank you... As
I said in my previous post, look more closely and realize that what c_str()
returns is not compatible with what strtok takes.

In other words, please check the documentations for c_str and strtok. You
may realize that strtok takes a 'char*' and unfortunately, c_str DOES NOT
return that type.

You need to copy the characters that are returned by c_str into a buffer,
then call strtok with that buffer:

#include <string.h>
/* ... */

// WARNING: buffer may be leaked if an exception is thrown
// before getting to the free(buffer) call below
//
// Also, strdup is not standard but exists on many systems

char * buffer = strdup(answer.c_str());
ptr = strtok(buffer, /* ... */);
/* ... */
free(buffer);

Ali

Oct 20 '05 #5
Ali, Thanks a million for your quick help.

Oct 20 '05 #6
In article <11**********************@g43g2000cwa.googlegroups .com>,
<tv****@hotmail.com> wrote:
I tried to convert string data type to char type so
that I can use strtok.

I have used c_str(). However, still getting error message
when compile .

Any help would be appreciate.


We don't know exactly what you did since you don't show
a working snippet. In the meantime, this may help:
http://www.comeaucomputing.com/techtalk/#atoi
It may not be what you want, but may have examples enough
to get you through your problem.
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Oct 20 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Markus Hämmerli | last post by:
I ' ll tra to convert a Cstring to char* without success. I working in a Unicode enabled environment this is working in Unicode CString source = _T("TestString"); TCHAR *szSource =...
16
by: Khuong Dinh Pham | last post by:
I have the contents of an image of type std::string. How can I make a CxImage object with this type. The parameters to CxImage is: CxImage(byte* data, DWORD size) Thx in advance
15
by: Yifan | last post by:
Hi Does anybody know how to convert System::String* to char*? I searched the System::String class members and did not find any. Thanks Yifan
3
by: t2581 | last post by:
Hi , I have database db2 udb 7.2 with following table and datatype CREATE DISTINCT TYPE APP.NOTEPAD AS SYSIBM .CLOB(4096); CREATE TABLE APP.AP_NOTE_PAD ( CONTROL_LOCATION INTEGER NOT NULL...
3
by: jacob navia | last post by:
Abstract: Continuing the discussion about abstract data types, in this discussion group, a string collection data type is presented, patterned after the collection in C# and similar languages...
5
by: Marc | last post by:
Hello dear, I have a string and every second char is a \0. Can I somehow convert it to a normal string. Or may-be in the underlying code I am doing something wrong, choose the wrong C# type? ...
12
by: Peter | last post by:
Trying to convert string to byte array. the following code returns byte array of {107, 62, 194, 139, 64} how can I convert this string to a byte array of {107, 62, 139, 65} ...
14
by: rtillmore | last post by:
Hello, I did a quick google search and nothing that was returned is quite what I am looking for. I have a 200 character hexadecimal string that I need to convert into a 100 character string. ...
0
Debadatta Mishra
by: Debadatta Mishra | last post by:
Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.