Reading multiple files from a directory?

I am having difficulty opening files from a directory that is located in a different folder than the EXE file. I was able to read one file. But how to read several files present in the directory in a loop using the program.

The code used to process a single file is shown 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;
}

This way I can process the file to parse the variables in the file and get the length of the lines. The problem is how to open multiple files, I have file names in the folder language, like:

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 a way to give the folder path in D: /folder/languagegeneral?

+6
source share
4 answers

, 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");
0

, .

- , .

0

, say processFile (char * _), , .

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

, .

0

simonc , .h argv, , , .h .

I assume you want to iterate over all .h into a directory . There are libraries that allow you to execute this cross-platform (search for these keywords), or you can do it on your OS:

  • Linux: → dirent

  • Windows: FindFirstFile FindNextFile → msdn

    (or a little hacky, but maybe a lot easier for you: first use system () for dir / ls * .h in the .txt file, then read this)

0
source

All Articles