加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

C语言将目录中文件按时间顺序从小到大排序,并输出文件名

(2013-05-09 21:52:00)
分类: 学习总结

#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

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有