Skip reading text file header in C

I am reading data from a file orderedfile.txt. Sometimes this file has a form header:

BEGIN header

       Real Lattice(A)               Lattice parameters(A)    Cell Angles
   2.4675850   0.0000000   0.0000000     a =    2.467585  alpha =   90.000000
   0.0000000  30.0000000   0.0000000     b =   30.000000  beta  =   90.000000
   0.0000000   0.0000000  30.0000000     c =   30.000000  gamma =   90.000000

 1                            ! nspins
25   300   300                ! fine FFT grid along <a,b,c>
END header: data is "<a b c> pot" in units of Hartrees

 1     1     1            0.042580
 1     1     2            0.049331
 1     1     3            0.038605
 1     1     4            0.049181

and sometimes there is no header, and the data starts on the first line. My code for reading in the data is shown below. It works when the data starts on line 1, but not with the header present. Is there any way around this?

int readinputfile() {
   FILE *potential = fopen("orderedfile.txt", "r");
   for (i=0; i<size; i++) {
      fscanf(potential, "%lf %lf %*f  %lf", &x[i], &y[i], &V[i]);
   }
   fclose(potential);
}
+4
source share
3 answers

The following code will use fgets () to read each line. For each line, sscanf () is used to scan the line and save it in double variables.
See Example (with stdin) in ideone .

#include <stdio.h>

int main()
{
   /* maybe the buffer must be greater */
   char lineBuffer[256];
   FILE *potential = fopen("orderedfile.txt", "r");

   /* loop through every line */
   while (fgets(lineBuffer, sizeof(lineBuffer), potential) != NULL)
   {
      double a, b, c;
      /* if there are 3 items matched print them */
      if (3 == sscanf(lineBuffer, "%lf %lf %*f %lf", &a, &b, &c))
      {
         printf("%f %f %f\n", a, b, c);
      }
   }
   fclose(potential);

   return 0;
}

, , , :

 1     1     2            0.049331

. - END header, BEGIN header , , , .
strstr().

+2

fscanf. , ; , :

int readinputfile() {
    FILE *potential = fopen("orderedfile.txt", "r");
    int res;
    while(res = fscanf(potential, "%lf %lf %*f %lf", &x[i], &y[i], &V[i])) {
        if (res != 3) {
            fscanf(potential, "%*[^\n]");
            continue;
        }
        i++;
        ... // Optionally, do anything else with the data that you read
    }
    fclose(potential);
}

+2

, , , - scanf():

FILE *fp = fopen(...);

int inHeader = 0;

size_t lineLen = 128;
char *linePtr = malloc( lineLen );

// skip header lines
while ( getline( &linePtr, &lineLen, fp ) >= ( ssize_t ) 0 )
{
    // check for the start of the header (need to do this first to
    // catch the first line)
    if ( !inHeader )
    {
        inHeader = !strncmp( linePtr, "BEGIN header", strlen( "BEGIN header" ) );
    }
    else
    {
        // if we were in the header, check for the end line and go to next line
        inHeader = strncmp( linePtr, "END header", strlen( "END header" ) );

        // need to skip this line no matter what because it in the header
        continue;
    }

    // if we're not in the header, either break this loop
    // which leaves the file at the first non-header line,
    // or process the line in this loop
    if ( !inHeader )
    {
        ...
    }
}
...

strstr() strncmp(). , / .

+2

Source: https://habr.com/ru/post/1683884/


All Articles