Menu

Dynamic Filename in C?

noob noob
2008-12-04
2012-09-26
  • noob noob

    noob noob - 2008-12-04

    Is it possible to have dynamic file-names in C, I need to write data to 9000 + files

     
    • cpns

      cpns - 2008-12-04

      A filename as passed to fopen() (and other file creation operations) is a simple string, so you simply create the string you need, the process has nothing to do with filenames specifically, it is more generally applicable. For example:


      char fname[32] ;
      int seq ;

      for( i = 0; i < 10000; i++ )
      {
      sprintf( fname, "rootname%4d.dat", i ) ;

      // use fname to open/create the file
      

      }

      The above creates the names:

      rootname0000.dat
      rootname0001.dat
      rootname0003.dat
      .
      .
      .
      rootname9998.dat
      rootname9999.dat

      Clifford

       
    • Wayne Keen

      Wayne Keen - 2008-12-04

      Are you asking how to create filenames on the fly in a program, something like

      wayne0001.txt
      wayne0002.txt

      etc.?

      It seems you could do this (I do this all the time in Python) with string operations,
      i.e. create your numbers, do an 'itoa' on them, can concatenate together into an
      appropriate string representation of your filename.

      Wayne

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.