使用C ++将目录的文件名写入txt文件? [英] Write file names of a directory into a txt file with C++?
本文介绍了使用C ++将目录的文件名写入txt文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用c ++将目录的文件名写入txt文件?
我尝试过:
#include < iostream >
#include < io.h >
#include < time.h >
#include < string >
<小号pan class =code-keyword> using namespace std;
string Chop(string& str)
{
string res = str;
int len = str.length();
if (str [len - 1 ] == ' r')
{
res.replace(len - 1 , 1 , );
}
len = str.length();
if(str [len - 1] =='n')
{
res.replace(len - 1, 1,);
}
return res;
}
void DumpEntry(_finddata_t& data)
{
string createtime(ctime(& data.time_create) ));
cout<< Chop(创建时间)<< t;
cout<< data.size<< t;
if ((data.attrib& _A_SUBDIR)== _A_SUBDIR)
{
cout<< [<< data.name<< ]<< ENDL;
}
else
{
cout<< data.name<< ENDL;
}
}
int main()
{
_finddata_t data;
int ff = _findfirst( .. /*.*\",& data);
if (ff!= - 1 )
{
< span class =code-keyword> int res = 0 ;
while (res!= - 1 )
{
DumpEntry (数据);
res = _findnext(ff,& data);
}
_findclose(ff);
}
return 0 ;
}
解决方案
了解基本文件I / O 。
提示:打开文件一次并将其用作DumpEntry函数的参数
How do I Write file names of a directory into a txt file with c++?
What I have tried:
#include <iostream>
#include <io.h>
#include <time.h>
#include <string>
using namespace std;
string Chop(string &str)
{
string res = str;
int len = str.length();
if (str[len - 1] == 'r')
{
res.replace(len - 1, 1, ");
}
len = str.length();
if (str[len - 1] == 'n')
{
res.replace(len - 1, 1, ");
}
return res;
}
void DumpEntry(_finddata_t &data)
{
string createtime(ctime(&data.time_create));
cout << Chop(createtime) << "t";
cout << data.size << "t";
if ((data.attrib & _A_SUBDIR) == _A_SUBDIR)
{
cout << "[" << data.name << "]" << endl;
}
else
{
cout << data.name << endl;
}
}
int main()
{
_finddata_t data;
int ff = _findfirst ("../*.*", &data);
if (ff != -1)
{
int res = 0;
while (res != -1)
{
DumpEntry(data);
res = _findnext(ff, &data);
}
_findclose(ff);
}
return 0;
}
解决方案
Learn the basic file I/O.
Tip: open the file once and use it as parameter for DumpEntry function.
这篇关于使用C ++将目录的文件名写入txt文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文