Since you all helped me so well the previous time, I have a second question for you. I think it's fairly simple, but for some reason I can't find the answer.
I want to write to a file, in a certain format; however, it doesn't write it exactly as I want:

fprintf(fpout, "ATOM     %2d  %c    %c      %d      %02.3f   %02.3f    %02.3f\n", i, 'C', 'C', 1, *(x+i), *(y+i), *(z+i));

This produces something like this:

ATOM      6  C    C      1      5.047   5.964    17.520
ATOM      7  C    C      1      10.631   18.405    10.309
ATOM      8  C    C      1      16.209   3.768    17.726
ATOM      9  C    C      1      11.412   1.535    16.305
ATOM     10  C    C      1      19.698   2.367    17.878
ATOM     11  C    C      1      15.690   2.018    5.066

(in the text file everything is in OK up to the 1).
However, as you can see, the coordinates (the last 3 floats).

because some numbers are in 2.3f format, and others in 1.3f format, they don't line up as i want. I tried using "%02.3f" so that it embeds a 0 in front of the coordinate IF the coordinate is in 1.3f format. (Still with me? ). But that doesn't work; the 0 isn't put in the file.
For some reason it does work with the %2d in the 2nd column.

Any ideas?

Thanks in advance,

Recommended Answers

All 2 Replies

The field width is the total number of printed characters, not the number of characters before the radix. Try "%06.3f" instead.

commented: short and precise +1

Wow... that's fast and correct ;). Thans a lot!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.