简体   繁体   中英

variable definition by fscanf textfile.txt in C

I have a textfile.txt containing variable definitions line by line, eg length = 1.1m

Using fscanf I want to store in my variable double length the value 1.1

Can someone give me some hints how to use fscanf in this case having a '=' seperator?

Thanks in advance!

Edit: Here is an exemplary inputfile.txt showing its structure:

[elements]
Number of elements = 2
length_1 = 1.5m
length_2 = 2.1m
velocity_1 = 0.35m/s
velocity_2 = 0.11m/s

[cells]
Number of cells = 2
cell_1 = 0.0m
cell_2 = 1.3m

I want to extract the parameters of both sections to two different structs.

A simple program to read a formatted file:

#include <stdio.h>

int main(void) {
    FILE *fp = fopen("data.txt", "r");
    float length;

    if (fp == NULL) {
        printf("The file was failed to open.\n");
        return -1;
    }

    while (fscanf(fp, "%*s %*s %f", &length) == 1)
        printf("%f", length);

    return 0;
}

Notice that we've used

%*s %*s %f

format %*s string skips the word from reading it.

The file data.txt looks something like:

length = 5.324325m

Then you will get an output of:

5.324325

You can now clearly see the strings are truncated and the value is successfully assigned to the variable.

Assuming if your text file is something like this:

length = 1.2m
length = 3.4m
length = 12.4m

Then you can read it using this code:

FILE* filePtr = fopen("textfile.txt","r"); 
if (filePtr==NULL) 
{ 
    printf("no such file."); 
    return 0; 
} 
  
float num; 
while (fscanf(filePtr,"length = %f", &num)==1) 
    printf("%f\n", num);
fclose(filePtr);

For more information for reading from file, check this link

Here a way to do, the two sections can be given in any order, or only one can be given, or even none:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef struct Element {
  double length;
  double velocity;
} Element;

void * read_section(FILE * fp, const char * what, int * n, size_t sz)
{
  if (*n != 0) {
    fprintf(stderr, "invalid file, several sections [%s]\n", what);
    exit(-1);
  }

  char fmt[64];
  
  sprintf(fmt, " Number of %s = %%d", what); /* notice space at beginning */

  if (fscanf(fp, fmt, n) != 1) {
    fprintf(stderr, "invalid file, expected 'Number of %s = <n>'\n", what);
    exit(-1);
  }
  
  if (*n <= 0) {
    fprintf(stderr, "number of %s must be > 0\n", what);
    exit(-1);
  }
  
  void * r = malloc(*n * sz);
  
  if (r == NULL) {
    fprintf(stderr, "not enough memory for %d %s\n", *n, what);
    exit(-1);
  }
  
  return r;
}

void read_var(FILE * fp, const char * var, const char * unity, int rank, double * v)
{
  char fmt[64];
  
  sprintf(fmt, " %s_%d = %%lg%s", var, rank, unity); /* notice space at beginning */
  
  if (fscanf(fp, fmt, v) != 1) {
    fprintf(stderr, "invalid file, expected '%s_%d = <val>%s'\n", var, rank, unity);
    exit(-1);
  }
}

int main(int argc, char ** argv)
{
  if (argc != 2) {
    fprintf(stderr, "Usage: %s <file>\n", *argv);
    exit(-1);
  }
  
  FILE * fp = fopen(argv[1], "r");
  
  if (fp == NULL) {
    perror("cannot open file");
    exit(-1);
  }
  
  int nelts = 0;
  Element * elts = NULL;
  int ncells = 0;
  double * cells = NULL;
  char line[32];
  int i;
  
  while (fscanf(fp, " %31s", line) == 1) { /* notice space at beginning of format */
    if (!strcmp(line, "[elements]")) {
      elts = read_section(fp, "elements", &nelts, sizeof(*elts));
      for (i = 0; i != nelts; ++i)
        read_var(fp, "length", "m", i+1, &elts[i].length);
      for (i = 0; i != nelts; ++i)
        read_var(fp, "velocity", "m/s", i+1, &elts[i].velocity);
    }
    else if  (!strcmp(line, "[cells]")) {
      cells = read_section(fp, "cells", &ncells, sizeof(*cells));
      for (i = 0; i != ncells; ++i)
        read_var(fp, "cell", "m", i+1, &cells[i]);
    }
    else {
      fputs("invalid file, section header expected\n", stderr);
      exit(-1);
    }
  }
  
  fclose(fp);
  
  /* to check */
  
  printf("%d elements:\n", nelts);
  for (i = 0; i != nelts; ++i)
    printf("%d) length=%g, velocity=%g\n", i, elts[i].length, elts[i].velocity);
  
  printf("\n%d cells:", ncells);
  for (i = 0; i != ncells; ++i)
    printf(" %g", cells[i]);
  putchar('\n');
  
  free(elts);
  free(cells);
  
  return 0;
}

Notice the space at the beginning of the formats when it is needed to bypass newlines (and possible other spaces).

Compilation and executions with your example:

pi@raspberrypi:/tmp $ gcc -g -Wall x.c
pi@raspberrypi:/tmp $ 
pi@raspberrypi:/tmp $ cat f1
[elements]
Number of elements = 2
length_1 = 1.5m
length_2 = 2.1m
velocity_1 = 0.35m/s
velocity_2 = 0.11m/s

[cells]
Number of cells = 2
cell_1 = 0.0m
cell_2 = 1.3m
pi@raspberrypi:/tmp $ ./a.out f1
2 elements:
0) length=1.5, velocity=0.35
1) length=2.1, velocity=0.11

2 cells: 0 1.3
pi@raspberrypi:/tmp $ 

exchanging section order

pi@raspberrypi:/tmp $ cat f2
[cells]
Number of cells = 2
cell_1 = 0.0m
cell_2 = 1.3m
[elements]
Number of elements = 2
length_1 = 1.5m
length_2 = 2.1m
velocity_1 = 0.35m/s
velocity_2 = 0.11m/s
pi@raspberrypi:/tmp $ ./a.out f2
2 elements:
0) length=1.5, velocity=0.35
1) length=2.1, velocity=0.11

2 cells: 0 1.3
pi@raspberrypi:/tmp $ 

only elements

pi@raspberrypi:/tmp $ cat f3
[elements]
Number of elements = 2
length_1 = 1.5m
length_2 = 2.1m
velocity_1 = 0.35m/s
velocity_2 = 0.11m/s

pi@raspberrypi:/tmp $ ./a.out f3
2 elements:
0) length=1.5, velocity=0.35
1) length=2.1, velocity=0.11

0 cells:
pi@raspberrypi:/tmp $ 

only cells

pi@raspberrypi:/tmp $ cat f4
[cells]
Number of cells = 2
cell_1 = 0.0m
cell_2 = 1.3m
pi@raspberrypi:/tmp $ ./a.out f4
0 elements:

2 cells: 0 1.3
pi@raspberrypi:/tmp $ 

empty input file

pi@raspberrypi:/tmp $ ./a.out /dev/null
0 elements:

0 cells:
pi@raspberrypi:/tmp $ 

more elements and cells

pi@raspberrypi:/tmp $ cat f
[elements]
Number of elements = 3
length_1 = 1.5m
length_2 = 2.1m
length_3 = 1.1m
velocity_1 = 0.35m/s
velocity_2 = 0.11m/s
velocity_3 = 0.33m/s

[cells]
Number of cells = 5
cell_1 = 0.0m
cell_2 = 1.3m
cell_3 = 2.3m
cell_4 = 3.3m
cell_5 = 4.3m
pi@raspberrypi:/tmp $ ./a.out f
3 elements:
0) length=1.5, velocity=0.35
1) length=2.1, velocity=0.11
2) length=1.1, velocity=0.33

5 cells: 0 1.3 2.3 3.3 4.3
pi@raspberrypi:/tmp $ 

and the possible error cases are checked, I encourage you to never suppose all is always ok but to check it is

To finish if you are under Linux/Unix check your program running under valgrind , for instance:

pi@raspberrypi:/tmp $ valgrind ./a.out f
==13981== Memcheck, a memory error detector
==13981== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==13981== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==13981== Command: ./a.out f
==13981== 
3 elements:
0) length=1.5, velocity=0.35
1) length=2.1, velocity=0.11
2) length=1.1, velocity=0.33

5 cells: 0 1.3 2.3 3.3 4.3
==13981== 
==13981== HEAP SUMMARY:
==13981==     in use at exit: 0 bytes in 0 blocks
==13981==   total heap usage: 5 allocs, 5 frees, 5,560 bytes allocated
==13981== 
==13981== All heap blocks were freed -- no leaks are possible
==13981== 
==13981== For lists of detected and suppressed errors, rerun with: -s
==13981== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
pi@raspberrypi:/tmp $ 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM