Subj : Re: index dependent filename To : borland.public.cpp.borlandcpp From : "Dwayne" Date : Tue Oct 28 2003 11:22 am Hello Cory, Cory >>I want to run a loop indexed by an integer (k) several times and output the data to a different file with each loop. Is there a way to write print to file statements wth name that depend on the index (k)? For example how do I write "yahoo on (k)" to file yahoo_k.txt for each index (k).<< Here is a extremely simple one. I could have left out a couple of things, Like the FileNameMain. and assignment of it, but, if somewhere else in the program you assign your Base Name, you have a idea how it could be done. #include #include #include #include void main(void) { char FileName[15]; char FileNameMain[15]; char IntVal[4]; int a; strcpy(FileNameMain,"Yahoo"); // Assign Base Name. for(a=1;a<10;a++) { itoa(a,IntVal,10); // If you notice, I only allowed 14 characters for the filename This includes the period AND the extension. // though I declared the length of 15, you must add one character for the NULL terminator. // the same thing goes with the InvVal..you are only allowed up to a 3 digit integer, 999 files. // In a DOS format, you are allowed 8x3 file names. this means a total of 12 characters. So a length // of 14 characters is plenty! . If you want longer filenames, better increase the lengths of the strings. //memset(FileName,'\0',strlen(FileName)); //I like making sure I am using a null string. easier to debug too. // This program will not create the files for you, but it will *make* the filenames for you. I don't want // to give you a program that creates a bunch of garbage on your computer...right? strcpy(FileName,FileNameMain); //Step one of creating index. assign Base. strcat(FileName,IntVal); //Step two, add on integer that has been converted to character strcat(FileName,".txt"); //Step three, add your extension. // Here you open your file and do something with it // Open file, // do stuff // close file. } } .