A
A
Arti-Jack2018-06-05 16:28:42
C++ / C#
Arti-Jack, 2018-06-05 16:28:42

Why are numbers written in one line with separation not readable?

I have a text file.

Количество точек N:
21
Данные входного сигнала:
          tнач            tкон              U 
        -10.000          10.000         -30.000 
Данные предаточной характеристики:
           Up1             Up2
        -10.000        -100.000 
 
   №            t               Uvx            Uvix 
   1         -10.000            0.000            0.000
   2          -9.000           -3.000          -30.000
   3          -8.000           -6.000          -60.000
   4          -7.000           -9.000          -90.000
   5          -6.000          -12.000         -100.000
   6          -5.000          -15.000         -100.000
   7          -4.000          -18.000         -100.000
   8          -3.000          -21.000         -100.000
   9          -2.000          -24.000         -100.000
  10          -1.000          -27.000         -100.000
  11           0.000          -30.000         -100.000
  12           1.000          -27.000         -100.000
  13           2.000          -24.000         -100.000
  14           3.000          -21.000         -100.000
  15           4.000          -18.000         -100.000
  16           5.000          -15.000         -100.000
  17           6.000          -12.000         -100.000
  18           7.000           -9.000          -90.000
  19           8.000           -6.000          -60.000
  20           9.000           -3.000          -30.000
  21          10.000            0.000            0.000

I need to get all the data from it. Including, transfer data from the table to arrays.
I use scanf to read numbers from a file:
FILE *f = fopen(File_Name, "r");
    //Подсчет кол-ва точек N
    int N = 0;
    char Sbuf[100];
    fgets(Sbuf, 100, f);
    fscanf(f, "%d", &N);

    fgets(Sbuf, 100, f);
    fgets(Sbuf, 100, f);
    fgets(Sbuf, 100, f);
    fgets(Sbuf, 100, f);
    fscanf(f, "%f%f%f\n", tn, tk, U);
    printf("%f\n", tn);
    fgets(Sbuf, 100, f);
    fgets(Sbuf, 100, f);
    fscanf(f, "%f%f", U1, U2);
    fgets(Sbuf, 100, f);

    fgets(Sbuf, 100, f);
    fgets(Sbuf, 100, f);
    int buf;
    for (int i = 0; i < N; i++)
        fscanf(f, "%d%f%f%f", &buf, &t[i], &Uvx[i], &Uvix[i]);

But for some reason it doesn't read. What is the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Dubrovin, 2018-06-05
@z3apa3a

For example, because after
fscanf(f, "%d", &N);
remains \n in the input buffer.
It's better not to mix fgets and fscanf, always read from the file with fgets, and parse the line read into the buffer with sscanf.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question