鱼C论坛

 找回密码
 立即注册
查看: 3317|回复: 6

关于字读取

[复制链接]
发表于 2012-3-21 00:57:30 | 显示全部楼层 |阅读模式
20鱼币

现需要读取一个标签对内中的数据。比如:
<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:存放需要读取的文件路径。
使用类前。可以初始化这个路径。也可以重新设置这个文件名。
没弄到我所需要的功能,求改写!

  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. using namespace std;
  5. class lOKeyword
  6. {
  7. private:
  8. string strLabel; //存放标签
  9. string *strReturn; //存放要输出的字符串
  10. ifstream read;  //文件
  11. string temp;
  12. public:
  13. lOKeyword();  //打开文件
  14. ~lOKeyword();  //关闭文件
  15. int readKeyword(string strLabel, string &strReturn); //文件操作
  16. bool OutPut();
  17. };
  18. lOKeyword::lOKeyword()
  19. {
  20. read.open("1.txt", ios::in);
  21. }
  22. lOKeyword::~lOKeyword()
  23. {
  24. read.close();
  25. }
  26. int lOKeyword::readKeyword(string strLabel, string &strReturn)
  27. {
  28. getline(read, strReturn);
  29. if(strReturn != "<" + strLabel + ">")
  30. {
  31.   return 1;
  32. }
  33. if(strReturn != "<"+ strLabel +">" + "</" + strLabel + ">")
  34. {
  35.   return 2;
  36. }

  37. this->strLabel = strLabel;
  38. this->strReturn = &strReturn;
  39. return 0;
  40. }
  41. bool lOKeyword::OutPut()
  42. {
  43. cout << *strReturn << endl;
  44. return true;
  45. }
  46. int main()
  47. {
  48. lOKeyword one;
  49. string temp_1, *temp_2;
  50. cout << "请输入标签名: ";
  51. cin >> temp_1;
  52. int num;
  53. num = one.readKeyword(temp_1, *temp_2);
  54. if(1 == num)
  55. {
  56.   cerr << "标签没有找到"<< endl;
  57.   return -1;
  58. }
  59. if(2 == num)
  60. {
  61.   cerr << "没有找到尾标签" << endl;
  62.   return -1;
  63. }
  64. one.OutPut();
  65. return 0;
  66. }
复制代码

最佳答案

查看完整内容

几个错误: 1. 成员指针strReturn没有初始化 且正确的命名应是m_strReturn ,在构造函数中m_strReturn=NULL 2.main中,传入的引用指针值的引用,这个指针 temp_2也没有初始化 , *temp_2取到的地址将是一个不可预知的值 3.int lOKeyword::readKeyword(string strLabel, string &strReturn)这个函数,我想你是要一次读取一个标签对的内容,可能有多个标签对吧,一个标签对也不一定是在同一行中,那么你只getline一次肯定是不行的。 ...
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2012-3-21 00:57:31 | 显示全部楼层
几个错误:
1. 成员指针strReturn没有初始化 且正确的命名应是m_strReturn ,在构造函数中m_strReturn=NULL
2.main中,传入的引用指针值的引用,这个指针 temp_2也没有初始化 , *temp_2取到的地址将是一个不可预知的值
3.int lOKeyword::readKeyword(string strLabel, string &strReturn)这个函数,我想你是要一次读取一个标签对的内容,可能有多个标签对吧,一个标签对也不一定是在同一行中,那么你只getline一次肯定是不行的。

3.后续判断的问题,你不能保证你读到的前几个字符,就一定是<strLabel> 或者</strLabel>,所以应该用find,先找到<strLabel>,然后再找</strLabel>,取<strLabel>和</strLabel>中间的内容就是你要的,当然,还可考虑你的文件格式有没有嵌套的比如
<strLable>
  <strLabel1></strLabel1>
<strLabel2>
    <strLabel1></strLabel1>

</strLabel2>  
</strLabel>
  再说,你的判断条件,strRetun既要等于<strLabel> 又要等于</strLabel>?

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2012-3-21 01:47:11 | 显示全部楼层
路过帮顶 路过帮顶
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2012-3-21 12:45:23 | 显示全部楼层
e ,这个有点复杂,你一步步看吧,用编译器。不太好说
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2012-3-21 13:58:37 | 显示全部楼层
string 没用过 用得char*
还有ofstream不需要是成员变量吧
要是成员变量也应该是 ofstream的指针

char lpTemp[255];
    while (!read->eof())
    {
        read->getline((char*)lpTemp, 255);
        cout << lpTemp<< endl;
    }
可以构造时就打开文件 也可以第一次调用读时打开文件 保证一个对象只有一个ifstream实例即可
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-6-10 19:35:32 | 显示全部楼层
:lol ,写了一堆,就不粘代码了,给分就行:P
擦,要隔60秒才能找你要分。。。:P
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-9-8 23:35:13 | 显示全部楼层

很不错呀,多谢分享!
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-13 20:42

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表