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.
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.
Where did you hear that?kryptkat> you can only have one file open at a time.
If you show the code you tried, we could help you determine the problem.chico1st> i tried this but it didnt work
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.
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:
It just looks through its internalCode: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 */ }
FILE _iob[FOPEN_MAX] array to find the first unopened file. If all are opened NULL is returned
Yeah , for example mine can open 256 .
isnt it too much =) ?
here are the errorsCode:#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)); } }
C:\Files.c(11) : error C2115: '=' : incompatible types
Error executing cl.exe.
Last edited by chico1st; 08-20-2007 at 09:17 AM.
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.
ok i tried this:
but i get these warnings... what is going on?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+"); } }
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
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.
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
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
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.
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....
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+"); } }