简体   繁体   English

从目录中读取多个文件?

[英]Reading multiple files from a directory?

I am having difficulty to open files from a directory which is in different folder than the exe. 我很难从与exe不同的文件夹中的目录打开文件。 I have managed to read one file. 我设法读了一个文件。 But how to read multiple files present in the directory in loop using program. 但是如何使用程序在循环中读取目录中存在的多个文件。

The code used for the single file processing is below: 用于单个文件处理的代码如下:

#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;
}

So I'm able to process file to parse the variables in the file and get the lengths of the strings. 所以我能够处理文件来解析文件中的变量并获得字符串的长度。 Problem is how to open multiple files I have file names in the folder language as: 问题是如何打开多个文件,我在文件夹语言中有文件名:

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

How can I open files of these names in a loop? 如何在循环中打开这些名称的文件?

Also is there any way to give the path of the folder D:/folder/language in general way? 还有什么方法可以给出文件夹D:/folder/language的路径一般吗?

You could pass the path as a command line argument into your program, reading its value from argv[1] if it is the first argument, then loop through the different files you want to read: 您可以将路径作为命令行参数传递到程序中,如果它是第一个参数,则从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");

I recommend to start by putting the actual parsing of the file into a separate function, so you can call that function with only the file name. 我建议首先将文件的实际解析放入一个单独的函数中,这样就可以只使用文件名调用该函数。

The the simplest thing would be to have a table with the file names, and loop through this table giving the filenames to the function you just created. 最简单的方法是使用一个包含文件名的表,然后遍历此表,将文件名赋予您刚刚创建的函数。

You could rewrite the program by putting functionality inside a function say processFile(char *fileName) and then call the function with full path filenames eg 您可以通过将函数放入函数中来重写程序,例如processFile(char * fileName),然后使用完整路径文件名调用函数,例如

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

Also you could put file names to process in a file and rewrite the program to loop over the lines in that file and call the function with filenames it finds. 您还可以将文件名放在文件中处理,并重写程序以循环该文件中的行,并使用它找到的文件名调用该函数。

As simonc has said you can give the path to the .h's via argv , but the problem is that this still requires that you know the names of the .h's at compile-time. 正如simonc所说,你可以通过argv给出.h的路径,但问题是这仍然要求你在编译时知道.h的名字。

I assume that you want to loop over all .h's in a directory . 我假设您想循环遍历目录中的所有.h There are libraries that let you do this cross-platform (search for these keywords), or you can do it dep on your OS: 有些库允许您跨平台(搜索这些关键字),或者您可以在您的操作系统上执行此操作:

  • Linux: -> dirent Linux: - > dirent

  • Windows: FindFirstFile FindNextFile -> msdn Windows:FindFirstFile FindNextFile - > msdn

    (or, a little hackish but maybe a lot easier for you: first use system() to dir/ls *.h into a .txt file, then read that) (或者,有点hackish但是对你来说可能更容易:首先使用system()将dir / ls * .h转换为.txt文件,然后阅读)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM