Creating a txt file every for cicle

P

Polvere@Lab

Hi,

I need to create a txt file every time the loop of a for cicle is executed,
and these files have to be named as the current for variable.
Something like this (in pseudo code, I know that's fully wrong):

for(a=0; a<VAR; a++)
{
fopen (a.txt);
fprintf ( whatever);
fclose (a.txt) ;
}

Can you help me?
Thank you
Davide
 
O

Otac0n

This looks like C code, so I'll do it as if it were:

-----------------------------------
#include <cstdio>

char buffer[100];
FILE *stream;

for(a=0; a<VAR; a++)
{
sprintf(buffer, "%i.txt", a); // AFAIK, this will never overflow a
100 character buffer
stream = fopen (buffer, "w");
fprintf (stream, "%i whatever", a);
fclose (stream) ;
}

------------------------------------
 
J

John Ratliff

Otac0n said:
This looks like C code, so I'll do it as if it were:

-----------------------------------
#include <cstdio>

char buffer[100];
FILE *stream;

for(a=0; a<VAR; a++)
{
sprintf(buffer, "%i.txt", a); // AFAIK, this will never overflow a
100 character buffer
stream = fopen (buffer, "w");
fprintf (stream, "%i whatever", a);
fclose (stream) ;
}

From the glibc sprintf man page

"BUGS
Because sprintf and vsprintf assume an arbitrarily long string,
callers must be careful not to overflow the actual space; this is often
impossible to assure. Note that the length of the strings produced is
locale-dependent and difficult to predict. Use snprintf and vsnprintf
instead (or asprintf and vasprintf)."

I don't know what the standard says (I'm hoping glibc follows it), but I
would use snprintf if you cannot be sure the length of the formatted string.

--John Ratliff
 
M

Marcin Kalicinski

char buffer[100];
FILE *stream;
for(a=0; a<VAR; a++)
{
sprintf(buffer, "%i.txt", a); // AFAIK, this will never overflow a
100 character buffer

Mbuntu-uzangu locale uses unary counting system, i.e. with one digit only.
Therefore 3 is represented as three dots '...', 11 as eleven '...........'
etc. So at the hundredth iteration your buffer will overflow and the poor
natives will get their systems cracked...

cheers,
Marcin
 
M

Mike Wahler

Marcin Kalicinski said:
char buffer[100];
FILE *stream;
for(a=0; a<VAR; a++)
{
sprintf(buffer, "%i.txt", a); // AFAIK, this will never overflow a
100 character buffer

Mbuntu-uzangu locale uses unary counting system, i.e. with one digit only.
Therefore 3 is represented as three dots '...', 11 as eleven '...........'
etc. So at the hundredth iteration your buffer will overflow and the poor
natives will get their systems cracked...

%i causes conversion to decimal digits, not 'unary'.

-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top