M
mdfoster44
Hi,
I want to read input data for crystal structures.
Often atom positions are given as fractions,
for example one line might read
"Si 1/3 0.250000 1/6"
I would like to store these fractions and print them later with eight
decimals places.
I want to be able to print them as
"Si 0.33333333 0.25000000 0.16666667"
Could someone please advise me how to do this?
I guess it's a very simple question, but I have been scanning the docs
and I've not found a suitable answer yet, except trying not to
"sscanf". Which I have been!
Here's my code:
#include <string.h>
#include <stdio.h>
typedef struct vector {
double x,y,z;
} vector;
int main(void)
{
char line[400];
char label[5];
FILE *input;
vector atom;
if( !(input=fopen("fraction.txt","r")) ){
printf("Cannot open file!\n");
}
while(fgets(line,399,input) != NULL) {
sscanf(line, "%s %lf %lf %lf", &label, &atom.x, &atom.y, &atom.z);
printf("%s %.8lf %.8lf %.8lf\n", label, atom.x, atom.y, atom.z);
}
}
fraction.txt:
Si 1/3 0.250000 1/6
output:
Si 1.00000000 0.00000000 0.00000000
cheers,
Martin
I want to read input data for crystal structures.
Often atom positions are given as fractions,
for example one line might read
"Si 1/3 0.250000 1/6"
I would like to store these fractions and print them later with eight
decimals places.
I want to be able to print them as
"Si 0.33333333 0.25000000 0.16666667"
Could someone please advise me how to do this?
I guess it's a very simple question, but I have been scanning the docs
and I've not found a suitable answer yet, except trying not to
"sscanf". Which I have been!
Here's my code:
#include <string.h>
#include <stdio.h>
typedef struct vector {
double x,y,z;
} vector;
int main(void)
{
char line[400];
char label[5];
FILE *input;
vector atom;
if( !(input=fopen("fraction.txt","r")) ){
printf("Cannot open file!\n");
}
while(fgets(line,399,input) != NULL) {
sscanf(line, "%s %lf %lf %lf", &label, &atom.x, &atom.y, &atom.z);
printf("%s %.8lf %.8lf %.8lf\n", label, atom.x, atom.y, atom.z);
}
}
fraction.txt:
Si 1/3 0.250000 1/6
output:
Si 1.00000000 0.00000000 0.00000000
cheers,
Martin