Re: [問題] 字串程式問題

看板C_and_CPP (C/C++)作者 (CA)時間16年前 (2009/05/09 23:55), 編輯推噓1(100)
留言1則, 1人參與, 最新討論串2/2 (看更多)
※ 引述《jeffy84123 (因為貼心所以是好人)》之銘言: : #include <iostream> : #include <conio.h> : #include <fstream> : #include<stdio.h> : #include<stdlib.h> : #include<string.h> : using namespace std; : int main(int argc, char* argv[]){ : string s1,s2,s3,s4,s5; : fstream file("01.txt",ios::in); : getline(file,s1); : getline(file,s2); : getline(file,s3); : getline(file,s4); : getline(file,s5); : //==========START==========// : char string[20]; : strcpy(string,s1.c_str() ); : //複製字串轉成char : char sep[]=" "; //分隔字元 : char *pStr; //用指標取出字串 : pStr=strtok(string,sep); //取出第一個字串 : while(pStr!=NULL){ : printf("%s",pStr); //顯示字串 : pStr=strtok(NULL,sep); //再次取出字串 : } // Please use string stream std::string token; std::istringstream sin( s1 ); while( sin << token ) { std::cout << token; } : //==========END==========// : printf("\n"),system("PAUSE"); : return 0; : } : 01.txt裡面是一字串 0000 copy string : 因為我getline出來是一字串 : 無法用strtok 所以我複製字串轉成char 因為 std::string 不需要 strtok 了 strtok 真的是一個設計不良的標準函式 請參照上面的 code : 請問那行s1.c_str()是什麼意思且可這樣用嗎? 取出 C-style string 編譯器說可以用就可以用 : 再請問複製貼上後沒辦法複製到空格 這是為什麼呢? What are you talking about? -- 自High筆記(半荒廢) http://legnaleurc.blogspot.com/ -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 59.127.21.174

05/10 11:57, , 1F
有人就是都用C++了寫起來還是跟C一模一樣
05/10 11:57, 1F
文章代碼(AID): #1A1QU4W_ (C_and_CPP)
討論串 (同標題文章)
文章代碼(AID): #1A1QU4W_ (C_and_CPP)