Thread: Can't creat as many files as need in a folder

  1. #1
    Registered User
    Join Date
    Jun 2019
    Posts
    8

    Can't creat as many files as need in a folder

    Hello friends,

    I hope there are some expert in Filestream programming with C.
    First of all, sorry for my english. I'm not native english speaker.

    I'm making some experiment with File streaming in C. In all the cases, until in the following one, the programs works as expected. But surprising, here not.

    What is the program about?

    I tried to write a program which creates as many files as needed in a folder. The name of the file does not change. Only the number of the file increase after every iteration by 1.

    The program works prefect and as supposed, until (at arround) 500 files are created. After this, the program continues working without any problem, but no one new file is created in the folder.

    I'm wondering why this happens. I took a look in Internet and it seems to me that the problem is not the OS. So I think it could be a problem with the buffer. But still cleaning the buffer does not helps.

    Here the code:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
      unsigned int i = 0, count = 1;
    
      // Max iteretions
      const unsigned int MAX = 520;
    
      unsigned char index_file_name = '0';
    
      // Datafile name and place
      char datafile_name[] = "many_files/datafile_name_00000.txt";
    
      FILE *file_pointer_datafile = NULL;
    
      //printf("Cantidad de vueltas> %i\n", MAX+1);
    #define INDEX 29
      printf("Program starts...\n");
    
      // Loop is created with goto
    while_GLOBAL:
    
      // The new Filename is changed increasing the number by one
      // So, first created file is named:
      // datafile_name_00001.txt
      // The next one
      // datafile_name_00002.txt
      // and so on.
      if (index_file_name != '0') {
    
        datafile_name[INDEX] = index_file_name;
      }
    
      else if (index_file_name == '0')
      {
        i = 0;
        while (datafile_name[INDEX - i] == '9') {
    
          datafile_name[INDEX - i] = '0';
    
          i++;
        }
        datafile_name[INDEX - i] += 1;
      }
      // Creats the realtion between filepointer and new name of file.
      file_pointer_datafile = fopen(datafile_name, "w+");
      //printf("%i\n", index_file_name-48);
    
      // Interrps the goto-loop when count is equal to MAX
      if (count <= MAX) {
    
        count++;
        index_file_name++;
        if (index_file_name > 57) {
    
          index_file_name = '0';
        }
        //printf("%i\n", count);
        goto while_GLOBAL;
    
      } else {
    
        printf("Programa finaliza...");
        goto while_FIN;
      }
      // Hear I am trying to clean any posible buffer
      fflush(stdin);
      fflush(stdout);
      fflush(file_pointer_datafile);
      // Relation between filepointer and file is closed
      fclose(file_pointer_datafile);
      goto while_GLOBAL;
    
    
      // End of program
    while_FIN:
    
      return 0;
    }
    What is happening? What I'm doing wrong?

    Thanks for any good idea!
    Gisi
    Last edited by Salem; 06-25-2019 at 10:58 PM. Reason: Removed fake indent tags

  2. #2
    Registered User
    Join Date
    May 2019
    Posts
    214
    Here's where 'goto' is not your friend

    Code:
    // Interrps the goto-loop when count is equal to MAX if(count<=MAX) {
    count++; index_file_name++; if(index_file_name>57) {
    index_file_name='0';
    }
    //printf("%i\n", count); goto while_GLOBAL;
    }

    Where you "goto while_GLOBAL", you're not closing the file.

    Your process is running out of file handles.

    I'd suggest something like:


    Code:
    while( count <= MAX )
    {
     // do whatever creates index_file_name
    
    
     // Creats the realtion between filepointer and new name of file. 
     file_pointer_datafile=fopen(datafile_name, "w+");
    
     count++; 
     index_file_name++; 
    
     if(index_file_name>57) 
     {
      index_file_name='0';
     }
    
    
     // Hear I am trying to clean any posible buffer
     fflush(stdin);
     fflush(stdout);
     fflush(file_pointer_datafile);
     // Relation between filepointer and file is closed 
     fclose(file_pointer_datafile);
    
    }
    
    // output finished, close program
    Last edited by Niccolo; 06-25-2019 at 04:35 PM.

  3. #3
    Registered User
    Join Date
    Jun 2019
    Posts
    8

    Smile

    Thank you! You give me the way to find the solution! It was not much to modify.

    You are right, goto is really not easy to handle.
    But in this case, I need to use goto. goto is much far away faster than while, for or do-while.
    So I need to find a solution with goto...

    Here the code with the correction you helped me to do:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        unsigned int
            i=0, count=1;
    
        // Max iteretions
        const unsigned int MAX=520;
    
        unsigned char
            index_file_name='0';
    
        // Datafile name and place
        char  datafile_name[]="many_files/datafile_name_00000.txt";
    
        FILE* file_pointer_datafile=NULL;
    
            //printf("Cantidad de vueltas> %i\n", MAX+1);
        #define INDEX 29
            printf("Program starts...\n");
    
            // Loop is created with goto
            while_GLOBAL:
    
            // The new Filename is changed increasing the number by one
            // So, first created file is named:
            // datafile_name_00001.txt
            // The next one
            // datafile_name_00002.txt
            // and so on.
            if(index_file_name!='0')
            {
                datafile_name[INDEX]=index_file_name;
            }
            else if(index_file_name=='0')
            {
                i=0;
                while(datafile_name[INDEX-i]=='9')
                {
                    datafile_name[INDEX-i]='0';
                    i++;
                }
                datafile_name[INDEX-i]+=1;
            }
    
            // Creats the realtion between filepointer and new name of file.
            file_pointer_datafile=fopen(datafile_name, "w+");
            //printf("%i\n", index_file_name-48);
    
            // Interrps the goto-loop when count is equal to MAX
            if(count<=MAX)
            {
                count++;
                index_file_name++;
                if(index_file_name>57)
                {
                    index_file_name='0';
                }
                //printf("%i\n", count);
                fflush(stdin);
                fflush(stdout);
                fflush(file_pointer_datafile);
                // Relation between filepointer and file is closed
                fclose(file_pointer_datafile);
                goto while_GLOBAL;
            }
            else
            {
                printf("Programa finaliza...");
                goto while_FIN;
            }
            // Hear I am trying to clean any posible buffer
    
            goto while_GLOBAL;
    
            // End of prgram
    while_FIN:
    
                fflush(stdin);
                fflush(stdout);
                fflush(file_pointer_datafile);
                // Relation between filepointer and file is closed
                fclose(file_pointer_datafile);
    
    return 0;
    
    }
    Thank you very much!!
    Gisi
    Last edited by GisiNA; 06-26-2019 at 02:13 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > But in this case, I need to use goto. goto is much far away faster than while, for or do-while.
    Rubbish.

    It wasn't faster because you spent HOURS chasing a problem that had no business being created.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jun 2019
    Posts
    8
    Sorry, I think I didn't express my idea well enough. I didn't mean that goto is faster by creating a program.

    What I mean is that a program written with goto instead of while, for or do-while, is much faster.
    In my case it is not important how long I need to write a program with goto.
    The resulting program itself, not the time it is need to create it, has to be so fast as possible.
    This is the reason why I needed to find a solution with goto and not with while, for or do-while.

    I hope, I could explain my idea this time better!

    Thank you again!
    Last edited by GisiNA; 06-26-2019 at 02:48 AM.

  6. #6
    Registered User
    Join Date
    May 2019
    Posts
    214
    After 40+ years of writing in C and C++, I can tell you that you've been given incorrect information about the speed of goto vs while, for or do-while. Factually, there is either no difference at all whatsoever, or there is only that difference you might think you can measure. When you examine the assembler produced by both results, you'll realize it CAN'T be any faster. There's just nothing "there" to be faster.

    Seriously, examine the matter from an engineer's perspective and test it under stress. Look at the underyling assembler.

    Aside from that, it can't impact the performance of this code because by far the time consuming component is the system calls to create the file, not the loop.

  7. #7
    Registered User
    Join Date
    Jun 2019
    Posts
    8
    I think this discussion not part of the problem I asked for, so I said good by and thanks again! :-D

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > The resulting program itself, not the time it is need to create it, has to be so fast as possible.
    The moment you opened a file, all sense of "+/- a few nanoseconds" went out the window.

    You're instantly comparing the nanoseconds of an instruction with the milliseconds it takes to move the head of your hard disk.
    That's easily in the order of 1E6 to 1E7 times slower.

    Besides, if you really cared about 'goto is better than while', you would have applied it to your INNERMOST loop first.
    > while(datafile_name[INDEX-i]=='9')
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    If it needs to be that fast, you should probably consider writing it in assembly

    As a side note...
    Why fflush(stdin) is wrong - Cprogramming.com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C get files in sub-folder
    By Ricardo Sousa in forum C Programming
    Replies: 1
    Last Post: 09-12-2013, 09:07 AM
  2. Replies: 2
    Last Post: 11-30-2012, 03:25 PM
  3. Replies: 4
    Last Post: 03-29-2012, 02:55 PM
  4. How to creat header files
    By Shidlingayya in forum C Programming
    Replies: 1
    Last Post: 08-09-2007, 07:53 AM
  5. get files in a folder and more
    By appleGuy in forum Windows Programming
    Replies: 3
    Last Post: 09-05-2006, 08:59 AM

Tags for this Thread