Variable filenames

This is a discussion on Variable filenames within the C Programming forums, part of the General Programming Boards category; Say I have a char array of file names, how would I use them with a fucntion like fprintf? For ...

  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,185
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 12:57 PM
  2. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 11:07 AM
  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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21