Thread: File *f[i] array?

  1. #1
    Wanabe Laser Engineer chico1st's Avatar
    Join Date
    Jul 2007
    Posts
    168

    File *f[i] array?

    how do i make a FILE pointer array in C?

    i tried this but it didnt work, i ment to post this a while ago but i think i got distracted.

  2. #2
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    you can only have one file open at a time.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    kryptkat> you can only have one file open at a time.
    Where did you hear that?

    chico1st> i tried this but it didnt work
    If you show the code you tried, we could help you determine the problem.

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Yeah, that one file opened at a time thing is totally wrong. You can open as many files as the underlying system permits you to, which is usually way more than just one.

    As for how to make the array, make it like you would any other array.

  5. #5
    Registered User
    Join Date
    Jan 2007
    Posts
    330
    You can have FOPEN_MAX files open, check your compiler to see how big that is.

    for instance, fopen on my system is implemented like this:

    Code:
    FILE *fopen (const char *filename, const char *opentype)
    {
        register int filenumber;
    
        for (filenumber=START_FILE; filenumber<FOPEN_MAX; filenumber++)
            if ((&_iob[filenumber])->_flag == 0)	/* look for free ele. in _iob */
    	    return _fopen (filename, opentype, &_iob[filenumber]);
        return (NULL) ;			/* No free elements in _iob structure */
    }
    It just looks through its internal

    FILE _iob[FOPEN_MAX] array to find the first unopened file. If all are opened NULL is returned

  6. #6
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    Yeah , for example mine can open 256 .

    isnt it too much =) ?

  7. #7
    Wanabe Laser Engineer chico1st's Avatar
    Join Date
    Jul 2007
    Posts
    168
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void main(){
    	FILE *xyz;
    	int i;
    	int INCREMENT = 5;
    
    	xyz = malloc(sizeof(FILE) * INCREMENT);
    	for (i = 0; i < INCREMENT; i++) {
    		xyz[i] = malloc(sizeof(FILE));
    	}
    
    }
    here are the errors
    C:\Files.c(11) : error C2115: '=' : incompatible types
    Error executing cl.exe.
    Last edited by chico1st; 08-20-2007 at 09:17 AM.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    main returns an int - always.

    You can't malloc a number of FILE's because FILE is an incomplete type. That is, you're only ever allowed to POINT to a FILE.

    Further, the only functions which can initialise a FILE* pointer for you are
    fopen
    freopen
    popen

    Perhaps start with
    FILE *myFiles[5];
    myFiles[0] = fopen("foo.txt", "r" );

    Then graduate to
    FILE **myFiles = malloc ( 5 * sizeof *myFiles );
    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
    Wanabe Laser Engineer chico1st's Avatar
    Join Date
    Jul 2007
    Posts
    168
    ok i tried this:

    Code:
    int main(){
    	FILE *f[20];
    	FILE *fbin;
    	char *FileName[1024];
    	char *Holder[1024];
    	int i;
    	int NumberOfFiles = 20;
    
    	for (i=0; i<NumberOfFiles; i++){
    		strcpy(FileName,"Channel");
    		itoa(i, Holder, 10);
    		strcat(FileName, Holder);
    		strcat(FileName, ".txt");
    		f[i] = fopen(FileName,"r+");
    	}
    
    
    }
    but i get these warnings... what is going on?
    C:\Files.c(14) : warning C4047: 'function' : 'char *' differs in levels of indirection from 'char *[1024]'
    C:\Files.c(14) : warning C4024: 'strcpy' : different types for formal and actual parameter 1

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    Your inability to read error messages, and generally declare arrays for storing chars.

    Drop the * from your two char arrays.

    Simply spraying "*" all over the place because you think there is a pointer involved somewhere isn't the way to go.
    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.

  11. #11
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Quote Originally Posted by chico1st View Post
    ok i tried this:

    Code:
    int main(){
    	FILE *f[20];
    	FILE *fbin;
    	char *FileName[1024];
    	char *Holder[1024];
    	int i;
    	int NumberOfFiles = 20;
    
    	for (i=0; i<NumberOfFiles; i++){
    		strcpy(FileName,"Channel");
    		itoa(i, Holder, 10);
    		strcat(FileName, Holder);
    		strcat(FileName, ".txt");
    		f[i] = fopen(FileName,"r+");
    	}
    
    
    }
    but i get these warnings... what is going on?
    Yikes.
    Remember that char* wah[1024] basically equals to char** wah which is not what you want. Array variable itself is a pointer, that extra * does not fit there. Just use char wah[1024]. And I noticed that in your previous code you used malloc but didn't free the buffer you allocated which is very bad.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I would use "sprintf()" to form the filename, rather than the list of strcpy/strcat -- see the example I posted previously on how to merge your files.

    --
    Mats

  13. #13
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Also sizeof(Holder) + 4 > sizeof(FileName), potential segfault ,

    Use sprintf as matsp said, and limit the size of Holder, so it and it's extension can fit into FileName.

  14. #14
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    Was thinking that the op wanted to do something like

    FILE *filepointer[numb]

    an array of file pointers....and was thinking file could only be used by one prog at a time....

  15. #15
    Registered User
    Join Date
    Aug 2007
    Posts
    42
    try:
    Code:
    int main(){
    	FILE *f[20];
    	char FileName[20];  /* Plenty large for your file naming */
    	int i;
    	int NumberOfFiles = 20;
    
    	for (i=0; i<NumberOfFiles; i++){
                             sprintf(FileName,"Channel%d.txt",i);
                             f[i] = fopen(FileName,"r+");
    	}
    
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM