المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : كيف يتم تحويل الـ string الى char حتى يمكنني استخدام الـ strtok في ++C؟



كابتن السفينة
05-10-2007, 10:13 PM
السلام عليكم

عندي مشكلة بسيطة ومو قادر اكمل البرنامج بسببها..
هذا مثال بسيط عشان أوضح المشكلة:


# include <iostream>
# include <string>

using namespace std;

int main () {

string line = "This is a line.";
char *tokenPtr;

tokenPtr =strtok(line," ");

while (tokenPtr != NULL)
{
cout << tokenPtr << endl;
tokenPtr=strtok(NULL," ");
}

return 0;
}

يطلع لي خطأ اثناء التشغيل..
فمثلا لو بدّلت string line الى [ ]char line بيمشي..

فكيف اقدر استخدم الstrtok مع الstring؟ (لان غصبا علي استخدمه)

وشكرا جزيلا مقدما..

TeamFlex
06-10-2007, 02:09 AM
لي 3 سنين عن ++C
اسف ما اتذكر شي

A.k.A Dragon
17-10-2007, 08:24 AM
sadly, you cannot use strok with strings :/ .... but you can convert a char array to a string using the following:
string name = “Bob”;
char nameC[20] = “Bill”;
//toCString now references the C-string “Bob”
char *toCString = name.c_str();
//toCppString now holds the characters “Bill”
string toCppString(nameC);

this might help ya go around the whole problem, atleast that's what i think.

I hope i wasnt too late with the reply

A.k.A Dragon
17-10-2007, 08:25 AM
string name = “Bob”;

char nameC[20] = “Bill”;



//toCString now references the C-string “Bob”

char *toCString = name.c_str();



//toCppString now holds the characters “Bill”

string toCppString(nameC);