#include
#include
#include
using namespace std;
int main()
{
// ">>" stands for
append,whereas ">" stands for override
// "/B" stands for omitting the
title info and abstract
// use "help dir" to read the
help file
// list the files in the
dir
//
/B表示去掉标题信息和摘要
/O:D表示按照日期时间从先到后对文件进行排序
system("dir d:\\lin_test /B /O:D >
d:\\filelist.txt");
// add the end of file '$'
in append type
FILE *file=fopen("d:\\filelist.txt","at");
fputc('$',file);
fclose(file);
ifstream inFile;
inFile.open("d:\\filelist.txt");
string str;
// not the end of file,then enter the if
sentence
if(!inFile.eof())
{
// arg0:input stream arg1:store
a line into str arg2:exit the
getline when meet '$'
getline(inFile,str,'$');
cout<<str<<endl;
}
return 0;
}
在D:\lin_test\下有如下五个文件:
test1.jpg
test2.jpg
testxp.jpg
li.jpg jie.jpg
运行程序以后在d:\下面有一个filelist.txt文件,其内容如下:
test1.jpg
test2.jpg
testxp.jpg
jie.jpg
li.jpg
$
控制台的输结果如下:
test1.jpg
test2.jpg
testxp.jpg
jie.jpg
li.jpg
press any key to continue!!!
请注意在li.jpg下面还行一行,只不过那行内容为空。应该是读取的filelist.txt中最后一行除$之外的其他字符。我们在DOS下面运行一下dir命令,可以看到程序和命令的结果保持一致。
http://s7/mw690/7476a4db07c6ea874bc06&690
加载中,请稍候......