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 string_cntr = FALSE,i = 0, n = 0;
fp = fopen("D:/folder/language/stringEnglish.h", "r");
tp = fopen("New Text Document.txt", "w");
tl = fopen("New Length Document.txt", "w");
while (NULL != fgets(str_buff, sizeof(str_buff), fp))
{
sscanf(str_buff, "%*[^\"]%*c%[^\"]%*c%*[^\n]%*c", 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?
source
share