diff --git a/tools/msi2lmp/src/ReadCarFile.c b/tools/msi2lmp/src/ReadCarFile.c index 7cde0914c4..24e2272a83 100644 --- a/tools/msi2lmp/src/ReadCarFile.c +++ b/tools/msi2lmp/src/ReadCarFile.c @@ -127,12 +127,15 @@ void ReadCarFile(void) /* First pass through file -- Count molecules */ - while(fgets(line,MAX_LINE_LENGTH,CarF) != NULL ) - if( strncmp(line,"end",3) == 0 ) + while (fgets(line,MAX_LINE_LENGTH,CarF) != NULL ) + if (strncmp(line,"end",3) == 0 ) no_molecules++; /* Allocate space to keep track of the number of atoms within a molecule */ - + if (no_molecules < 1) { + fprintf(stderr, "No molecules in system"); + exit(32); + } no_atoms = (int *) calloc(no_molecules,sizeof(int)); if ( no_atoms == NULL ) { printf("Could not allocate memory for no_atoms\n"); diff --git a/tools/msi2lmp/src/SearchAndFill.c b/tools/msi2lmp/src/SearchAndFill.c index f195e739c9..7a7a1032ea 100644 --- a/tools/msi2lmp/src/SearchAndFill.c +++ b/tools/msi2lmp/src/SearchAndFill.c @@ -12,6 +12,7 @@ #include #include #include +#include #if defined(_WIN32) #define strdup(x) _strdup(x) @@ -91,10 +92,14 @@ void SearchAndFill(struct FrcFieldItem *item) } file_pos = ftell(FrcF); + if (file_pos < 0) { + fprintf(stderr, "Could not obtain file stream position: ", strerror(errno)); + exit(2); + } /* Count the number of lines until next item is found */ - while( strncmp(fgets(line,MAX_LINE_LENGTH,FrcF), "#", 1) != 0 ) + while (strncmp(fgets(line,MAX_LINE_LENGTH,FrcF), "#", 1) != 0 ) ctr++; /* Allocate the memory using calloc */ @@ -110,7 +115,10 @@ void SearchAndFill(struct FrcFieldItem *item) /* Read lines until keyword is found */ - fseek(FrcF,file_pos,SEEK_SET); + if (fseek(FrcF,file_pos,SEEK_SET) < 0) { + fprintf(stderr, "Resetting file stream failed: ", strerror(errno)); + exit(2); + } strcpy(line,"empty"); /* Read lines until data starts (when !--- is found) */