作者jeffy84123 (因為貼心所以是好人)
看板C_and_CPP
標題[問題] 字串程式問題
時間Sat May 9 22:59:48 2009
#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)