Problem with sprintf

M

MJK

Hello All:

I am trying to use sprintf to create files within a loop. Using
something like this:
char filename[255];
for( i = 1; i < 10 ; ++i){
sprintf(filename,"/pp/wwwwww/MMMMMMMMMMM/SSS/fffffffff/
FFFFFFFF/CCCCCCCCCC%02dfffffffff.inp",i);
fp = fopen(filename,"w"); ....
and so on…
The problem is that in some of the files (in Linux) it gives me a
wired character like ^@^ but when I look at the file in Windows, it is
something like a bold 'i' which is not possible to copy it (error
message at textpad). When I make the filename like this:
sprintf(filename,"/CCCCCCCCCC%02dfffffffff.inp",i); ,everything is
fine. Any limitation in the number of characters in sprintf? Anybody
has any comments on this?

Thanks in advance,
Mohsen
 
K

Keith Thompson

MJK said:
I am trying to use sprintf to create files within a loop. Using
something like this:
char filename[255];
for( i = 1; i < 10 ; ++i){
sprintf(filename,"/pp/wwwwww/MMMMMMMMMMM/SSS/fffffffff/
FFFFFFFF/CCCCCCCCCC%02dfffffffff.inp",i);
fp = fopen(filename,"w"); ....
and so onÂ…
The problem is that in some of the files (in Linux) it gives me a
wired character like ^@^ but when I look at the file in Windows, it is
something like a bold 'i' which is not possible to copy it (error
message at textpad). When I make the filename like this:
sprintf(filename,"/CCCCCCCCCC%02dfffffffff.inp",i); ,everything is
fine. Any limitation in the number of characters in sprintf? Anybody
has any comments on this?

The C99 standard says:

The number of characters that can be produced by any single
conversion shall be at least 4095.

Since your array is only 255 characters long, and since most
implementations probably don't impose a fixed limit anyway, that's
not likely to be your problem.

You said your code is "something like" what you posted. If you
post real code (a working program that exhibits the problem,
copy-and-pasted from your source file to your news client), we
might have some chance of helping you.
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top