Thread: Variable filenames

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    8

    Variable filenames

    Say I have a char array of file names, how would I use them with a fucntion like fprintf? For example, I have a loop that runs ten times, and each time I want to print to a different file. How do I convert strings into filenames that fprintf can use, or is there an easier way to do this?

    Thanks for you help.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you want to write to a file, you must first open it.

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    8
    Right, I've opened ten files that have corresponding names to the ten entries in a char array. How use the strings to access the files though?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    An array of FILE* as well perhaps?

    fp[ i ] = fopen( filename[ i ], "w" );
    kinda thing.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about printing a variable???
    By Hoser83 in forum C++ Programming
    Replies: 2
    Last Post: 03-31-2006, 01:57 PM
  2. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 12:07 PM
  3. Replies: 2
    Last Post: 04-12-2004, 01:37 AM
  4. write Variable and open Variable and get Information
    By cyberbjorn in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2004, 01:30 AM