I have to generate a number of files based on a variable.
Is there a way to do this withou declaring the FILE (FILE *fptr)?
I've search a lot, and found about fstream, but this, apparently, only works with C++T
Thanks
I have to generate a number of files based on a variable.
Is there a way to do this withou declaring the FILE (FILE *fptr)?
I've search a lot, and found about fstream, but this, apparently, only works with C++T
Thanks
Just call `fclose(fopen(filename, "w"));`.
Last edited by msh; 06-20-2012 at 06:59 AM. Reason: Derp
Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.
If lack of clarity was a competition sport, you would win a fair few prizes.
What do you mean by "generate a number of files based on a variable"?????
The user will put a numer, like 3
And I have to generate 3 txt files.
Well then you would need a loop and some naming convention for your files (such as 1.txt, 2.txt, 3.txt)
1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
3. Get rid of conio.h and other antiquated DOS crap headers.
4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.
Yeah, i think i know how to do this loop naming...
But, dont I need to declare the pointer to these files?
I dont know what number the user will put.
If was only 3 files, i could write
FILE *fptr1
FILE *fptr2
FILE *fptr3
Can I just declare one of this and rename the txt everytime i want to create a new file?
The name of the variable is meaningless. Naming a FILE pointer fptr1 does not mean it can only open a file on disk (or wherever) named fptr1.
Look again at claudiu's post. Imagine what would happen if the FILE pointer (whatever you choose to name it) was local to the loop body .....