Code:
double (*alpha)[3], (*beta)[3], (*gamma)[4], (*chi)[4], (*epsilon)[3], (*zeta)[3];
alpha = malloc(length*sizeof(double)*3);
beta = malloc(length*sizeof(double)*3);
gamma = malloc(length*sizeof(double)*4);
epsilon = malloc(length*sizeof(double)*3);
chi = malloc(length*sizeof(double)*4);
zeta = malloc(length*sizeof(double)*3);
for (unsigned int nuc = 2; nuc <= 4; nuc++) {//read in torsions with 3 present
//read in alpha
sprintf(file,"%salpha%u",simulation,nuc);
FILE *fh;
fh = fopen(file,"r");
if (fh != NULL) {
unsigned int c = 0;
while(!feof(fh)) {
if (fscanf(fh,"%f %lf",&time,&alpha[nuc-2][c]) != 2) {
continue;
}
if (alpha[nuc-2][c] < 0.0) {
alpha[nuc-2][c] += 360.0;
}
}
} else {
printf("Could not read %s.\n",file);
exit(EXIT_FAILURE);
}
//read in beta
sprintf(file,"%sbeta%u",simulation,nuc);
fh = fopen(file,"r");
if (fh != NULL) {
unsigned int c = 0;
while(!feof(fh)) {
if (fscanf(fh,"%f %lf",&time,&beta[nuc-2][c]) != 2) {
continue;
}
if (beta[nuc-2][c] < 0.0) {
beta[nuc-2][c] += 360.0;
}
}
} else {
printf("Could not read %s.\n",file);
exit(EXIT_FAILURE);
}
//read in zeta
sprintf(file,"%szeta%u",simulation,nuc-1);
fh = fopen(file,"r");
if (fh != NULL) {
unsigned int c = 0;
while(!feof(fh)) {
if (fscanf(fh,"%f %lf",&time,&zeta[nuc-1][c]) != 2) {
continue;
}
if (zeta[nuc-1][c] < 0.0) {
zeta[nuc-1][c] += 360.0;
}
}
} else {
printf("Could not read %s.\n",file);
exit(EXIT_FAILURE);
}
//read in epsilon
sprintf(file,"%sepsilon%u",simulation,nuc-1);
fh = fopen(file,"r");
if (fh != NULL) {
unsigned int c = 0;
while(!feof(fh)) {
if (fscanf(fh,"%f %lf",&time,&epsilon[nuc-1][c]) != 2) {
continue;
}
if (epsilon[nuc-1][c] < 0.0) {
epsilon[nuc-1][c] += 360.0;
}
}
} else {
printf("Could not read %s.\n",file);
exit(EXIT_FAILURE);
}
}
for (unsigned int nuc = 1; nuc <= 4; nuc++) {
//read in gamma
sprintf(file,"%sgamma%u",simulation,nuc);
FILE *fh;
fh = fopen(file,"r");
if (fh != NULL) {
unsigned int c = 0;
while(!feof(fh)) {
if (fscanf(fh,"%f %lf",&time,&gamma[nuc-1][c]) != 2) {
continue;
}
if (gamma[nuc-1][c] < 0.0) {
gamma[nuc-1][c] += 360.0;
}
}
} else {
printf("Could not read %s.\n",file);
exit(EXIT_FAILURE);
}
//read in chi
sprintf(file,"%schi%u",simulation,nuc);
fh = fopen(file,"r");
if (fh != NULL) {
unsigned int c = 0;
while(!feof(fh)) {
if (fscanf(fh,"%f %lf",&time,&chi[nuc-1][c]) != 2) {
continue;
}
if (chi[nuc-1][c] < 0.0) {
chi[nuc-1][c] += 360.0;
}
}
} else {
printf("Could not read %s.\n",file);
exit(EXIT_FAILURE);
}
}
//free memory for each torsion stored in memory
free(alpha); alpha = NULL;
free(beta); beta = NULL;
free(gamma); gamma = NULL;
free(epsilon); epsilon = NULL;
free(zeta); zeta = NULL;
free(chi); chi = NULL;
This code works just fine (this is a snippet from the complete program code). This is pretty cumbersome and lengthy, and I want other people to be able to easily read it in as few lines as possible in C, which more people know than perl, is faster, and is almost universally installed. Also, as you said, perl is often criticized for being difficult to read.
Bookmarks