【问题标题】:Reading multiple files from a directory?从目录中读取多个文件?
【发布时间】:2013-02-08 10:30:57
【问题描述】:

我无法从与 exe 位于不同文件夹的目录中打开文件。我设法读取了一个文件。但是如何使用程序循环读取目录中存在的多个文件。

用于单个文件处理的代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    FILE *fp, *tp, *tl;
    char str_buff[1024] = { FALSE };
    char str[125];
    char strlengths[MAX_NO_OF_STRINGS]= { FALSE };
    //int Result;
    //int string_startflag = FALSE;
    int string_cntr = FALSE,i = 0, n = 0;

    fp = fopen("D:/folder/language/stringEnglish.h", "r");
    tp = fopen("New Text Document.txt", "w"); // open the file to Write
    tl = fopen("New Length Document.txt", "w"); // open the file to Write lengths

    while (NULL != fgets(str_buff, sizeof(str_buff), fp))
    {
        sscanf(str_buff, "%*[^\"]%*c%[^\"]%*c%*[^\n]%*c", str);

        //printf("%s\n", str);

        if (string_cntr > 6)
        {
            if (string_cntr<= MAX_NO_OF_STRINGS)
            {
                fprintf(tp, "%s\n", str);
                strlengths[i] = strlen(str);
                i++;
            }
        }
        string_cntr++;
    }

    for(n=0;n<(MAX_NO_OF_STRINGS-6);n++)
    {
        fprintf(tl,"%d\n",strlengths[n]);
    }

    fclose(fp);
    fclose(tp);
    fclose(tl);

    return 0;
}

所以我能够处理文件以解析文件中的变量并获取字符串的长度。问题是如何打开多个文件我的文件夹语言中的文件名如下:

stringItalian.h,stringLatvian.h,stringSlovakian.h,stringSlovenian.h,stringSpanish.h,stringSwedish.h,stringTurkish.h,stringUkrainian.h

如何循环打开这些名称的文件?

还有什么方法可以给出文件夹D:/folder/language的路径吗?

【问题讨论】:

    标签: c


    【解决方案1】:

    您可以将路径作为命令行参数传递到您的程序中,如果它是第一个参数,则从 argv[1] 读取其值,然后遍历您要读取的不同文件:

    int main(int argc, char* argv[])
    {
        ...
        const char* files[] = {"stringItalian.h", "stringLatvian.h",
                               "stringSlovakian.h", "stringSlovenian.h",
                               "stringSpanish.h", "stringSwedish.h",
                               "stringTurkish.h", "stringUkrainian.h"};
        int i;
        char fullpath[256];
    
        for (i=0; i<sizeof(files)/sizeof(files[0]); i++) {
            strcpy(fullpath, argv[1]);
            strcat(fullpath, files[i]);
            fp = fopen(fullpath, "r");
    

    【讨论】:

    • 谢谢你,这肯定会帮助我。会试试这个方法
    【解决方案2】:

    我建议首先将文件的实际解析放入一个单独的函数中,这样您就可以只使用文件名调用该函数。

    最简单的方法是创建一个包含文件名的表,然后循环遍历该表,将文件名提供给您刚刚创建的函数。

    【讨论】:

    • 感谢您使用函数进行解析。simonc 提供了一个很好的示例将使用函数合并它
    【解决方案3】:

    您可以通过将功能放入函数中来重写程序,例如 processFile(char *fileName),然后使用完整路径文件名调用该函数,例如

    processFile("D:/folder/language/stringEnglish.h");
    processFile("D:/folder/language/stringItalian.h");
    

    您还可以将要处理的文件名放入文件中,然后重写程序以遍历该文件中的行并使用它找到的文件名调用函数。

    【讨论】:

      【解决方案4】:

      正如 simonc 所说,您可以通过 argv 提供 .h 的路径,但问题是这仍然需要您在编译时知道 .h 的名称。

      我假设您想遍历目录中的所有 .h。有一些库可以让您跨平台执行此操作(搜索这些关键字),或者您可以在您的操作系统上执行此操作:

      • Linux:-> dirent

      • Windows:FindFirstFile FindNextFile -> msdn

        (或者,有点老套,但对你来说可能更容易:首先使用 system() 将 dir/ls *.h 放入 .txt 文件,然后读取)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-12-02
        • 2014-07-23
        • 2013-09-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-03-13
        相关资源
        最近更新 更多