The file starts with a number (N) that tells how many atoms (and how many rows) will follow.
The remaining file is made of 4 columns with type of atom and 3 cordinates. An example is the following:
4
Pd 1 1 2.
Pd 3 1.4 1.
Pd 1 4.3 11
Pd 4 1.2 3.
Now, for every simulation or elaboration, i need to read these kind of files and, preferably, create 4 arrays one of strings and the other 3 of float.
The best thing would be to have a separate function outside the main, in order to call it when needed.
So the values must be passed by reference and not by value, using C pointers.
The problems arise because of the clumsy way C handles strings, which basically are arrays of char.
Moreover, I do not expect the atom type to be a string of more than 5 characters, so what I need to do when passing by reference my array of strings (which is a Nx6 matrix of char) is to send a triple pointer to the function that is, actually, a double pointer to a string and everything blows up without a careful use of & and *.
Here's the result.
That's it. It took time to get it so simply and now I am happy and I wanted to share this in order to avoid a lot of complications to those interested.
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- void parserxyz(char *fileneeded, double **atomx, double **atomy, double **atomz, char (**atomtype)[5], unsigned int *N)
- {
- FILE * fp;
- unsigned int i;
- fp = fopen(fileneeded, "r");
- if (fp == NULL) {
- exit(EXIT_FAILURE);
- }
- fscanf(fp,"%u", N);
- *atomx = (double*) malloc(sizeof(double)*(*N));
- *atomy = (double*) malloc(sizeof(double)*(*N));
- *atomz = (double*) malloc(sizeof(double)*(*N));
- *atomtype= malloc(5*sizeof(char) * (*N));
- for (i=0; i<(*N); ++i)
- {
- fscanf(fp,"%s %lf %lf %lf", (*atomtype)[i], &(*atomx)[i], &(*atomy)[i], &(*atomz)[i] );
- }
- fclose(fp);
- }
Nessun commento:
Posta un commento