Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

simple: integer to ascii

Status
Not open for further replies.

1510982

Programmer
Feb 17, 2002
57
SI
the problem is very simple, i have a for loop and i have to generate files 1.txt, 2.txt 3.txt... n.txt.

FILE *target;

for(i=0; i<100; i++) {
target = fopen(i . &quot;.txt&quot;,&quot;w&quot;); //here comes the problem
fclose(target);
}

i thank you in advance, lorenzo
 
Code:
char buf[30];  // why 30? dunno... why not
for(i=0; i<100; i++) {
  sprintf( buf, &quot;%d.txt&quot;, i);
  target = fopen(i . buf,&quot;w&quot;); //there goes the problem
  fclose(target);
}

does that help?
-pete
 
palbano, thanks, ofcourse it helps.

here is fixed sourcecode:

FILE *test;
char buf[30];
for(int i=0; i<100; i++) {
sprintf(buf, &quot;%d.txt&quot;, i);
test = fopen(buf, &quot;w&quot;);
fclose(test);
}

thanks again. lorenzo.
 
The solution appears to be good, I did wonder in the first example why the file was never opened, but it was closed in the end. You have to always do an fopen before you do the fclose. It appears that the buffer will also read a 0.txt file as well as the 1.txt, 2.txt....., n.txt. Should there be a test like an (if buf > &quot;0.txt&quot; && buf < &quot;100.txt&quot;) continue to process otherwise exit. It appears this would be a good test to include. I just wondered.

Linda
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top