Thread: Help creating multiple text files

  1. #16
    Registered User
    Join Date
    Mar 2009
    Posts
    37
    Thanks adak, but it gives the same result as my effort previously. Is very frustrating. Yours does look nicer though!

    I will muck around with your version now. maybe I missed something.

  2. #17
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    OK, one more time!

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()  {
       char ch;
       char file[12] = {"TomB"};
       char newfile[12] = {"\0"};
       char ext[5] = {".txt"};
       int i = 0, length, j;
       FILE  *nf;
       
       printf("\n\n");   
       while(ch = file[i]) {
          newfile[0] = ch;
          strcat(newfile, ext);
          nf = fopen(newfile, "w");
    
          printf("\n%s ", newfile);
          if (nf == NULL) {
             fprintf(stderr, "Can't open created file %s!\n", newfile);
             exit(1);
          }
          length = strlen(newfile);
          for(j = 0; j < length; j++)
             newfile[j] = '\0';
          ++i;
    
       }
       j = getchar();
       return 0;
    }
    Output:

    T.txt
    o.txt
    m.txt
    B.txt

  3. #18
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    using strcat when you need to update only 1 char... too complicated


    Code:
    char mask[] = "TomB";
    char name[] = "1.txt";
    size_t len = strlen(mask);
    size_t i;
    for(i = 0; i< len; i++)
    {
       FILE* fp;
       name[0] = mask[i]; /* see - you just replace 1 char in the name */
       fp = fopen (name, "w");
       if(fp)
       {
          printf("File %s opened\n", name);
          fclose(fp);
       }
    }
    something like that should work I suppose
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #19
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Why not put the filename generating code into its own function, to simplify things?

    Code:
    char* generate_filename( const char* template_name, int index, const char* extension, char* buffer, int maxlen )
    {
    	char digits[ 32 ];
    	sprintf( digits, "%d", index );
    	if( ( int )( strlen( template_name ) + strlen( digits ) + strlen( extension ) + 1 ) > maxlen )
    		return NULL;
    	strcpy( buffer, template_name );
    	strcat( buffer, digits );
     	strcat( buffer, extension );
    	return buffer;
    }
    
    int main( void )
    {
    	char buffer[ 1024 ];
    	for( int i = 12; i < 20; i++ )
    	{
    		if( generate_filename( "TomB", i, ".txt", buffer, 1024 ) )
    		{
    			puts( buffer );
    		}
    		else
    		{
    			puts( "buffer too small" );
    		}
    	}
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #20
    Registered User
    Join Date
    Mar 2009
    Posts
    37
    Thank you all for your input, it has given me a lot to think about and to learn. I will try to understand fully what you have all shown.

    I will not look into how to consolidate all this and to learn how to actually manipulate the files now.

    Thanks again!


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DirectX | Drawing text
    By gavra in forum Game Programming
    Replies: 4
    Last Post: 06-08-2009, 12:23 AM
  2. Creating multiple files with string as filename
    By jmayer in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2008, 09:55 AM
  3. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM
  4. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  5. Replies: 1
    Last Post: 05-01-2003, 02:52 PM