MFC下如何读取.txt文件 在MFC中的CFile类有个Read方法如何读取如下格式的。txt文件内容书名;价格书名;价格书名;价格我想分别获得书名和价格,这样我可以对其做相关操作或者有什么其他方法可以实现 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 用ReadLine()读取了一行后怎么分别获得书名和价格,能有代码吗? 假设你的txt文件内容是这样的:书名1:价格1书名2:价格2你可以先用一个字符串将这些文本内容存下来,比如:CString strtemp="";file.ReadHuge(strtemp,file.GetLength());//file是你要操作的文件对象然后利用CString的Find函数将相应的书名找出来然后就可以获得价格这里就主要是一些字符串的操作,相信你有能力操作吧! txt文件里面具体有什么是不知道的只知道他的结构书名1;价格1书名2;价格2问题就是怎么使用这个字符串,MFC还在学习中,不熟悉他的函数最好能有具体的代码参考下 利用 ReadLine, 读取每一行,然后对每一行字符串进行解析, 提取出书名和价格 利用ReadLine取得一行之后,分割字符串,像你"书名1:价格1"这样的字符串就用函数AfxExtractSubString以":"为特征符号来分割,ok?这是我用VC开发网上书店www.TianfuBook.com 试试下面的方式。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) 修改一下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) 11.txt文件内容C++ Primer ; 99.00C++ 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(); } 用 ReadLine, 读取每一行,然后对每一行字符串进行解析, 分割字符串,像你"书名1:价格1"这样的字符串就用函数AfxExtractSubString以":"为特征符号来分割 CStdioFile打开 readline读取 AfxExtractSubString分割 stlgetline()函数加我的群19206896 我建议:1,先把文件的内容读取到一个CString的对象里面,然后对CString对象进行操作;2,用CString的Find函数查找逗号,这样从第一个位置开始到逗号的位置就是书名3,然后接着用Find函数从逗号位置查找换行“\r\n”,这就找到了价格4,循环步骤2,3就可以查找到所有的对应的书名和价格。CString类的功能函数是很强大的,真的,可以仔细看看。 OpenGL 等弧长切分曲面? 如何解压winrar压缩包? 如何显示行号 如何在一个线程中改变控件上的文件字? 关于灰掉菜单的问题 帮我优化这个算法 高手呀,可以说具体点么,急 如何监控网络邻居,在何时对共享文件进行了什么样的操作 100问一个超简单的新建窗口的问题,马上给分!!UP有分! << Inside visual c++>>第五版的中文版出了吗? 国米VS巴萨 顶狂人 HOOK的DLl注入中按键模拟问题
怎么分别获得书名和价格,
能有代码吗?
书名2:价格2
你可以先用一个字符串将这些文本内容存下来,比如:
CString strtemp="";
file.ReadHuge(strtemp,file.GetLength());//file是你要操作的文件对象然后利用CString的Find函数将相应的书名找出来
然后就可以获得价格
这里就主要是一些字符串的操作,相信你有能力操作吧!
只知道他的结构书名1;价格1
书名2;价格2
问题就是怎么使用这个字符串,MFC还在学习中,不熟悉他的函数
最好能有具体的代码参考下
char name[50], price;
FILE * file = fopen("fileName", "r"); while( fgets(buff, 1024, file) != 0)
{
sscanf(buff, "%s;%s", name, price);
...
}fclose(file)
char name[50], price[50];
FILE * file = fopen("fileName", "r"); while( fgets(buff, 1024, file) != 0)
{
sscanf(buff, "%s;%s", name, price);
...
}fclose(file)
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();
}
getline()函数
加我的群19206896
1,先把文件的内容读取到一个CString的对象里面,然后对CString对象进行操作;
2,用CString的Find函数查找逗号,这样从第一个位置开始到逗号的位置就是书名
3,然后接着用Find函数从逗号位置查找换行“\r\n”,这就找到了价格
4,循环步骤2,3就可以查找到所有的对应的书名和价格。CString类的功能函数是很强大的,真的,可以仔细看看。