reading first line of a txt file with c

Discussion in 'C Programming' started by shekharpati@gmail.com, Jun 9, 2005.

  1. Guest

    hi all,
    I,m trying to read a space delimited file with the use of formated
    input through fscanf. But surprisingly i am able to read all the 4999
    lines of data except the "first line" which is giving random output(i.e
    not present in the file). the code is given below(the relevant part).

    sample input from the data file
    01 48554.73828 504 30.48.37.001953 76.15.33.632813
    2 48554.67969 504 30.48.36.722168 76.15.33.527344
    3 48554.63281 504 30.48.36.442383 76.15.33.421875
    4 48554.58594 504 30.48.36.162598 76.15.33.316406
    5 48554.54297 504 30.48.35.882813 76.15.33.210938

    the output(the first line)
    18001844.273438 135633.742190 44.273438 30 5000 14
    2 48554.67969 504 30.48.36.722168 76.15.33.527344
    3 48554.63281 504 30.48.36.442383 76.15.33.421875
    4 48554.58594 504 30.48.36.162598 76.15.33.316406
    5 48554.54297 504 30.48.35.882813 76.15.33.210938


    I got this prob before when i was only reading the first two columns
    but as you might have noticed the leading '0' before 1 solved it.
    Surprisingly i can still read the first two columns correctly but the
    later ones are the ones that get of of hand. That too only in the first
    line. rest 4999 lines are read just fine. I cannot figure it out. plz
    help.its important

    shashank
    (i am using borland c++ 5.5, win2000)
    _______________________________

    fp=fopen ("TEMP31.TXT","r");
    if (fp==NULL)
    { puts("cannot open file");exit(1);
    }
    while ((fgetc(fp))!=EOF)
    {

    fscanf(fp,"%d%lf%d%d.%d.%lf%d.%d.%lf",&x[a],&y[a],&fl_h[a],&latdeg[a],&latmin[a],&latsec[a],&longdeg[a],
    &longmin[a],&longsec[a]);

    printf ("\n%d %lf %d %d %lf %d %d %lf
    %d",x[a],y[a],latdeg[a],latmin[a],latsec[a],longdeg[a],longmin[a],longsec[a],fl_h[a]);//(here
    when i print it gives the correct values)

    if ((a%100)==0) {getch();} else{}
    a=a+1;
    }

    fclose(fp);

    getch();

    for (i=0;i<AR;i++)
    {
    lat=(latsec+(latmin*60)+(latdeg*3600));
    lon=(longsec+(longmin*60)+(longdeg*3600));
    printf ("\n%lf %lf %lf %d %d
    %d",lat,lon,latsec,latmin,latdeg,fl_h);// from here
    on i get junk value

    if ((i%100)==0) {getch();} else{}
    }
    getch();


    _______________________________

    this is the starting code i.e the code before the above given prog
    segment, the initialisations and all (though i dont think it will be
    neccessary but just to be sure)

    #include<stdio.h>
    #include<conio.h>
    #include<math.h>
    #include<stdlib.h>
    #define ARRAY 5000
    #define AR 4999
    #define PI 3.142857
    #define CONV 30.8333
    main()
    {
    clrscr();
    FILE *fp;
    static int
    x[AR],latdeg[AR],latmin[AR],longdeg[AR],longmin[AR],fl_h[AR];
    static double
    y[AR],longsec[AR],latsec[AR],lat_p[100],long_p[100],lat_m[100];
    static double
    long_m[100],lat[AR],lon[AR],dist_p[100],dist_m[100],p[100],m[100],hp[100],

    hm[100],lata,longa,latb,longb,latp,longp,latm,longm,dist_f,dist_i,pa,t,ta,tta,tta1,ttta,ttta1;
    int a=0,b=0,c=0,d=0,e=0,ca=0,da=0,f=0,g=0,i,j,z,sl=0,sl1=0;
    double
    sa=0.0,sa1=0,sb=0.0,sc=0.0,sd=0.0,se=0,sf=0,sg=0,sg1=0,sh=0,sh1=0,xa=0,xb=0,xp=0,xm=0,ya,yb,yp=0,ym=0;
    static float fl_p[100],fl_m[100],fl_hp,fl_hm,fl_ha,fl_hb;
    _________________***********_______________
    , Jun 9, 2005
    #1
    1. Advertising

  2. Guest

    hi,
    i'm sorry, the correct sample output is
    18001844.273438 135633.742190 44.273438 30 5000 14
    110916.722168 274533.527344 36.722168 48 30 504
    110916.442383 274533.421875 36.442383 48 30 504
    110916.162598 274533.316406 36.162598 48 30 504
    110915.882813 274533.210938 35.882813 48 30 504
    110915.603027 274533.105469 35.603027 48 30 504

    As you can see that in the code i have printed in different order than
    the input and most of all done away with some "." in formatted scanf.
    But still if you match except the first line the output is predictable.

    hope i am clear
    shashank
    , Jun 9, 2005
    #2
    1. Advertising

  3. Guest

    hi
    i'm sorry, the correct sample output code is

    18001844.273438 135633.742190 44.273438 30 5000 14
    110916.722168 274533.527344 36.722168 48 30 504
    110916.442383 274533.421875 36.442383 48 30 504
    110916.162598 274533.316406 36.162598 48 30 504
    110915.882813 274533.210938 35.882813 48 30 504
    110915.603027 274533.105469 35.603027 48 30 504

    as you can see in the code that i have printed it in a different order
    than input and done away with some "."s(don't worry its intentional)
    but if u match the output is predictable except the first line as i
    said earlier.
    plz help
    shashank
    , Jun 9, 2005
    #3
  4. CBFalconer Guest

    wrote:
    >

    .... snip ...
    >
    > As you can see that in the code i have printed in different order than
    > the input and most of all done away with some "." in formatted scanf.
    > But still if you match except the first line the output is predictable.
    >
    > hope i am clear


    No, you are not. Why do you want to make it hard for anyone
    willing to help you? Put together a compilable complete source,
    limiting line length to 65 or even 72 chars, with proper
    indentation, together with a clear sample of the input. all in one
    message.

    And get rid of the non-standard portions, such as getch.

    Then someone may decide to cut and paste and compile.

    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
    CBFalconer, Jun 9, 2005
    #4
  5. pete Guest

    [ot]Re: reading first line of a txt file with c

    wrote:

    > #define PI 3.142857


    #define PI 3.14159265

    I consider most mnemonics for PI digits, to be idiotic.

    shekharpati is using twenty two sevenths.
    To use that, you have to remember a two digit number,
    a one digit number, an operation, and the fact that it
    only gets you three significant figures.

    --
    pete
    pete, Jun 10, 2005
    #5
  6. On 9 Jun 2005 04:41:22 -0700, wrote:

    >hi all,
    >I,m trying to read a space delimited file with the use of formated
    >input through fscanf. But surprisingly i am able to read all the 4999
    >lines of data except the "first line" which is giving random output(i.e
    >not present in the file). the code is given below(the relevant part).
    >
    >sample input from the data file
    >01 48554.73828 504 30.48.37.001953 76.15.33.632813
    >2 48554.67969 504 30.48.36.722168 76.15.33.527344
    >3 48554.63281 504 30.48.36.442383 76.15.33.421875
    >4 48554.58594 504 30.48.36.162598 76.15.33.316406
    >5 48554.54297 504 30.48.35.882813 76.15.33.210938


    I don't get it. What kind of number is 30.48.35.882813? Without
    proper spacing, it becomes a tedious exercise to guess what you want.
    --

    Best wishes,

    Bob
    Robert W Hand, Jun 10, 2005
    #6
  7. CBFalconer Guest

    Re: [ot]Re: reading first line of a txt file with c

    pete wrote:
    > wrote:
    >
    > > #define PI 3.142857

    >
    > #define PI 3.14159265
    >
    > I consider most mnemonics for PI digits, to be idiotic.
    >
    > shekharpati is using twenty two sevenths.
    > To use that, you have to remember a two digit number,
    > a one digit number, an operation, and the fact that it
    > only gets you three significant figures.


    355/113 gives you seven sig. digits. Some other interesting nums:

    312689 / 99532 = 3.141592653618936 error 0.000000000029144
    833719 / 265381 = 3.141592653581078 error 0.000000000008715
    1146408 / 364913 = 3.141592653591404 error 0.000000000001611
    4272943 / 1360120 = 3.141592653589389 error 0.000000000000404
    5419351 / 1725033 = 3.141592653589815 error 0.000000000000022
    58466453 / 18610450 = 3.141592653589784 error 0.000000000000009
    74724506 / 23785549 = 3.141592653589791 error 0.000000000000002
    80143857 / 25510582 = 3.141592653589793 error 0.000000000000000

    Output from ratpi.c, following:

    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <float.h>

    /* Public domain by C.B Falconer, 2003-06-04 */
    int main(int argc, char **argv)
    {
    int num, approx, bestnum, bestdenom;
    int lastnum = 500;
    double error, leasterr, pi, criterion;

    pi = 4 * atan(1.0);
    criterion = 2 * pi * DBL_EPSILON;
    if (argc > 1) lastnum = strtol(argv[1], NULL, 10);
    if (lastnum <= 0) lastnum = 500;
    printf("Usage: ratpi [maxnumerator]\n"
    "Rational approximation to PI = %.*f\n", DBL_DIG, pi);
    for (leasterr = pi, num = 3; num < lastnum; num++) {
    approx = (int)(num / pi + 0.5);
    error = fabs((double)num / approx - pi);
    if (error < leasterr) {
    bestnum = num;
    bestdenom = approx;
    leasterr = error;
    printf("%8d / %-8d = %.*f error %.*f\n",
    bestnum, bestdenom,
    DBL_DIG, (double)bestnum / bestdenom,
    DBL_DIG, leasterr);
    if (leasterr <= criterion) break;
    }
    }
    return 0;
    } /* main */

    My machine takes 25 sec. to evaluate ratpi 99999999. Probably a
    useful benchmark. Here it is virtually immune to gcc optimization,
    except -Os which bumps it to 31 sec.

    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
    CBFalconer, Jun 11, 2005
    #7
  8. pete Guest

    Re: [ot]Re: reading first line of a txt file with c

    CBFalconer wrote:
    >
    > pete wrote:
    > > wrote:
    > >
    > > > #define PI 3.142857

    > >
    > > #define PI 3.14159265
    > >
    > > I consider most mnemonics for PI digits, to be idiotic.
    > >
    > > shekharpati is using twenty two sevenths.
    > > To use that, you have to remember a two digit number,
    > > a one digit number, an operation, and the fact that it
    > > only gets you three significant figures.

    >
    > 355/113 gives you seven sig. digits. Some other interesting nums:


    But knowing 3.141593 is easier;
    you don't have to remember that it yields seven digits,
    you can just look at it.

    > 312689 / 99532 = 3.141592653618936 error 0.000000000029144
    > 833719 / 265381 = 3.141592653581078 error 0.000000000008715
    > 1146408 / 364913 = 3.141592653591404 error 0.000000000001611
    > 4272943 / 1360120 = 3.141592653589389 error 0.000000000000404
    > 5419351 / 1725033 = 3.141592653589815 error 0.000000000000022
    > 58466453 / 18610450 = 3.141592653589784 error 0.000000000000009
    > 74724506 / 23785549 = 3.141592653589791 error 0.000000000000002
    > 80143857 / 25510582 = 3.141592653589793 error 0.000000000000000


    Those are all good examples to make my point.
    Each formula requires memorizing as many or more digits,
    than it yields,
    plus it also requires memorizing how many digits are yielded.

    --
    pete
    pete, Jun 11, 2005
    #8
  9. Re: [ot]Re: reading first line of a txt file with c

    On Fri, 10 Jun 2005, pete wrote:

    > wrote:
    >
    > > #define PI 3.142857

    >
    > #define PI 3.14159265
    >
    > I consider most mnemonics for PI digits, to be idiotic.
    >
    > shekharpati is using twenty two sevenths.
    > To use that, you have to remember a two digit number,
    > a one digit number, an operation, and the fact that it
    > only gets you three significant figures.


    1. Go to: http://www.gutenberg.org/

    2. Follow the link labelled "Advanced Search".

    3. In the form provided, enter "50" (without the quotes) in the
    "EText-No.:" field and submit a search with the "Go!" button:

    EText-No.: 50____ Go!

    4. From the page you get, download the text (about 1.4MB) or zipped
    (819200 bytes) copy of pimil10.txt and then ...

    5. Enjoy the luxury of having Pi available to more than a million
    decimal places.

    --
    Can you Change: *alchemy to alchemy* (* == Unicorn)
    mindworks mindworks
    in 103 moves? Try http://www.chebucto.ns.ca/~af380/AMPuzzle.html
    (Requires Firefox or a browser with a compatable version of JavaScript)
    Norman L. DeForest, Jun 11, 2005
    #9
  10. pete Guest

    Re: [ot]Re: reading first line of a txt file with c

    Norman L. DeForest wrote:

    > 5. Enjoy the luxury of having Pi available to more than a million
    > decimal places.


    I like Ooura's pi_fftc.c program.

    http://momonga.t.u-tokyo.ac.jp/~ooura/pi_fft.html

    --
    pete
    pete, Jun 11, 2005
    #10
  11. Re: [ot]Re: reading first line of a txt file with c

    On Sat, 11 Jun 2005 01:21:19 +0000, pete wrote:

    > CBFalconer wrote:


    ....

    >> 355/113 gives you seven sig. digits. Some other interesting nums:

    >
    > But knowing 3.141593 is easier;

    ___
    355/113 is very easy to remember if you write it as 113|355

    i.e. the digits 113355 split by a division.

    Lawrence
    Lawrence Kirby, Jun 11, 2005
    #11
  12. Re: [ot]Re: reading first line of a txt file with c

    Lawrence Kirby <> writes:
    > On Sat, 11 Jun 2005 01:21:19 +0000, pete wrote:
    >
    >> CBFalconer wrote:

    >
    > ...
    >
    >>> 355/113 gives you seven sig. digits. Some other interesting nums:

    >>
    >> But knowing 3.141593 is easier;

    > ___
    > 355/113 is very easy to remember if you write it as 113|355
    >
    > i.e. the digits 113355 split by a division.


    I find it easier just to remember the first few digits of pi:

    3.1415926535897932384626433832795028841971693993751058209749445923078164

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Jun 11, 2005
    #12
  13. Re: [ot]Re: reading first line of a txt file with c

    On Sat, 11 Jun 2005 16:23:16 +0000, Keith Thompson wrote:

    > Lawrence Kirby <> writes:
    >> On Sat, 11 Jun 2005 01:21:19 +0000, pete wrote:
    >>
    >>> CBFalconer wrote:

    >>
    >> ...
    >>
    >>>> 355/113 gives you seven sig. digits. Some other interesting nums:
    >>>
    >>> But knowing 3.141593 is easier;

    >> ___
    >> 355/113 is very easy to remember if you write it as 113|355
    >>
    >> i.e. the digits 113355 split by a division.

    >
    > I find it easier just to remember the first few digits of pi:
    >
    > 3.1415926535897932384626433832795028841971693993751058209749445923078164


    I find it easier to look it up when I need it which is frankly very rarely.

    :)

    Lawrence
    Lawrence Kirby, Jun 11, 2005
    #13
  14. CBFalconer Guest

    Re: [ot] pi (was: reading first line of a txt file with c)

    Keith Thompson wrote:
    > Lawrence Kirby <> writes:
    >> On Sat, 11 Jun 2005 01:21:19 +0000, pete wrote:
    >>> CBFalconer wrote:

    >>
    >> ...
    >>
    >>>> 355/113 gives you seven sig. digits. Some other interesting nums:
    >>>
    >>> But knowing 3.141593 is easier;

    >> ___
    >> 355/113 is very easy to remember if you write it as 113|355
    >>
    >> i.e. the digits 113355 split by a division.

    >
    > I find it easier just to remember the first few digits of pi:
    >
    > 3.1415926535897932384626433832795028841971693993751058209749445923078164


    What do the Germans call it, after someone who calculated roughly
    that many digits by hand, and made a mistake halfway through :-(

    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
    CBFalconer, Jun 11, 2005
    #14
  15. Joe Wright Guest

    Re: [ot] pi

    CBFalconer wrote:
    > Keith Thompson wrote:
    >
    >>Lawrence Kirby <> writes:
    >>
    >>>On Sat, 11 Jun 2005 01:21:19 +0000, pete wrote:
    >>>
    >>>>CBFalconer wrote:
    >>>
    >>>...
    >>>
    >>>
    >>>>>355/113 gives you seven sig. digits. Some other interesting nums:
    >>>>
    >>>>But knowing 3.141593 is easier;
    >>>
    >>> ___
    >>>355/113 is very easy to remember if you write it as 113|355
    >>>
    >>>i.e. the digits 113355 split by a division.

    >>
    >>I find it easier just to remember the first few digits of pi:
    >>
    >>3.1415926535897932384626433832795028841971693993751058209749445923078164

    >
    >
    > What do the Germans call it, after someone who calculated roughly
    > that many digits by hand, and made a mistake halfway through :-(
    >

    Scheissdreck.. ? :)

    --
    Joe Wright
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
    Joe Wright, Jun 11, 2005
    #15
  16. Re: [ot]Re: reading first line of a txt file with c

    JRS: In article <>, dated Sat, 11 Jun 2005
    00:07:24, seen in news:comp.os.msdos.programmer, CBFalconer
    <> posted :

    borland.public (which you inherited) is not a newsgroup. Removed.

    >pete wrote:
    >> wrote:
    >>
    >> > #define PI 3.142857

    >>
    >> #define PI 3.14159265


    If possible, one should use a value for pi built into the system. That
    should be the very best float value for its resolution, and typo-free.
    I have met a well-known language in which no literal constant would give
    exactly that value.


    >355/113 gives you seven sig. digits. Some other interesting nums:
    >
    > 312689 / 99532 = 3.141592653618936 error 0.000000000029144
    > 833719 / 265381 = 3.141592653581078 error 0.000000000008715
    > 1146408 / 364913 = 3.141592653591404 error 0.000000000001611
    > 4272943 / 1360120 = 3.141592653589389 error 0.000000000000404
    > 5419351 / 1725033 = 3.141592653589815 error 0.000000000000022
    >58466453 / 18610450 = 3.141592653589784 error 0.000000000000009
    >74724506 / 23785549 = 3.141592653589791 error 0.000000000000002
    >80143857 / 25510582 = 3.141592653589793 error 0.000000000000000


    In javascript, I don't get your sixth and seventh of those eight.
    That's using IEEE doubles, and a different algorithm, so different
    rounding errors.

    > ... ...


    >My machine takes 25 sec. to evaluate ratpi 99999999. Probably a
    >useful benchmark. Here it is virtually immune to gcc optimization,
    >except -Os which bumps it to 31 sec.


    My javascript takes about a second to reach 245850922/78256779 - so
    evidently I have a different algorithm.
    <URL:http://www.merlyn.demon.co.uk/js-demos.htm#App> "Calc 0" button;
    Use "Show Functions" to see how.

    On looking briefly at yours, it is probably less susceptible to
    accumulating rounding errors than mine.

    But it's clearly better for testing execution speed.

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
    <URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
    Dr John Stockton, Jun 12, 2005
    #16
  17. Richard Bos Guest

    Re: [ot] pi (was: reading first line of a txt file with c)

    CBFalconer <> wrote:

    > Keith Thompson wrote:
    > > I find it easier just to remember the first few digits of pi:
    > >
    > > 3.1415926535897932384626433832795028841971693993751058209749445923078164

    >
    > What do the Germans call it, after someone who calculated roughly
    > that many digits by hand, and made a mistake halfway through :-(


    Nothing. The Germans (and the Dutch, too) called pi the Ludolphine
    number for a couple of centuries, after Ludolph van (or von?) Ceulen,
    who, living in Leiden, calculated the first 35 digits of pi. They were,
    however, all correct (and are engraved on his tombstone - he did, after
    all, spend decades on the calculation, in between teaching maths and
    fencing).
    The man who made a mistake was the Englishman William Shanks, who used
    Machin's formula to calculate 707 digits of pi, but made an error in the
    527th digit, and all digits after that had only 1/10th chance of being
    correct.

    Richard
    Richard Bos, Jun 13, 2005
    #17
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sameen
    Replies:
    2
    Views:
    425
    Victor Bazarov
    Aug 29, 2005
  2. Jochen Brenzlinger
    Replies:
    7
    Views:
    5,479
    Roedy Green
    Sep 15, 2011
  3. Richard Schneeman
    Replies:
    16
    Views:
    460
    Daniel Bush
    Aug 27, 2008
  4. Mmcolli00 Mom
    Replies:
    18
    Views:
    561
    Brian Candler
    Dec 22, 2008
  5. jobo

    How to read .txt file line by line

    jobo, Apr 17, 2007, in forum: Javascript
    Replies:
    1
    Views:
    81
    scripts.contact
    Apr 17, 2007
Loading...

Share This Page