作者legnaleurc (CA)
看板C_and_CPP
标题Re: [问题] 字串程式问题
时间Sat May 9 23:55:42 2009
※ 引述《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
1F:推 joefaq:有人就是都用C++了写起来还是跟C一模一样 05/10 11:57