免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 5523 | 回复: 10
打印 上一主题 下一主题

请教,怎么解决在linux和vc里读取文件夹下文件名的功能 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-03-19 14:14 |只看该作者 |倒序浏览
有个项目,要用ansi c读取目录下的文件名,但是有个要求,在linux下和vc里都要编译通过
我是满足了这头,满足不了那头,头疼呀

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
2 [报告]
发表于 2007-03-19 14:47 |只看该作者
使用宏来处理吧。

论坛徽章:
95
程序设计版块每日发帖之星
日期:2015-09-05 06:20:00程序设计版块每日发帖之星
日期:2015-09-17 06:20:00程序设计版块每日发帖之星
日期:2015-09-18 06:20:002015亚冠之阿尔艾因
日期:2015-09-18 10:35:08月度论坛发贴之星
日期:2015-09-30 22:25:002015亚冠之阿尔沙巴布
日期:2015-10-03 08:57:39程序设计版块每日发帖之星
日期:2015-10-05 06:20:00每日论坛发贴之星
日期:2015-10-05 06:20:002015年亚冠纪念徽章
日期:2015-10-06 10:06:482015亚冠之塔什干棉农
日期:2015-10-19 19:43:35程序设计版块每日发帖之星
日期:2015-10-21 06:20:00每日论坛发贴之星
日期:2015-09-14 06:20:00
3 [报告]
发表于 2007-03-19 16:26 |只看该作者
原帖由 九霄 于 2007-3-19 14:14 发表
有个项目,要用ansi c读取目录下的文件名,但是有个要求,在linux下和vc里都要编译通过
我是满足了这头,满足不了那头,头疼呀


条件编译, 再没有什么更好的方法了. 读取目录是和OS相关的.

论坛徽章:
0
4 [报告]
发表于 2007-03-19 18:18 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
5 [报告]
发表于 2007-03-19 18:19 |只看该作者
LZ的公司要求真高。linux和windows都要行

论坛徽章:
0
6 [报告]
发表于 2007-03-19 18:48 |只看该作者
原帖由 九霄 于 2007-3-19 14:14 发表
有个项目,要用ansi c读取目录下的文件名,但是有个要求,在linux下和vc里都要编译通过
我是满足了这头,满足不了那头,头疼呀


ansi c?
如果可以用C++的话,你可以试试
boost::filesystem::path



  1. //  simple_ls program  -------------------------------------------------------//

  2. //  ?Copyright Jeff Garland and Beman Dawes, 2002

  3. //  Use, modification, and distribution is subject to the Boost Software
  4. //  License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  5. //  http://www.boost.org/LICENSE_1_0.txt)

  6. //  See http://www.boost.org/libs/filesystem for documentation.

  7. #include "boost/filesystem/operations.hpp"
  8. #include "boost/filesystem/path.hpp"
  9. #include <iostream>

  10. namespace fs = boost::filesystem;

  11. int main( int argc, char* argv[] )
  12. {

  13.   fs::path full_path( fs::initial_path() );

  14.   if ( argc > 1 )
  15.     full_path = fs::system_complete( fs::path( argv[1], fs::native ) );
  16.   else
  17.     std::cout << "\nusage:   simple_ls [path]" << std::endl;

  18.   unsigned long file_count = 0;
  19.   unsigned long dir_count = 0;
  20.   unsigned long err_count = 0;

  21.   if ( !fs::exists( full_path ) )
  22.   {
  23.     std::cout << "\nNot found: " << full_path.native_file_string() << std::endl;
  24.     return 1;
  25.   }

  26.   if ( fs::is_directory( full_path ) )
  27.   {
  28.     std::cout << "\nIn directory: "
  29.               << full_path.native_directory_string() << "\n\n";
  30.     fs::directory_iterator end_iter;
  31.     for ( fs::directory_iterator dir_itr( full_path );
  32.           dir_itr != end_iter;
  33.           ++dir_itr )
  34.     {
  35.       try
  36.       {
  37.         if ( fs::is_directory( *dir_itr ) )
  38.         {
  39.           ++dir_count;
  40.           std::cout << dir_itr->leaf()<< " [directory]\n";
  41.         }
  42.         else
  43.         {
  44.           ++file_count;
  45.           std::cout << dir_itr->leaf() << "\n";
  46.         }
  47.       }
  48.       catch ( const std::exception & ex )
  49.       {
  50.         ++err_count;
  51.         std::cout << dir_itr->leaf() << " " << ex.what() << std::endl;
  52.       }
  53.     }
  54.     std::cout << "\n" << file_count << " files\n"
  55.               << dir_count << " directories\n"
  56.               << err_count << " errors\n";
  57.   }
  58.   else // must be a file
  59.   {
  60.     std::cout << "\nFound: " << full_path.native_file_string() << "\n";   
  61.   }
  62.   return 0;
  63. }



复制代码

论坛徽章:
0
7 [报告]
发表于 2007-03-19 19:19 |只看该作者

回复 3楼 MMMIX 的帖子

MinGW 提供一个 dirent.o,你引用 dirent.h 并连接 dirent.o 试试看。这样可以不用过多地考虑 Win32 API 了。

论坛徽章:
0
8 [报告]
发表于 2007-03-19 23:59 |只看该作者
原帖由 soul_of_moon 于 2007-3-19 18:19 发表
LZ的公司要求真高。linux和windows都要行


是呀,头都大了,要在这两个环境都能编译通过,晕死了

论坛徽章:
0
9 [报告]
发表于 2007-03-19 23:59 |只看该作者
原帖由 westgarden 于 2007-3-19 18:48 发表


ansi c?
如果可以用C++的话,你可以试试
boost::filesystem::path


[code]
//  simple_ls program  -------------------------------------------------------//

//  ?Copyright Jeff Garland and ...


谢谢兄弟了,可惜不能用C++来实现,开发环境限制死了

论坛徽章:
0
10 [报告]
发表于 2007-03-20 00:02 |只看该作者
原帖由 langue 于 2007-3-19 19:19 发表
MinGW 提供一个 dirent.o,你引用 dirent.h 并连接 dirent.o 试试看。这样可以不用过多地考虑 Win32 API 了。


谢谢,明天上班时我研究下这个,希望能解决问题
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP