C++ string::npos,结合find,或者sscanf

来源:互联网 发布:单片机12864 编辑:程序博客网 时间:2024/06/05 09:01


    首先getline

getline(istream &in, string &s)

从输入流读入一行到string s

•功能:
–从输入流中读入字符,存到string变量
–直到出现以下情况为止:
•读入了文件结束标志
•读到一个新行
•达到字符串的最大长度
–如果getline没有读入字符,将返回false,可用于判断文件是否结束
[cpp] view plain copy print?在CODE上查看代码片派生到我的代码片
  1. #include<iostream>  
  2. #include<fstream>  
  3. #include<string>  
  4.   
  5. using namespace std;  
  6.   
  7. int main()  
  8. {  
  9.     string buff;  
  10.     ifstream infile;  
  11.     ofstream outfile;  
  12.     cout<<"Input file name: "<<endl;  
  13.     cin>>buff;  
  14.     infile.open(buff.c_str());  
  15.   
  16.     if(!infile)  
  17.         cout<<"error"<<buff<<endl;  
  18.       
  19.     cout<<"Input outfile name: "<<endl;  
  20.     cin>>buff;  
  21.     outfile.open(buff.c_str());  
  22.       
  23.     while(getline(infile, buff))  
  24.         outfile<<buff<<endl;  
  25.   
  26.     infile.close();  
  27.     outfile.close();  
  28.     return 0;  
  29.   
  30. }  


      

http://blog.csdn.net/stpeace/article/details/13069403

设1.txt文件内容如下:

name = baidu
url = www.baidu.com

 

       看程序:

[cpp] view plain copy print?
  1. #include <fstream>  
  2. #include <string>  
  3. #include <iostream>  
  4. using namespace std;  
  5.   
  6. int main()  
  7. {  
  8.     ifstream in("1.txt");  
  9.     string filename;  
  10.     string line;  
  11.     string s;  
  12.     string :: size_type pos;  
  13.   
  14.     if(in) // 有该文件  
  15.     {  
  16.         while (getline (in, line)) // line中不包括每行的换行符  
  17.         {   
  18.             pos = line.find("name");  
  19.             if(pos != string :: npos)  
  20.             {  
  21.                 s = line.substr(pos + strlen("name") + strlen(" = "));  
  22.                 cout << s.c_str() << endl;  
  23.             }  
  24.   
  25.             pos = line.find("url");  
  26.             if(line.find("url") != string :: npos)  
  27.             {  
  28.                 s = line.substr(pos + strlen("url") + strlen(" = "));  
  29.                 cout << s.c_str() << endl;  
  30.             }  
  31.   
  32.         }  
  33.     }  
  34.     else // 没有该文件  
  35.     {  
  36.         cout <<"no such file" << endl;  
  37.     }  
  38.   
  39.     return 0;  
  40. }  

     结果为:

baidu

www.baidu.com

 

    当然也可以用sscanf, 如下:

[cpp] view plain copy print?
  1. #include <fstream>  
  2. #include <string>  
  3. #include <iostream>  
  4. using namespace std;  
  5.   
  6. int main()  
  7. {  
  8.     ifstream in("1.txt");  
  9.     string filename;  
  10.     string line;  
  11.     char s1[1024] = {0};  
  12.     char s2[1024] = {0};  
  13.   
  14.     if(in) // 有该文件  
  15.     {  
  16.         while (getline (in, line)) // line中不包括每行的换行符  
  17.         {   
  18.             if(2 == sscanf(line.c_str(), "%s = %s", s1, s2))  
  19.             {  
  20.                 cout << s2 << endl;  
  21.             }  
  22.             else  
  23.             {  
  24.                 return 1;  
  25.             }  
  26.               
  27.         }  
  28.     }  
  29.     else // 没有该文件  
  30.     {  
  31.         cout <<"no such file" << endl;  
  32.     }  
  33.   
  34.     return 0;  
  35. }  



拓展:

查找两个字符串中的公共字符;

返回公共字符的索引;

[cpp] view plain copy print?在CODE上查看代码片派生到我的代码片
  1. #include<iostream>  
  2. #include<string>  
  3.   
  4. using namespace std;  
  5.   
  6. int main()  
  7. {  
  8.     string s = "**Gteate Wall**!";  
  9.     string t = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";  
  10.     cout<<"s: "<<s<<endl;  
  11.     cout<<"t: "<<t<<endl;  
  12.   
  13.     int first=s.find_first_of(t);  
  14.   
  15.     if(first == string::npos){  
  16.         cout<<"s中所有字符均不在t中"<<endl;  
  17.     }else {  
  18.         cout<<"s中出现在t中的字符的第一个字符:"<<s[first]<<endl;  
  19.     }  
  20.   
  21.     int last = s.find_last_of(t);  
  22.     if(last == string::npos){  
  23.         cout<<"s中所有字符均不在t中"<<endl;  
  24.         return 1;  
  25.     }else {  
  26.         cout<<"s中出现在t的字符的最后一个字符:"<<s[last]<<endl;  
  27.         return 1;  
  28.     }  
  29.   
  30. }  

string::npos的一些说明

string::npos 的一些说明

一、定义

std:: string ::npos的定义:

static const size_t npos = -1;

表示 size_t 的最大值( Maximum value for size_t ,如果对 -1 表示size_t的最大值有疑问可以采用如下代码验证:

#include <iostream>#include <limits>#include <string>using namespace std;int main(){    size_t npos = -1;    cout << "npos: " << npos << endl;    cout << "size_t max: " << numeric_limits<size_t>::max() << endl;}

在我的PC上执行结果为:

                 npos:           4294967295

                 size_t max:  4294967295

可见他们是相等的,也就是说npos表示size_t的最大值

二、使用

2.1 如果作为一个 返回值 (return value) 表示没有找到匹配项 ,例如:

#include <iostream>#include <limits>#include <string>using namespace std;int main(){  string filename = "test";  cout << "filename : " << filename << endl;  size_t idx = filename.find('.');   //作为return value,表示没有匹配项  if(idx == string::npos)  {    cout << "filename does not contain any period!" << endl;  }}
2.2 但是string::npos作为string的成员函数的一个 长度参数 时,表示“ 直到字符串结束 (until the end of the string)”。例如:
tmpname.replace(idx+1, string::npos, suffix);

这里的string::npos就是一个长度参数,表示直到字符串的结束,配合idx+1表示,string的剩余部分。

#include <iostream>#include <limits>#include <string>using namespace std;int main(){  string filename = "test.cpp";  cout << "filename : " << filename << endl;  size_t idx = filename.find('.');   //as a return value  if(idx == string::npos)  {    cout << "filename does not contain any period!" << endl;  }  else  {    string tmpname = filename;    tmpname.replace(idx + 1, string::npos, "xxx"); //string::npos作为长度参数,表示直到字符串结束    cout << "repalce: " << tmpname << endl;  }}

执行结果为:

filename:test.cpp

replace: test.xxx      


0 0