Indexing files to write to?

This is a discussion on Indexing files to write to? within the C Programming forums, part of the General Programming Boards category; I'm writing a program and I want to put a written file inside a for loop. For each run through ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    15

    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:

    Code:
    for(f=0;f<files;f++) {
    
       srand(f);
    
       output= fopen("sequence.in","w");             //now open/create file for writing
    
       for(t=0;t<number;t++) {                     //bases are generated with the given frequencies
          rand=rnum();
    
          if(rand<freqa) {
          	base='A';
          }
          if(rand>=freqa && rand< (freqa + freqt)) {
          	base='T';
          }
          if(rand>=(freqa + freqt) && rand< (freqa + freqt + freqg)) {
          	base='G';
          }
          if(rand>= (1 - freqc) && rand<1.00) {
          	base='C';
          }
    
          fprintf(output, "%c",base);
       }
    
       fprintf(output, "%c",'Q');           //add 'Q' to end of file (quit command in program that will read the file)
    
       fclose(output);
    
       }
    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 sequence0.in, sequence1.in, 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
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,450
    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
    Posts
    15
    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, 06: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, 12:26 PM
  5. Write and use data stored in data files...
    By alex6852 in forum C++ Programming
    Replies: 2
    Last Post: 11-01-2001, 12:45 PM

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