Questions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tinker

    #1

    Questions

    Does anyone know how to read a txt file which is created before and
    inside the files only hv numbers and did not structured.
    how can i only read the secound integer such as the patten inside the
    txt file is 11 12 13 and i only want the programme to display the 12
    on the screen?
    Thanks a lot!Please help!
  • Jens.Toerring@physik.fu-berlin.de

    #2
    Re: Questions

    Tinker <lty_hk@hotmail .com> wrote:[color=blue]
    > Does anyone know how to read a txt file which is created before and
    > inside the files only hv numbers and did not structured.
    > how can i only read the secound integer such as the patten inside the
    > txt file is 11 12 13 and i only want the programme to display the 12
    > on the screen?[/color]

    FILE *fp;
    int number;
    int count = 0;

    if ( ( fp = fopen( "file.dat", "r" ) ) == NULL )
    exit( EXIT_FAILURE );

    while ( fscanf( "%d", &number ) == 1 && ++count < 2 )
    /* empty */ ;

    if ( count != 2 )
    exit( EXIT_FAILURE );

    printf( "The number is %d\n", number );

    Take care, this will also pick up numbers embedded in text like
    "abc234xyz" .
    Regards, Jens
    --
    \ Jens Thoms Toerring ___ Jens.Toerring@p hysik.fu-berlin.de
    \______________ ____________ http://www.toerring.de

    Comment

    • j

      #3
      Re: Questions


      <Jens.Toerring@ physik.fu-berlin.de> wrote in message
      news:2s8it6F1if gctU1@uni-berlin.de...[color=blue]
      > Tinker <lty_hk@hotmail .com> wrote:[color=green]
      > > Does anyone know how to read a txt file which is created before and
      > > inside the files only hv numbers and did not structured.
      > > how can i only read the secound integer such as the patten inside the
      > > txt file is 11 12 13 and i only want the programme to display the 12
      > > on the screen?[/color]
      >
      > FILE *fp;
      > int number;
      > int count = 0;
      >
      > if ( ( fp = fopen( "file.dat", "r" ) ) == NULL )
      > exit( EXIT_FAILURE );
      >
      > while ( fscanf( "%d", &number ) == 1 && ++count < 2 )
      > /* empty */ ;[/color]

      Or simply eliminate the use of ``count''
      and use ``*'' along with a conversion
      specifier that is followed by the apprioriate
      conversion specifier for the value that he wants.



      --
      j


      Comment

      • CBFalconer

        #4
        Re: Questions

        Tinker wrote:[color=blue]
        >
        > Does anyone know how to read a txt file which is created before
        > and inside the files only hv numbers and did not structured. how
        > can i only read the secound integer such as the patten inside the
        > txt file is 11 12 13 and i only want the programme to display the
        > 12 on the screen?[/color]

        What are hv numbers? High voltage?

        --
        Chuck F (cbfalconer@yah oo.com) (cbfalconer@wor ldnet.att.net)
        Available for consulting/temporary embedded and systems.
        <http://cbfalconer.home .att.net> USE worldnet address!


        Comment

        • Arthur J. O'Dwyer

          #5
          Re: Questions


          On Sun, 3 Oct 2004, CBFalconer wrote:[color=blue]
          >
          > Tinker wrote:[color=green]
          >>
          >> Does anyone know how to read a txt file which is created before
          >> and inside the files only hv numbers and did not structured. how
          >> can i only read the secound integer such as the patten inside the
          >> txt file is 11 12 13 and i only want the programme to display the
          >> 12 on the screen?[/color]
          >
          > What are hv numbers? High voltage?[/color]

          No, no, the word "hv" is clearly being used as a verb in that sentence!
          I respectfully submit that it is intended as an abbreviation of "hover"
          --- i.e., the numbers are actually hovering inside the files. (The OP
          is obviously going to be interested in the 'float' datatype.)

          -Arthur

          Comment

          • j

            #6
            Re: Questions


            "j" <jake30NOSPAM@b ellsouth.net> wrote in message
            news:N0F7d.1295 62$Np2.43016@bi gnews4.bellsout h.net...

            < snip>
            [color=blue]
            > specifier that is followed by the apprioriate
            > conversion specifier for the value that he wants.
            >[/color]

            I meant appropriate. I certainly butchered that word.



            --
            j


            Comment

            • Tinker

              #7
              Re: Questions

              "j" <jake30NOSPAM@b ellsouth.net> wrote in message news:<N0F7d.129 562$Np2.43016@b ignews4.bellsou th.net>...[color=blue]
              > <Jens.Toerring@ physik.fu-berlin.de> wrote in message
              > news:2s8it6F1if gctU1@uni-berlin.de...[color=green]
              > > Tinker <lty_hk@hotmail .com> wrote:[color=darkred]
              > > > Does anyone know how to read a txt file which is created before and
              > > > inside the files only hv numbers and did not structured.
              > > > how can i only read the secound integer such as the patten inside the
              > > > txt file is 11 12 13 and i only want the programme to display the 12
              > > > on the screen?[/color]
              > >
              > > FILE *fp;
              > > int number;
              > > int count = 0;
              > >
              > > if ( ( fp = fopen( "file.dat", "r" ) ) == NULL )
              > > exit( EXIT_FAILURE );
              > >
              > > while ( fscanf( "%d", &number ) == 1 && ++count < 2 )
              > > /* empty */ ;[/color]
              >
              > Or simply eliminate the use of ``count''
              > and use ``*'' along with a conversion
              > specifier that is followed by the apprioriate
              > conversion specifier for the value that he wants.[/color]


              Thanks
              But how about if i want to read the secound number of each line?
              Please help

              Comment

              • j

                #8
                Re: Questions


                "Tinker" <lty_hk@hotmail .com> wrote in message
                news:673df668.0 410030919.4ae86 e29@posting.goo gle.com...[color=blue]
                > "j" <jake30NOSPAM@b ellsouth.net> wrote in message[/color]
                news:<N0F7d.129 562$Np2.43016@b ignews4.bellsou th.net>...[color=blue][color=green]
                > > <Jens.Toerring@ physik.fu-berlin.de> wrote in message
                > > news:2s8it6F1if gctU1@uni-berlin.de...[color=darkred]
                > > > Tinker <lty_hk@hotmail .com> wrote:
                > > > > Does anyone know how to read a txt file which is created before and
                > > > > inside the files only hv numbers and did not structured.
                > > > > how can i only read the secound integer such as the patten inside[/color][/color][/color]
                the[color=blue][color=green][color=darkred]
                > > > > txt file is 11 12 13 and i only want the programme to display the 12
                > > > > on the screen?
                > > >
                > > > FILE *fp;
                > > > int number;
                > > > int count = 0;
                > > >
                > > > if ( ( fp = fopen( "file.dat", "r" ) ) == NULL )
                > > > exit( EXIT_FAILURE );
                > > >
                > > > while ( fscanf( "%d", &number ) == 1 && ++count < 2 )
                > > > /* empty */ ;[/color]
                > >
                > > Or simply eliminate the use of ``count''
                > > and use ``*'' along with a conversion
                > > specifier that is followed by the apprioriate
                > > conversion specifier for the value that he wants.[/color]
                >
                >
                > Thanks
                > But how about if i want to read the secound number of each line?
                > Please help[/color]

                Then you would want to exclude everything that follows
                the second number up to a new line.

                so you can use,

                #include <stdio.h>

                int main(void)
                {
                int num;

                while((fscanf(s tdin, "%*s %d %*[^\n]", &num)) == 1)
                printf("Number: %d\n", num);

                return 0;
                }

                or with a different variation of the format string:
                fscanf(stdin, "%*d %d %*[^\n]", &num);
                fscanf(stdin, "%*[^ ] %d %*[^\n]", &num);

                You need to take into account your programs behaviour
                if your file will have spurious data. e.g., ``aaaaa 12 1000''
                or whitespace that proceeds the second number.
                Then construct the appropriate format string
                to attain the desired behaviour that you want for
                your program.



                Comment

                Working...