求助!遍历一个文件夹下的txt文件,并可以随机的读取一篇txt的内容出来。赋值给一个字符串保存,

weixin_42976685 2019-04-16 03:23:18
BOOL CWebDlg::OnInitDialog()
{
CDialog::OnInitDialog();

// Add "About..." menu item to system menu.

// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);

CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}

// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon

// TODO: Add extra initialization here
ffFile("","txt")

return TRUE; // return TRUE unless you set the focus to a control
}


这是系统自动的代码,加了一句ffFile("","txt"),因为添加这个按F9,和F5之后才能进行调试,才能抓起值,我下面的程序只能读出文件夹,第一个冒号填的文件夹,第二个该填什么呢?





本人刚接触VC++,请各位能指点一二吗
在前面的
void CWebDlg::ff(CString strDir,CString strExt)
{
static int i=0,j=0;
CFileFind fileFinder ;
CString strWildcard = strDir;

if(strWildcard.Right(1) != "\\")
strWildcard += "\\";
strWildcard += "*.*";


BOOL fSuccess = fileFinder.FindFile(strWildcard) ;

while (fSuccess)
{
fSuccess = fileFinder.FindNextFile() ;

if (fileFinder.IsDots()) {
continue ;
}

if (fileFinder.IsDirectory())
{

ff(fileFinder.GetFilePath(), strExt) ; //递归调用
}
else
{

CString fpath[1024];
CString fname[1024];
int artnum;
CString strFilePath = fileFinder.GetFileName();
if (0 == strExt.CompareNoCase(PathFindExtension(strFilePath)))
{
fpath[i++]=strFilePath;
fname[j++]=fileFinder.GetFileName();
artnum++;
}
}
}

fileFinder.Close() ;

...全文
44 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
schlafenhamster 2019-04-16
  • 打赏
  • 举报
回复
例子

typedef struct FileInfo
{
string szFileName;
string szFilePath;
string szCreateTime;
}FileInfo;

vector<FileInfo>fileList;

//
void find(LPCTSTR lpPath,vector<FileInfo>& FileList)
{
string szFind(lpPath);
WIN32_FIND_DATA FindFileData;
szFind+=_T("\\*.*");
HANDLE hFind=::FindFirstFile(szFind.c_str(),&FindFileData);
if(INVALID_HANDLE_VALUE==hFind) return;
//
do
{
if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{// dir
if(FindFileData.cFileName[0] != '.')
{
string szPath(lpPath);
szPath+=_T("\\");
szPath+=FindFileData.cFileName;
//TRACE1("%s\n",szPath.c_str());
find(szPath.c_str(),FileList);
}
}
else
{// file
string strFileName=FindFileData.cFileName;//文件名
//TRACE1("%s\n",strFileName.c_str());
int at = strFileName.find_last_of('.');
if(at<0) continue;// file without extention
string ext=strFileName.substr(at);
//TRACE1("%s\n",ext.c_str());
//
at=0;// reset
while(((int)at=suffixStr.find(ext,at))>=0)// find type
// if(stricmp(suffixStr.c_str(),_T(".txt"))==0)
{// not suffixStr.c_str()==_T(".txt") ; never == !
//afxDump << at << "\n";
at+=ext.size();// move to seperetor '.'
if(suffixStr[at] != '.') continue;// not wanted !
//
FILETIME ft=FindFileData.ftCreationTime;
SYSTEMTIME st={0};
TCHAR buf[64]={0};
FileTimeToLocalFileTime(&(FindFileData.ftCreationTime),&ft);
FileTimeToSystemTime(&ft,&st);
sprintf(buf,_T("%4d年%02d月%02d日%02d:%02d:%02d"),
st.wYear,st.wMonth,st.wDay,st.wHour/*北京时间*/,st.wMinute,st.wSecond);
string strFilePath=lpPath;
FileInfo fi;
fi.szFileName=strFileName;
fi.szFilePath=strFilePath;
fi.szCreateTime=buf;
//TRACE1("%s\n",strFileName.c_str());
FileList.push_back(fi);
}
}
} while (::FindNextFile (hFind, &FindFileData));
}

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧