Thread: Creating files in a specific directory

  1. #1
    Registered User
    Join Date
    Jul 2014
    Posts
    15

    Creating files in a specific directory

    Hello again, I am trying to generate 7 files in an already created directory. My code compiles and runs, but when I check inside the directory, nothing is there. Can anyone offer any suggestions? Edit: I should also mention that if I create these without trying to specify a directory, they are all created in the program directory exactly how I would expect.

    Code:
    void generateRooms(char *roomsDirectory) {
        int bufSize = 20;
        char *currentFile = malloc(bufSize);
        char *prefix = "room_";
    
        int i;
        for(i = 0; i < 7; i++) {
            snprintf(currentFile, bufSize, "%s/%s%d", roomsDirectory, prefix, i+1);
            FILE *f = fopen(currentFile, "w");
        }
    }
    Last edited by hinesro; 01-31-2015 at 07:48 PM.

  2. #2
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    You don't check if the openings were successful, and you don't close the files after you open them.

  3. #3
    Registered User
    Join Date
    Jul 2014
    Posts
    15
    Simply opening them should create the files if they don't yet exist, no? Anyway, checking for success shows that it is indeed failing to open.

    Code:
    void generateRooms(char *roomsDirectory) {
        int bufSize = 20;
        char *currentFile = malloc(bufSize);
        char *prefix = "room_";
    
        int i;
        for(i = 0; i < 7; i++) {
            snprintf(currentFile, bufSize, "%s/%s%d", roomsDirectory, prefix, i+1);
            FILE *f = fopen(currentFile, "w");
            if (f == NULL) {
                printf("Error: Failed to open %s.\n", currentFile);
                exit(1);
            }
            fclose(f);
        } 
    }
    It seems like it really shouldn't be that hard to just create a directory (which has been done), and then create some files in that directory. Any helpful guidance would really be appreciated.
    Last edited by hinesro; 01-31-2015 at 11:55 PM.

  4. #4
    Registered User
    Join Date
    Jul 2014
    Posts
    15
    Solved, buffer wasn't big enough for the entire path.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    So why are you ignoring the advice in your previous thread, regarding buffer lengths?
    You persist in using malloc to allocate small buffers which are way too easy to overflow by accident, and then you leak the memory as well.

    fopen() will not create a directory for you.

    roomsDirectory MUST already exist, and the current directory MUST be the parent directory of roomsDirectory.
    If either of these isn't true, the program will fail.
    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.

  6. #6
    Registered User
    Join Date
    Jul 2014
    Posts
    15
    Thanks.
    Last edited by hinesro; 02-01-2015 at 01:57 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to extract a list of files in a specific directory?
    By Perspektyva in forum C++ Programming
    Replies: 4
    Last Post: 11-11-2008, 02:02 PM
  2. Listing specific files in a directory
    By knirirr in forum C Programming
    Replies: 14
    Last Post: 01-29-2008, 05:42 AM
  3. Replies: 3
    Last Post: 11-22-2005, 10:29 AM
  4. Creating files in specific directories
    By Kyoto Oshiro in forum C++ Programming
    Replies: 2
    Last Post: 03-06-2002, 08:50 PM