I wrote a code that calculate something. As I have many files, I am trying to rewrite the code, that it can read a files from a folder, calculate some function and give an output in another folder. I found some examples how I can make it, but something is wrong. As an output I did not received every file (ex. I have 5 input files in a folder, but I received only 4 files with results). What I am doing wrong? Here is my main part of the code:
int main()
{
int my_size = 100;
char katalogIn[512];
strcpy(katalogIn, "input");
char katalogOut[512];
strcpy(katalogOut, "output");
DIR *dp;
struct dirent *entry;
struct stat statbuf;
if ((dp = opendir(katalogIn)) == NULL) {
fprintf(stderr, "Cannot open directory: %s\n", katalogIn);
return 1;
}
int fileCounter = 0;
char fileName[512];
while ((entry = readdir(dp)) != NULL) {
lstat(entry->d_name, &statbuf);
fileCounter++;
if (S_ISDIR(statbuf.st_mode)) {
//printf("Done");
} else {
if (fileName != NULL)
{
FILE *fileData1;
strcpy(fileName, katalogIn);
strcat(fileName, "/");
strcat(fileName, entry->d_name);
fileData1 = fopen(fileName, "r");
if (fileData1 == NULL) {
printf("Cannot open the file\n");
return -1;
}
char buffor[512];
float *x = (float*)malloc(my_size * sizeof(float));
float *y = (float*)malloc(my_size * sizeof(float));
float *z = (float*)malloc(my_size * sizeof(float));
if (x != NULL && y != NULL && z != NULL)
{
int i;
fseek(fileData1, 0, 0);
for (i = 0; i < my_size; i++)
{
if (fscanf(fileData1, "%f\t%f\t%f", x + i, y + i, z +i) != 3)
{
break;
}
}
fclose(fileData1);
FILE *fileOutput1;
strcpy(fileName, katalogOut);
strcat(fileName, "/");
strcat(fileName, entry->d_name);
//strcat(fileName,"out");
fileOutput1 = fopen(fileName, "w");
if (fileOutput1 == NULL) {
printf("Cannot open the file\n");
return -1;
}
for (i = 0; i < my_size; i++)
{
float convX, convY;
int successCode = my_func(x[i], y[i], &convX, &convY);
fprintf(fileOutput1,"%.2f\t%.2f\t%.2f\n", convX, convY,z[i]);
}
free(x);
free(y);
free(z);
fclose(fileOutput1);
}
}
}}
closedir(dp);
return (EXIT_SUCCESS);
}