Thread: Creating new files

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    5

    Creating new files

    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

  2. #2
    Novice
    Join Date
    Jul 2009
    Posts
    568
    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.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    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"?????
    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, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Jun 2012
    Posts
    5
    The user will put a numer, like 3
    And I have to generate 3 txt files.

  5. #5
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    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.

  6. #6
    Registered User
    Join Date
    Jun 2012
    Posts
    5
    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?

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    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 .....
    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, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating files
    By sigur47 in forum C++ Programming
    Replies: 7
    Last Post: 02-18-2012, 04:28 AM
  2. Creating Files
    By Seb in forum C++ Programming
    Replies: 5
    Last Post: 06-15-2003, 08:11 PM
  3. help on creating files
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 01-15-2002, 07:26 PM
  4. creating .DAT files
    By breed in forum C Programming
    Replies: 3
    Last Post: 11-30-2001, 09:09 AM
  5. creating + using files
    By dave in forum C++ Programming
    Replies: 1
    Last Post: 10-26-2001, 09:49 PM