reading data file from C program 
Author Message
 reading data file from C program

Hi, thanks everybody who answerd my first question, still cannot get
through.
Below I post my program and fragment of the data file which I tryed to read.
When I try to run this program, it returns all zeros for "momentum" and
"observable". Can anyone give me a clue why?

------------------ program---------------
#include <stdio.h>
#include <stdlib.h>

int main (void)
{
  float momentum = 0., observable = 0.;
  int count = 0, k = 0, j = 0;
  int lineSize = 0;
  char line[32];
  FILE *fp;

  fp = fopen ("test.plt","r");
  if (fp == NULL) {
    printf ("Cannot open file.\n");
    exit (1);
  }
  do {
    lineSize = 32;
    fgets (line,lineSize,fp);
    count += 1;
    printf ("%s\n",line);
    printf ("Line number %i, line length %i\n", count, strlen(line));
    k = sscanf (line,"%15.5E%15.5E", &momentum, &observable);
    printf ("Momentum %E, value %E, k = %d\n",momentum,observable,k);

  } while (count <= 10);

  fclose (fp);
  return 0;

Quote:
}

----------------------------------------

---------------- test.plt-------------------
   -1.40000E+01    2.95878E+02
   -1.20000E+01    2.97528E+02
   -1.00000E+01    2.98939E+02
   -8.00000E+00    3.00108E+02
   -6.00000E+00    3.01032E+02
   -4.00000E+00    3.01708E+02
   -2.00000E+00    3.02135E+02
    0.00000E+00    3.02310E+02
    2.00000E+00    3.02234E+02
    4.00000E+00    3.01906E+02
    6.00000E+00    3.01328E+02
    8.00000E+00    3.00502E+02



Sat, 24 Aug 2002 03:00:00 GMT  
 reading data file from C program

Quote:

>    k = sscanf (line,"%15.5E%15.5E", &momentum, &observable);

You are confusing scanf and printf conversion specifiers.

In scanf, one does not specify a precision. Moreover, the field width
specification, while supported, is largely useless for scanning numeric data.
It's biggest use is limiting the number of characters read into an array.

Try

    sscanf(string, "%f%f", &momentum, &observable);

Also, you should use double rather than float. The float type is primarily
useful as a means of saving space in large arrays, nothing more.



Sun, 25 Aug 2002 03:00:00 GMT  
 reading data file from C program

Quote:

> Hi, thanks everybody who answerd my first question, still cannot get
> through.
> Below I post my program and fragment of the data file which I tryed to read.
> When I try to run this program, it returns all zeros for "momentum" and
> "observable". Can anyone give me a clue why?

Below are the changes and resulting code to make all the diagnostics my
compiler produces disappear.  Amazingly, once the compiler finds nothing
to complain about your symptom disappears as well.  Perhaps there is a
lesson here about turning your diagnostics on.

   ** changes **

2a3

Quote:
> #include <string.h>

7,8c8,9
<       int count = 0, k = 0, j = 0;
<       int lineSize = 0;
---
Quote:
>     int count = 0, k;
>     int lineSize;

15c16
<               exit(1);
---
Quote:
>         exit(EXIT_FAILURE);

22,23c23,24
<               printf("Line number %i, line length %i\n", count,
strlen(line));
<               k = sscanf(line, "%15.5E%15.5E", &momentum,
&observable);
---

Quote:
>         printf("Line number %i, line length %d\n", count, (int)strlen(line));
>         k = sscanf(line, "%15E%15E", &momentum, &observable);

  ** resulting code **

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

int main(void)
{
    float momentum = 0., observable = 0.;
    int count = 0, k;
    int lineSize;
    char line[32];
    FILE *fp;

    fp = fopen("test.plt", "r");
    if (fp == NULL) {
        printf("Cannot open file.\n");
        exit(EXIT_FAILURE);
    }
    do {
        lineSize = 32;
        fgets(line, lineSize, fp);
        count += 1;
        printf("%s\n", line);
        printf("Line number %i, line length %d\n", count,
(int)strlen(line));
        k = sscanf(line, "%15E%15E", &momentum, &observable);
        printf("Momentum %E, value %E, k = %d\n", momentum, observable,
               k);

    } while (count <= 10);

    fclose(fp);
    return 0;

Quote:
}

> ------------------ program---------------
> #include <stdio.h>
> #include <stdlib.h>

> int main (void)
> {
>   float momentum = 0., observable = 0.;
>   int count = 0, k = 0, j = 0;
>   int lineSize = 0;
>   char line[32];
>   FILE *fp;

>   fp = fopen ("test.plt","r");
>   if (fp == NULL) {
>     printf ("Cannot open file.\n");
>     exit (1);
>   }
>   do {
>     lineSize = 32;
>     fgets (line,lineSize,fp);
>     count += 1;
>     printf ("%s\n",line);
>     printf ("Line number %i, line length %i\n", count, strlen(line));
>     k = sscanf (line,"%15.5E%15.5E", &momentum, &observable);
>     printf ("Momentum %E, value %E, k = %d\n",momentum,observable,k);

>   } while (count <= 10);

>   fclose (fp);
>   return 0;
> }
> ----------------------------------------

> ---------------- test.plt-------------------
>    -1.40000E+01    2.95878E+02
>    -1.20000E+01    2.97528E+02
>    -1.00000E+01    2.98939E+02
>    -8.00000E+00    3.00108E+02
>    -6.00000E+00    3.01032E+02
>    -4.00000E+00    3.01708E+02
>    -2.00000E+00    3.02135E+02
>     0.00000E+00    3.02310E+02
>     2.00000E+00    3.02234E+02
>     4.00000E+00    3.01906E+02
>     6.00000E+00    3.01328E+02
>     8.00000E+00    3.00502E+02

--

What one knows is, in youth, of little moment; they know enough who
know how to learn. - Henry Adams

A thick skin is a gift from God. - Konrad Adenauer
__________________________________________________________
Fight spam now!
Get your free anti-spam service: http://www.brightmail.com



Sun, 25 Aug 2002 03:00:00 GMT  
 reading data file from C program
Hi,

The mistake is in your control string of  "sscanf" .

it should be
k = sscanf (line,"%16E%16E", &momentum, &observable);

the number after the % is max field width to be read and  should be an
integer.It is not analogous to that of printf,sprintf,....etc where you
specify the precision.
The above 16  after the % is 15 digits + 1 dot"."
--
bye bye,
Srikanth

Quote:
> Hi, thanks everybody who answerd my first question, still cannot get
> through.
> Below I post my program and fragment of the data file which I tryed to
read.
> When I try to run this program, it returns all zeros for "momentum" and
> "observable". Can anyone give me a clue why?

> ------------------ program---------------
> #include <stdio.h>
> #include <stdlib.h>

> int main (void)
> {
>   float momentum = 0., observable = 0.;
>   int count = 0, k = 0, j = 0;
>   int lineSize = 0;
>   char line[32];
>   FILE *fp;

>   fp = fopen ("test.plt","r");
>   if (fp == NULL) {
>     printf ("Cannot open file.\n");
>     exit (1);
>   }
>   do {
>     lineSize = 32;
>     fgets (line,lineSize,fp);
>     count += 1;
>     printf ("%s\n",line);
>     printf ("Line number %i, line length %i\n", count, strlen(line));
>     k = sscanf (line,"%15.5E%15.5E", &momentum, &observable);
>     printf ("Momentum %E, value %E, k = %d\n",momentum,observable,k);

>   } while (count <= 10);

>   fclose (fp);
>   return 0;
> }
> ----------------------------------------

> ---------------- test.plt-------------------
>    -1.40000E+01    2.95878E+02
>    -1.20000E+01    2.97528E+02
>    -1.00000E+01    2.98939E+02
>    -8.00000E+00    3.00108E+02
>    -6.00000E+00    3.01032E+02
>    -4.00000E+00    3.01708E+02
>    -2.00000E+00    3.02135E+02
>     0.00000E+00    3.02310E+02
>     2.00000E+00    3.02234E+02
>     4.00000E+00    3.01906E+02
>     6.00000E+00    3.01328E+02
>     8.00000E+00    3.00502E+02



Sun, 25 Aug 2002 03:00:00 GMT  
 reading data file from C program
Thanks to everybody!
That made it!

Sergey



Sun, 25 Aug 2002 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Newbie: separate big .cs file into small .cs files

2. reading fortran data file from C program

3. program can't read its own data files

4. Can't Read Dbase data file from C program [HELP]

5. Reading file..Saving data in new file...

6. Movie/Sound files, reading data embedded with another file

7. resx files needed for cs - files ??

8. CS files display in VS6 like CPP files ?

9. How to read a MS Word file ( .doc ) file from C program

10. Read data on serial port from C program

11. How to properly read data from a CSV file

12. Reading text file data into an array

 

 
Powered by phpBB® Forum Software