fopen

This is a discussion on fopen within the C Programming forums, part of the General Programming Boards category; is it possible to work with a variable in this piece of code if ((fp = fopen("output", "r")) == NULL) ...

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    67

    Angry fopen

    is it possible to work with a variable in this piece of code
    if ((fp = fopen("output", "r")) == NULL)

    so it would be fopen("outputvarname", ""r")

    something like fopen("output%d", "r", i)
    And i've tried
    Code:
     
    sprintf(bestand, "output%d", i);
            
           if ((fp = fopen(bestand, "r")) == NULL) {
    can someone give me a hint to this so i dont have to open 255 files seperately
    thanx
    Last edited by TeQno; 05-22-2003 at 11:13 AM.

  2. #2
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    Well this worked for me:
    Code:
    int main()
    {
      unsigned int i;
      FILE * pFile;
      char filename[BUFSIZ];
    
      for (i = 0; i < 2; i++)
      {
        sprintf(filename, "test%d.txt", i);
        if ((pFile = fopen(filename, "wt")) != NULL)
        {
          fprintf(pFile, "%d", i);
          fclose(pFile);
        }
      }
    
      return 0;
    }
    DavT
    -----------------------------------------------

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with stat() and fopen()
    By movl0x1 in forum C Programming
    Replies: 6
    Last Post: 07-25-2007, 05:28 AM
  2. fopen help.
    By chriscolden in forum C Programming
    Replies: 17
    Last Post: 01-13-2006, 05:27 AM
  3. problem with fopen command
    By emon in forum C Programming
    Replies: 2
    Last Post: 03-11-2004, 11:11 PM
  4. fopen() and open()
    By Encrypted in forum C Programming
    Replies: 8
    Last Post: 02-09-2003, 03:57 PM
  5. fopen vs open
    By rotis23 in forum Linux Programming
    Replies: 5
    Last Post: 12-10-2002, 01:30 PM

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