Originally posted by Prelude
itoa() isn't a standard function, it would be better/easier/less foogly(Yes, I made that up) to use sprintf.
-Prelude
foogly I like
I hadn't seen your post before I went home, so I've used itoa (and it works ) so a general question - should you avoid all non-standard functions? for itoa you have to include stdlib.h, isn't this included in all compilers? should you try to only use ANSI functions? are POSIX ok? Is the only problem portability, or are standard functions more efficient as well? At the moment I'm more interested in getting something that works on my PC, but I'd like to avoid bad habits!
Some other questions, I'm using a for loop with the int value, in the loop I need to open a file, copy the contents to another file with filename the string value of the integer value with a file extension (*.in), append two lines of text to the file including string values for the int value of the iteration. Because I need to create the file with string value of int value of the iteration (with extension) and later use the string value without the file extension, I've used itoa to generate the string value (nfin), defined a new string variable (runnumber), set it equal to nfin, used strcat to append the extension to nfin, then used runnumber to append the string value in the file.... it works but is there a better way of doing it?:
Code:
int iruns;
char nfin[100];
for (iruns = 0; iruns < nruns; iruns++)
{
itoa(iruns+1, nfin, 10); \*iruns int to string*\
char runnumber[100];
runnumber = nfin;
strcat(nfin, ".in");
ftemp = fopen("input.temp","r");
fin = fopen(nfin, "a");
while ((character = fgetc(ftemp)) != EOF)
{
fputc(character, fin);
}
fputs("database = D:\\", fin);
fputs(runnumber, fin);
fputs(".txt\n", fin);
fputs("path-filename = D:\\", fin);
fputs(runnumber, fin);
fputs(".res\n", fin);
fclose(fin);
do more stuff.........
later in the loop I need to run a console application with a path to the this *.in file that I've created. I've used spawnl(P_WAIT, "prog.exe", " nfin.in"); the problem here is I need to insert a space before the file name, so I can't just use nfin, so I've defined *another* string (runfile) as a space, and appended nfin using strcat:
Code:
char runfile[100] = " ";
strcat(runfile, nfin);
spawnl(P_WAIT, "Chess.exe", runfile);
Again it works, but I'm sure there's a better way of doing it.... Any advice will be much appreciated
Apologies for the long post and thankyou!