• 7093阅读
  • 1回复

打印指定目录下的文件名,特别对歌曲目录很有用 [复制链接]

上一主题 下一主题
离线浪漫天使
 
只看楼主 倒序阅读 楼主  发表于: 2009-01-19
— 本帖被 XChinux 执行加亮操作(2009-01-19) —
  1. #include <QtGui>
  2. int main( int argc, char ** argv )
  3. {
  4.     QApplication a( argc, argv );
  5. a.setQuitOnLastWindowClosed(true);
  6. //get the dir name
  7. QString dirName;
  8. if( argc <2 )
  9. {
  10.   QMessageBox::information(0,"debug","-d dirname");
  11.   exit(0);
  12. }
  13. else if( strcmp(argv[1],"-d" )==0 )
  14.   dirName = QString::fromLocal8Bit(argv[2]);
  15. //get the dir information
  16. QFileInfoList allfile;
  17. if( !dirName.isEmpty() )
  18. {
  19.   QDir dir(dirName);
  20.   if( dir.exists() )
  21.   {
  22.   allfile = dir.entryInfoList();
  23.   }
  24. }
  25. //get all the file name in this dir
  26. int i=0;
  27. if( allfile.count()<=0 )
  28. {
  29.   QMessageBox::information(0,"debug","there is no file in the dir");
  30.   exit(0);
  31. }
  32. else
  33. {
  34. //create the file to store the filename
  35.   QFile songfile(dirName+"/songname.txt");
  36.   bool openflag = false;
  37.   openflag = songfile.open(QIODevice::WriteOnly|QIODevice::Text);
  38.   if( openflag )
  39.   {
  40.   QTextStream out(&songfile);
  41.   for(i=0;i<allfile.count();++i)
  42.   {
  43.     QFileInfo info = allfile.at(i);
  44.     out<<info.fileName()<<endl;
  45.    
  46.   }
  47.   songfile.close();
  48.   }
  49.   else
  50.   {
  51.   QMessageBox::information(0,"debug","open error");
  52.   exit(0);
  53.   }
  54. }//end allfile.count
  55. exit(0);
  56.     //return a.exec();
  57. }


主要是为了方便别人选歌,一个一个歌名显然不是机器时代的做法。
就想到自己用过的qt呵呵:)
还有很多不完善的地方,比如目录下还有目录,这个就要用到递归,还请大家多多指正。

编译测试环境:xp + dev-cpp + qt441
离线pqfeng
只看该作者 1楼 发表于: 2009-05-14
严重感谢你的代码,我先借了你部分代码。
快速回复
限100 字节
 
上一个 下一个