Like Tree1Likes
  • 1 Post By Tclausex

Array of file names (such that you call fopen with array[i])

This is a discussion on Array of file names (such that you call fopen with array[i]) within the C Programming forums, part of the General Programming Boards category; Hello, I am trying to create an array of file names such that when I want to open one of ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    17

    Array of file names (such that you call fopen with array[i])

    Hello,

    I am trying to create an array of file names such that when I want to open one of the files, who's name is given in the array, I can call that element of the array.

    My code for making the array is the following,

    Code:
    char file_name[40];
    char *file_locations[N];         // array of N names (*) 
    
    
    for(ii=0; ii<=N-1; ii++){        // printing the names of all the files P1_8mag_1.txt - P1_8mag_N.txt 
        sprintf(file_name, "P1_8mag_%i.txt", ii+1);
        file_locations[ii] = file_name;
    }

    then to open a given file I run

    Code:
    FILE*fp1=fopen(file_locations[i], "r");     // open the file

    The problem with this is that in the first step: all elements of the array "file_locations" are the name of the last file in the loop (in this case "P1_8mag_N.txt")

    What is wrong with this?

    Ps thanks for all of you comments, I am new to C and programming.

    BN
    Last edited by bnickerson; 02-09-2013 at 05:10 PM.

  2. #2
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    You have created a 1D array of char pointers and then proceeded to point all of them to file_name, which of course is the last string you created with sprintf.

    You can either create a 2D array of N x 40 or you can malloc() the memory for each string created and point each element to their respective malloc'd string.

    If you do the former, you'll need to use the function strcpy() and not the assignment operator =.
    bnickerson likes this.

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    17
    Thank you Tclausex,

    Is there any benefit to using one or the other? Or is it simply preference?

    Code:
    char file_name[40];
    char file_locations[N][40];         // array of 16 pointers (*) 
    
    
    for(ii=0; ii<=N-1; ii++){        // printing the names of all the files P1_8mag_1.txt - P1_8mag_16.txt 
        sprintf(file_name, "P1_8mag_%i.txt", ii+1);
        strcpy(file_locations[ii], file_name);
    }
    gives error : warning: incompatible implicit declaration of built-in function ‘strcpy’
    BN
    Last edited by bnickerson; 02-09-2013 at 05:38 PM.

  4. #4
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    malloc would only allocate enough memory needed for each string, but your strings seem to be within a few characters of the same length, so not much benefit really. Unless your N is very large and you might not use all N rows.

  5. #5
    Registered User
    Join Date
    Feb 2013
    Posts
    17
    Sorry there is no error, I forgot to inlude
    Code:
    #include<string.h>
    Thanks again for you help

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,636
    Quote Originally Posted by bnickerson View Post
    Code:
    char file_locations[N][40];         // array of 16 pointers (*)
    That comment is completely wrong. file_locations is an array of arrays, not an array of pointers.

    Pointers and arrays are very very different things. Potential confusion comes about because, in some circumstances, they can be used interchangeably. But that does not make them the same thing.

    If your teacher is telling you pointers and arrays are the same thing, get a new teacher. If your textbook is telling you they are the same thing, burn it.

    If you are jumping to the conclusion that "can be used interchangeably in some circumstances" means "a pointer is an array", rethink.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  7. #7
    Registered User
    Join Date
    Feb 2013
    Posts
    17
    Quote Originally Posted by grumpy View Post
    That comment is completely wrong. file_locations is an array of arrays, not an array of pointers.

    Pointers and arrays are very very different things. Potential confusion comes about because, in some circumstances, they can be used interchangeably. But that does not make them the same thing.

    If your teacher is telling you pointers and arrays are the same thing, get a new teacher. If your textbook is telling you they are the same thing, burn it.

    If you are jumping to the conclusion that "can be used interchangeably in some circumstances" means "a pointer is an array", rethink.

    I understand that they are not the same. However, is there any error in the code I have posted? It gives me what I want namely an array "file_locations" consisting of elements

    Code:
    P1_8mag_1.txt
    P1_8mag_2.txt
    P1_8mag_3.txt
    P1_8mag_4.txt
    .
    .
    .
    P1_8mag_N.txt
    which can then be used to open each of the locations

    Code:
    FILE*fp=fopen(file_locations[i], "r");     // open the file
    .
    .
    .
    .
    fclose(fp);
    Last edited by bnickerson; 02-09-2013 at 08:40 PM.

  8. #8
    Registered User hex_dump's Avatar
    Join Date
    Dec 2012
    Posts
    88
    Quote Originally Posted by bnickerson View Post
    I understand that they are not the same. However, is there any error in the code I have posted? It gives me what I want namely an array "file_locations" consisting of elements

    Code:
    P1_8mag_1.txt
    P1_8mag_2.txt
    P1_8mag_3.txt
    P1_8mag_4.txt
    .
    .
    .
    P1_8mag_N.txt
    which can then be used to open each of the locations

    Code:
    FILE*fp=fopen(file_locations[i], "r");     // open the file
    .
    .
    .
    .
    fclose(fp);
    Yup, file_locations is an array of arrays NOT and array of pointer to character. If you understood that, remove the comment from your code. It'll help with future misunderstandings.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,824
    Before you rush off and create an array of 1000's of filenames, you should be aware that most operating systems restrict the number of open files per process.
    Eg.
    c++ - Is there a limit on number of open files in Windows - Stack Overflow
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Registered User
    Join Date
    Feb 2013
    Posts
    17
    Quote Originally Posted by Salem View Post
    Before you rush off and create an array of 1000's of filenames, you should be aware that most operating systems restrict the number of open files per process.
    Eg.
    c++ - Is there a limit on number of open files in Windows - Stack Overflow
    I read a bit about this on another post and that is why I wanted to save all of the file names to an array. In doing so I can then easily open and close each file in a loop while editing them.

    Thank you everyone for your comments!

    P.s. How do I edit my post? Indeed grumpy and hex_dump pointed out that I do not fully understand the relation between pointers and arrays and I would like to remove the related comment. However, the edit button is no longer showing.

  11. #11
    Registered User
    Join Date
    Nov 2012
    Posts
    1,220
    Quote Originally Posted by bnickerson View Post
    Hello,

    I am trying to create an array of file names such that when I want to open one of the files, who's name is given in the array, I can call that element of the array.
    Code:
    char file_name[40];
    char *file_locations[N];         // array of N names (*) 
    
    for(ii=0; ii<=N-1; ii++){        // printing the names of all the files P1_8mag_1.txt - P1_8mag_N.txt 
        sprintf(file_name, "P1_8mag_%i.txt", ii+1);
        file_locations[ii] = file_name;
    }
    The problem with this is that in the first step: all elements of the array "file_locations" are the name of the last file in the loop (in this case "P1_8mag_N.txt")

    What is wrong with this?
    The problem here is that every time through the loop, you are assigning the same address (file_name) to the array. A simple way to solve this is strdup.

    Code:
    for(int ii=0; ii<=N-1; ii++){        // printing the names of all the files P1_8mag_1.txt - P1_8mag_N.txt 
    	sprintf(file_name, "P1_8mag_%i.txt", ii+1);
    	if ((file_locations[ii] = strdup(file_name)) == NULL) {
    		printf("not enough memory!\n");
    		return 1;
    	}
    }
    
    // ...
    
    // all done: clean up each i in file_locations[i]
    for (int i=0; i < N; i++)
    	free(file_locations[i]);
    strdup means will duplicate the string data in your temporary buffer file_name into a new location before assigning it into your file_locations[i] array member. If there is not enough memory, NULL is returned and the program aborts with an error message (unlikely but necessary to check for it)

  12. #12
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by bnickerson View Post
    P.s. How do I edit my post? Indeed grumpy and hex_dump pointed out that I do not fully understand the relation between pointers and arrays and I would like to remove the related comment. However, the edit button is no longer showing.
    After some time (AFAIK 1 hour) you can't edit you post anymore.

    Bye, Andreas

  13. #13
    Registered User hex_dump's Avatar
    Join Date
    Dec 2012
    Posts
    88
    There's no need to edit your post. Just take it out of your code and be done with it. You can post your new code when you're ready.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fopen - Inputting a file to fill an array.
    By Alan Gott in forum C Programming
    Replies: 4
    Last Post: 11-07-2011, 02:57 PM
  2. Replies: 3
    Last Post: 12-10-2010, 08:51 AM
  3. 2D array of names
    By Jasper in forum C Programming
    Replies: 4
    Last Post: 08-27-2009, 06:09 AM
  4. Noob fopen Array help!!!
    By elementalwinds in forum C Programming
    Replies: 8
    Last Post: 10-19-2006, 10:54 PM
  5. Variable file names with fopen function?
    By Chickenhawk in forum C Programming
    Replies: 5
    Last Post: 07-26-2006, 05:38 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21