[問題] 字串程式問題

看板C_and_CPP (C/C++)作者 (因為貼心所以是好人)時間16年前 (2009/05/09 22:59), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串1/2 (看更多)
#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); //再次取出字串 } //==========END==========// printf("\n"),system("PAUSE"); return 0; } 01.txt裡面是一字串 0000 copy string 因為我getline出來是一字串 無法用strtok 所以我複製字串轉成char 請問那行s1.c_str()是什麼意思且可這樣用嗎? 再請問複製貼上後沒辦法複製到空格 這是為什麼呢? -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 61.59.12.217 ※ 編輯: jeffy84123 來自: 61.59.12.217 (05/09 23:05)
文章代碼(AID): #1A1Pfbvc (C_and_CPP)
討論串 (同標題文章)
文章代碼(AID): #1A1Pfbvc (C_and_CPP)