gpt4 book ai didi

c++ - 根据结尾类型对文件路径文件进行排序(超过 7000 行)

转载 作者:太空宇宙 更新时间:2023-11-04 13:45:37 27 4
gpt4 key购买 nike

我正在尝试对一个文件(超过 7000 行)进行排序,其中每一行都是来 self ssh 进入的服务器的文件路径,并将每个文件路径放入一个按字母顺序排序的文本文件中,并取决于结尾类型(.png、.jpg、.php、.html、.doc 等),并将这些文件路径放在它们自己单独的文本文件中(用于组织目的)。

文件中的一些示例行:

./public_html/application/libraries/phpass-0.1/c/crypt_private.c
./public_html/creativity/archive/oldsite/curricular/revised ArtScience.10.1.doc
./public_html/chambers/Chambers Fund Guidelines9-1-2010 .pdf
./public_html/js/jquery-ui/development-bundle/demos/autocomplete/images/ui-anim_basic_16x16.gif
./tmp/webalizer/ssl/entrepreneurship.wfu.edu/hourly_usage_201112.png
./public_html/js/jquery-ui/development-bundle/demos/droppable/images/high_tatras2.jpg
./public_html/js/jquery-ui/development-bundle/demos/autocomplete/categories.html

我在上面提供的行仅代表我必须整理的不同类型文件中的一小部分。其中一些,在查看文件后要么有多个结尾:

./public_html/creativity/archive/oldsite/home_images/_notes/home_nav_bottom.jpg.mno

或者根本没有结尾:

./public_html/old/mambots/editors/tinymce/jscripts/tiny_mce/plugins/insertdatetime

在考虑了我将如何在 C++ 中实现它之后,这是我将要做的事情的粗略轮廓(伪代码):

int main()
{
/*have all necessary includes and namespaces*/
/*initialize variables and do file opening*/

while(/*we are not at end of file*/)
{
switch(/*by the type of file ending*/)
{
case .png:
/*store it in a separate file just for .png lines*/
break;
case .jpg
/*store it in a separate file just for .jpg lines*/
break;
/*have more cases to handle the rest of the type of endings*/
case default:
break;
}

}

/*close file*/
return 0;
}

我的问题如下:

  1. 我如何在文件中逐行检查是否已达到 .jpg、.png、.php 等结尾?

  2. 如何在我的 switch 语句中考虑所有可能的文件结尾(尽管我已经浏览了整个文件,但我不确定有多少不同的结尾)?

  3. 如何处理文件路径可能有多个结尾的情况(如我上面提供的示例)?

  4. 当然,如果有更好的方法使用 C++(也许是另一种语言可以使这更容易?),我会洗耳恭听。

最佳答案

为什么不使用文件扩展名作为文件名的一部分,以确保不同文件类型的文件分开?

有点像这样:

int main()
{
/*have all necessary includes and namespaces*/
/*initialize variables and do file opening*/

while(/*we successfully read a line from the file*/)
{
/* extract the file extension from end of line*/

/* create a file name incorporating the file extension (table lookup?) */

/* Append the line to the file of that file name */
}

/*close file*/
return 0;
}

所以你的文件名可能是这样的:

list-of-jpg.txt
list-of-mpeg.txt
list-of-html.txt
etc...

注意事项:

可以像这样从行中提取文件扩展名:

std::string ext;
std::string::size_type pos = line.rfind('.');
if(pos != std::string::npos)
ext = line.substr(pos + 1);

当一个文件有多个结尾时,通常是最后一个应用。例如,扩展名为 .tar.gz 的文件是创建为 tar 但后来被 gzipped 的文件。所以它现在是一个 gzip gz。所以我会相信最后的延期。它可能是从以前的扩展名格式转换而来的文件的真实格式。

关于c++ - 根据结尾类型对文件路径文件进行排序(超过 7000 行),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26130593/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com