MFC中如何读取.txt文件中的数组

MFC中如何读取.txt文件中的数组,第1张

这么规律的数据就很容易处理了。先readline读一行数据,第一行舍弃。往后每次读一行,然后用字符分割函数,分割成两个字符串再转换一下就行了。你可以选择用for,也可以用while,判断为#就退出。以下是我从网页源码中提取背景音乐地址的代码,你可以看看

bool CMyWinInet_1Dlg::findstring(CString str)

{

char p=NULL;

CString Music_name="";

CString Music_link="";

CString name="";

CString link="";

bool tis=0;

bool flag=0;

p=strGetBuffer(strGetLength()+1);//把字符串传给字符指针

for(;p!='\0';p++)//从网页源代码中提取音乐地址

{

if(p==','){tis=1;p++;}

if(tis) name+=p;

if(p==':')

{

tis=0;

if(name=="xsong_name:"||name=="xsong_url:")

{

p++;

for(;p!=',';p++)

{

if(name=="xsong_name:")

{Music_name+=p; flag=0;link=Music_name;}

if(name=="xsong_url:")

{Music_link+=p;flag=1;}

}

if(flag)

{

int count = m_GridGetItemCount();

CString str;

strFormat(_T("%d"),count+1);

m_GridInsertItem(count,"");

m_GridSetItemText(count,0,link);

m_GridSetItemText(count,1,Music_link);

}

}

name="";

Music_name="";

Music_link="";

}

}

return 0;

}

MFC将单个数据写文件

[cpp] view plain copy

CStudent st;//假设有学生类,其构造函数会初始化三个成员变量m_stuID、m_stuID、m_stuScore

m_stuID = this->m_stuID;//this->m_stuID是窗体类中的成员

m_stuName = this->m_stuName;

m_stuScore = this->m_stuScore;

CFile file;

//=======================写=======================

fileOpen(_T("E:\\studentdata"),CFile::modeCreate|CFile::modeReadWrite);//打开/创建文件,打开后对其进行读写

CArchive ar(&file,CArchive::Mode::store);//这个包有两个方向(存储和读取),这里用文件进行存储

arWriteObject(&st);//把地址给他,它会自己写

//======================================================

arClose();

fileClose();

这是我的程序代码片段,有关获取鼠标点到列表框行列的代码,发给你参考下:

typedef pair<int,int> CellIndex; // Row, Column

// 当鼠标右键点击时有一个CPoint传给你,然后根据CPoint来获取列表框的行和列。

// 用HitTest函数即可获取。

CListCtrlEx::CellIndex CListCtrlEx::PointToIndex(const CPoint&point)

{

LVHITTESTINFO lvHitTestInfo;

CRect rect;

lvHitTestInfopt=point;

if(HitTest(&lvHitTestInfo)>=0||(SubItemHitTest(&lvHitTestInfo)>=0)&&lvHitTestInfoiItem>=0)

{

intnRow =lvHitTestInfoiItem;

intnColumnCount =GetColumnCount();

for(intnColumn=0;nColumn<nColumnCount;++nColumn)

{

if(GetCellRect(nRow,nColumn,rect))

{

if(rectPtInRect(point))

{

return make_pair(nRow,nColumn);

}

}

}

}

return make_pair(-1,-1);

}

把以下代码放到Win32控制台运行,亲测可用:

#include <windowsh>

#define FILEILTER "" //查找条件,例如查找exe格式的那么就用exe,当然也可以直接用setupexe

#include "iostreamh"

#include "stdioh"

BOOL IsRoot(LPCTSTR lpszPath)

{

TCHAR szRoot[4];

wsprintf(szRoot, "%c:\\", lpszPath[0]);

return (lstrcmp(szRoot, lpszPath) == 0);

}

void FindInAll(LPCTSTR lpszPath)

{

TCHAR szFind[MAX_PATH];

lstrcpy(szFind, lpszPath);

if (!IsRoot(szFind))

lstrcat(szFind, "\\");

lstrcat(szFind, FILEILTER); // 找所有文件

WIN32_FIND_DATA wfd;

HANDLE hFind = FindFirstFile(szFind, &wfd);

if (hFind == INVALID_HANDLE_VALUE) // 如果没有找到或查找失败

return;

do

{

if (wfdcFileName[0] == '')

continue; // 过滤这两个目录

if (wfddwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)

{

TCHAR szFile[MAX_PATH];

if (IsRoot(lpszPath))

wsprintf(szFile, "%s%s", lpszPath, wfdcFileName);

else

{

wsprintf(szFile, "%s\\%s", lpszPath, wfdcFileName);

FindInAll(szFile); // 如果找到的是目录,则进入此目录进行递归

}

}

else

{

TCHAR szFile[MAX_PATH];

if (IsRoot(lpszPath))

{

wsprintf(szFile, "%s%s", lpszPath, wfdcFileName);

}

else

{

wsprintf(szFile, "%s\\%s", lpszPath, wfdcFileName);

printf("%s\n",szFile);

}

// 对文件进行 *** 作

}

} while (FindNextFile(hFind, &wfd));

FindClose(hFind); // 关闭查找句柄

}

int main(int argc, char argv[])

{

FindInAll("E:"); //这里设置目录为E:,请根据需要更改

return 0;

}

以上就是关于MFC中如何读取.txt文件中的数组全部的内容,包括:MFC中如何读取.txt文件中的数组、MFC 怎么利用序列化读取路径的文件,得到数据、MFC单文档怎么获取列表控件的行数等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/10128511.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-05
下一篇2023-05-05

发表评论

登录后才能评论

评论列表(0条)

    保存