Thread: Indexing files to write to?

  1. #1
    Registered User
    Join Date
    May 2008

    Indexing files to write to?

    I'm writing a program and I want to put a written file inside a for loop. For each run through the loop, I want to write to a different file. How do I do this? Here's the code:

    for(f=0;f<files;f++) {
       output= fopen("","w");             //now open/create file for writing
       for(t=0;t<number;t++) {                     //bases are generated with the given frequencies
          if(rand<freqa) {
          if(rand>=freqa && rand< (freqa + freqt)) {
          if(rand>=(freqa + freqt) && rand< (freqa + freqt + freqg)) {
          if(rand>= (1 - freqc) && rand<1.00) {
          fprintf(output, "%c",base);
       fprintf(output, "%c",'Q');           //add 'Q' to end of file (quit command in program that will read the file)
    I tried putting a '%d' inside the fopen command where I specify the name of the file, but that obviously didn't work. Ideally, I'd like it to write files with the names,, etc. up to whatever value I feed it for the variable 'files.' Is there any way to do what I'm trying to accomplish?

  2. #2
    Join Date
    Oct 2007
    Inside my computer
    Read the manual for fopen. It takes a filename, but it never mentions it takes a variable number of arguments or can format a string.
    Then read the manual for sprintf and you should understand.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    May 2008
    Thanks, that did it. I figured fscanf wouldn't do it, but I didn't know about sprintf.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to write to files ?
    By Akilla in forum C++ Programming
    Replies: 6
    Last Post: 07-08-2002, 12:01 PM
  2. The problem read and write in files
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 07-05-2002, 12:54 PM
  3. write in c
    By PutoAmo in forum C Programming
    Replies: 6
    Last Post: 04-03-2002, 07:53 PM
  4. Can I not call fstream to write to two files sequentially?
    By tigeress in forum C++ Programming
    Replies: 1
    Last Post: 01-20-2002, 01:26 PM
  5. Write and use data stored in data files...
    By alex6852 in forum C++ Programming
    Replies: 2
    Last Post: 11-01-2001, 01:45 PM