0

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);
}
raquela
  • 268
  • 2
  • 8

1 Answers1

2

In your readdir loop, the name of the "entry" (entry->d_name) is the relative name to the directory it's in. So if you have a file /input/foo then entry->d_name will be "foo" only.

You should really check what lstat returns. I'll bet it's -1 with errno set to ENOENT (which means statbuf is uninitialized and all members will have indeterminate values).

To get lstat to not fail, you either need to chdir into the directory, or you should have a temporary string where you concatenate the directory and file names. Something you do later in the code (though I would suggest using snprintf instead of three string function calls).


There are also a few other problems that looks weird. For example fileName is an array, which means it will never be NULL.

Also please read this discussion about casting the result of malloc. Not that you need to allocate dynamically, since you have a size fixed at time of compilation which means you could use arrays for x, y and z.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • I think that I do not understand how I can correct my code. After adding a line chdir(katalogIn); there is an error "Cannot open the file". And about weird things in my code: I'm learning C by myself, so I know that many things can be done in a better way, but on one file it was working correctly. – raquela Jun 19 '17 at 12:58
  • @raquela That's because you *later* in the code use the whole "appending directory and file name" thing. Since you do that, *don't* do the `chdir` and instead move the "appending directory and file name" thing to before the call to `lstat`. Like `snprintf(fileName, sizeof(fileName), "%s/%s", katalogIn, entry->d_name); int res = lstat(fileName); ...`. – Some programmer dude Jun 19 '17 at 13:00