please help me out with this problem????

Status
Not open for further replies.

thinker

Think Well Before You Do
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:

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


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?
 

hansraj

In the zone
man u r probably dealing with something too fundoo..... i cant make out a single line of u r post except the last one. hope u find some good geek around
 

fun2sh

Pawned!... Beyond GODLIKE
wil u put ur complete program here???
n there is a seperate programin section for all these
 

QwertyManiac

Commander in Chief
Try this example. I had to remove the pre-decimal specifier, so that the printf obeys the width set, dunno why it didn't though, when coupled with that specifier.

Code:
#include<stdio.h>

int main()
{
    int i;
    float a[4]={5.123,10.444,16.555,11.777},b[4]={6.321,7.432,8.654,18.987},c[4]={13.124,7.442,19.201,4.666};
    for(i=0;i<4;i++)
    printf("ATOM %02d %c %c %d [B]%0[I]6[/I].3f %0[I]6[/I].3f %0[I]6[/I].3f[/B]\n", i, 'C', 'C', 1, a[i], b[i], c[i]);
}
Outputs as:
Code:
ATOM 00 C C 1 05.123 06.321 13.124
ATOM 01 C C 1 10.444 07.432 07.442
ATOM 02 C C 1 16.555 08.654 19.201
ATOM 03 C C 1 11.777 18.987 04.666
There you go, all are lined up evenly.

I think using C++ would be better for formatting your outputs the easy way. Why choose C?
 
Last edited:
Status
Not open for further replies.
Top Bottom