开发者

How to do read and substract 2 values in different rows in a file in C

开发者 https://www.devze.com 2023-04-11 19:40 出处:网络
I have a txt file like 19.39825343760206 , -99.20035242455288 19.402068553663323, -99.18884038346096 19.412830341813137, -99.18181299584194

I have a txt file like

19.39825343760206 , -99.20035242455288
19.402068553663323, -99.18884038346096
19.412830341813137, -99.18181299584194
19.420813944440887, -99.17626082318111
19.421929492669275, -99.17060940878673
19.423626831405098, -99.16279076950832
19.42586547013487,  -99.15465428846164
19.427341437102363, -99.14900153296276
19.42677988826859,  -99.14223767237945
19.426539585246708, -99.13745395260139
19.425789584581995, -99.13291163163467
19.42548983629902,  -99.12465713339134
19.428912245829306, -99.11943755464836
19.430273095215284, -99.11480135636612
19.42715425437469,  -99.10933367328926
19.423242338939442, -99.10226739363952

How could I read the second value of each row and then then subtract the value in the next row, and then print it, like:

(-99.20035242455288)  -  (-99.18884038346096)
(-99.18884038346096)  -  (-99.18181299584194)  
(-99.18181299584194)  -  (-99.17626082318111)
...
(-99.10933367328926)  -  (-99.10226739363952)
??

I was doing a function like the following but do not know how to keep the first value and then the value in the second row.... Also how to tell C that I want the second value (separated by a comma)

void read_second_value (const char* file_name)
{
  FILE* file = fopen (file_name, "r");
  double i        = 0;
  double i_plus_1 = 0;
  fscanf (file, "%f", &i);    

  while (!feof (file))
  {  
      printf ("%f ", i);
      fscanf (file, "%f", &i);      
    }
  fclose (file);        
}

but I have tried the solution like:

#include <stdio.h>
#include <stdlib.h>
int main()
{
  char* file_name = "C:\\test.txt";
  FILE* fp = NULL;
  double left_value, right_value = 0.0, prev_value = 0.0;

  if ((fp = fopen (file_name, "r")) == NULL) {
    printf ("FILE 开发者_如何转开发OPEN ERROR: %d\n", errno);
    exit (1);
  }
  if (fscanf (fp, "%lf, %lf", &left_value, &right_value) != 2)
    printf ("WARNING: failed to read two values...\n");
  else
    printf ("left_value: %lf, right_value: %lf ", left_value, right_value);
  while (!feof (fp))
  {
      prev_value = right_value;
      if (fscanf (fp, "%lf, %lf", &left_value, &right_value) != 2)
        printf ("WARNING: failed to read two values...\n");
      else {
        printf ("left_value: %lf, right_value: %lf ", left_value, right_value);
        printf ("%lf - %lf = %lf\n", prev_value, right_value, prev_value - left_value);
      }
    }
  fclose (fp);
  return 0;
}

but get infinite

WARNING: failed to read two values...


This sample code works ... as long as you fix the first line:

GOOD: xxx.00, yyy.00 BAD: xxx.00 , yyy.00

#include <stdio.h>
#include <errno.h>

int
main (int argc, char *argv[])
{
  FILE* fp = NULL;
  int ct = 0;
  double left_value, right_value = 0.0, prev_value = 0.0;

  if (argc != 2) {
    printf ("USAGE: %s <filename>\n", argv[0]);
    return 1;
  }

  if ((fp = fopen (argv[1], "r")) == NULL) {
    printf ("FILE OPEN ERROR: %d\n", errno);
    return 1;
  }

  ct = fscanf (fp, "%lf, %lf", &left_value, &right_value);
  if (ct != 2) {
    printf ("WARNING: failed to read two values...\n");
    return 1;
  }

  while (!feof (fp)) {
      prev_value = right_value;
      ct = fscanf (fp, "%lf, %lf", &left_value, &right_value);
      if (ct == 0) {
        printf ("End of file...\n");
      }
      else if (ct != 2) {
        printf ("WARNING: failed to read two values...\n");
      }
      else {
        printf ("left_value: %lf, right_value: %lf ",
          left_value, right_value);
        printf ("%lf - %lf = %lf\n",
          prev_value, right_value, prev_value - left_value);
      }
  }
  printf ("Closing file...\n");
  fclose (fp);
  return 0;
}

The first line has a "space-comma-space" after the first number; all the rest of the lines have a "comma-space".

If you want to use "scanf", you MUST have a consistent data set.

If you want more robust parsing, then you should:

1) "fgets()" each line

2) use functions like "strchr()" (preferred) or "strok()" (deprecated) to parse the individual tokens (3 tokens per line: 1st number, comma and 2nd number).


How about something like this:

void read_values (const char* file_name)
{
  FILE* fp = NULL;
  double left_value, right_value = 0.0, prev_value = 0.0;

  if ((fp = fopen (file_name, "r")) == NULL) {
    printf ("FILE OPEN ERROR: %d\n", errno);
    exit (1);
  }
  if (fscanf (fp, "%lf, %lf", &left_value, &right_value) != 2)
    printf ("WARNING: failed to read two values...\n");
  else
    printf ("left_value: %lf, right_value: %lf ", left_value, right_value);
  while (!feof (fp))
  {  
      prev_value = right_value;
      if (fscanf (fp, "%lf, %lf", &left_value, &right_value) != 2)
        printf ("WARNING: failed to read two values...\n");
      else {
        printf ("left_value: %lf, right_value: %lf ", left_value, right_value);
        printf ("%lf - %lf = %lf\n", prev_value, right_value, prev_value - left_value);
      }
    }
  fclose (fp);        
}

PS: I have not compiled or tested ... but I hope it will be useful to you ...

0

精彩评论

暂无评论...
验证码 换一张
取 消