现需要读取一个标签对内中的数据。比如: <C++> int float double char bool </C++> 使用接口的声明如下: int readKeyword(string strLabel, string &strReturn); 参数说明: strLabel为标签对,如上面的“C++” strReturn为存放去读后的字符串。以一个空格为间隔。 如,上面读取后,存放格式为:“int float double char bool” 返回值为int类型。返回0表示没有错误。 返回1表示标签对没有找到。 返回2表示标签对找到,但没有找到结束的标签对( </C++> )。此时strReturn扔存放标签开始处到返回前找到的所有值。 返回3表示其他类型错误。 定义类名称为:IOKeyword 类中其他必要参数:string strFile:存放需要读取的文件路径。 使用类前。可以初始化这个路径。也可以重新设置这个文件名。 没弄到我所需要的功能,求改写!
- #include <iostream>
- #include <fstream>
- #include <string>
- using namespace std;
- class lOKeyword
- {
- private:
- string strLabel; //存放标签
- string *strReturn; //存放要输出的字符串
- ifstream read; //文件
- string temp;
- public:
- lOKeyword(); //打开文件
- ~lOKeyword(); //关闭文件
- int readKeyword(string strLabel, string &strReturn); //文件操作
- bool OutPut();
- };
- lOKeyword::lOKeyword()
- {
- read.open("1.txt", ios::in);
- }
- lOKeyword::~lOKeyword()
- {
- read.close();
- }
- int lOKeyword::readKeyword(string strLabel, string &strReturn)
- {
- getline(read, strReturn);
- if(strReturn != "<" + strLabel + ">")
- {
- return 1;
- }
- if(strReturn != "<"+ strLabel +">" + "</" + strLabel + ">")
- {
- return 2;
- }
-
- this->strLabel = strLabel;
- this->strReturn = &strReturn;
- return 0;
- }
- bool lOKeyword::OutPut()
- {
- cout << *strReturn << endl;
- return true;
- }
- int main()
- {
- lOKeyword one;
- string temp_1, *temp_2;
- cout << "请输入标签名: ";
- cin >> temp_1;
- int num;
- num = one.readKeyword(temp_1, *temp_2);
- if(1 == num)
- {
- cerr << "标签没有找到"<< endl;
- return -1;
- }
- if(2 == num)
- {
- cerr << "没有找到尾标签" << endl;
- return -1;
- }
- one.OutPut();
- return 0;
- }
复制代码
|