在MFC中的CFile类有个Read方法如何读取如下格式的。txt文件内容书名;价格
书名;价格
书名;价格我想分别获得书名和价格,这样我可以对其做相关操作
或者有什么其他方法可以实现

解决方案 »

  1.   

    用ReadLine()读取了一行后
    怎么分别获得书名和价格,
    能有代码吗?
      

  2.   

    假设你的txt文件内容是这样的:书名1:价格1
    书名2:价格2
    你可以先用一个字符串将这些文本内容存下来,比如:
    CString strtemp="";
    file.ReadHuge(strtemp,file.GetLength());//file是你要操作的文件对象然后利用CString的Find函数将相应的书名找出来
    然后就可以获得价格
    这里就主要是一些字符串的操作,相信你有能力操作吧!
      

  3.   

    txt文件里面具体有什么是不知道的
    只知道他的结构书名1;价格1
    书名2;价格2
    问题就是怎么使用这个字符串,MFC还在学习中,不熟悉他的函数
    最好能有具体的代码参考下
      

  4.   

    利用 ReadLine, 读取每一行,然后对每一行字符串进行解析, 提取出书名和价格
      

  5.   

    利用ReadLine取得一行之后,分割字符串,像你"书名1:价格1"这样的字符串就用函数AfxExtractSubString以":"为特征符号来分割,ok?这是我用VC开发网上书店www.TianfuBook.com
      

  6.   

    试试下面的方式。char buff[1024];                 
    char name[50], price;                     
                                     
    FILE * file = fopen("fileName", "r");  while( fgets(buff, 1024, file) != 0)
    {
    sscanf(buff, "%s;%s", name, price);

    ...
    }fclose(file)
      

  7.   

    修改一下char buff[1024];                 
    char name[50], price[50];                     
                                     
    FILE * file = fopen("fileName", "r");  while( fgets(buff, 1024, file) != 0)
    {
        sscanf(buff, "%s;%s", name, price);
        
        ...
    }fclose(file)
      

  8.   

    11.txt文件内容
    C++ Primer ; 99.00
    C++ Template ; 89.90
    -----------------------------------
    //C/C++ code
    #include <map>
    using namespace std;map<CString, CString> bookInfo;
        try
        {
            CString strLine(_T(""));
            CString strName(_T(""));
            CString strPrice(_T(""));
            LPCTSTR szToken = _T(";");
            CString strText(_T(""));
            CStdioFile file;
            file.Open(_T("F:\\11.txt"), CFile::modeRead);        
            
            while(file.ReadString(strLine))
            {
                int curPos = 0;
                int nIndex = 1;            while(_T("") != (strText = strLine.Tokenize(szToken, curPos)))
                {
                    strText.Trim(_T(" "));
                    if(nIndex % 2)
                    {
                        strName = strText;
                    }
                    else
                    {
                        strPrice = strText;
                    }
                    ++nIndex;
                }
                bookInfo.insert(make_pair(strName, strPrice));    
            }
            file.Close();        strName.Empty();
            strPrice.Empty();
            strText.Empty();        for(map<CString, CString>::const_iterator iter = bookInfo.begin(); iter != bookInfo.end(); iter++)
            {
                strName = iter->first;
                strPrice = iter->second;
                strText += strName + _T(" ; ") + strPrice + _T("\r\n");
            }
            AfxMessageBox(strText);
        }
        catch(CFileException* e)
        {
            e->ReportError();
            e->Delete();
        }
      

  9.   

    用 ReadLine, 读取每一行,然后对每一行字符串进行解析, 分割字符串,像你"书名1:价格1"这样的字符串就用函数AfxExtractSubString以":"为特征符号来分割
      

  10.   

    CStdioFile打开 readline读取 AfxExtractSubString分割
      

  11.   

    stl
    getline()函数
    加我的群19206896
      

  12.   

    我建议:
    1,先把文件的内容读取到一个CString的对象里面,然后对CString对象进行操作;
    2,用CString的Find函数查找逗号,这样从第一个位置开始到逗号的位置就是书名
    3,然后接着用Find函数从逗号位置查找换行“\r\n”,这就找到了价格
    4,循环步骤2,3就可以查找到所有的对应的书名和价格。CString类的功能函数是很强大的,真的,可以仔细看看。