Questions

Discussion in 'C Programming' started by Tinker, Oct 2, 2004.

  1. Tinker

    Tinker Guest

    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!
     
    Tinker, Oct 2, 2004
    #1
    1. Advertising

  2. Tinker

    -berlin.de Guest

    Tinker <> 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 the
    > 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 */ ;

    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 ___ -berlin.de
    \__________________________ http://www.toerring.de
     
    -berlin.de, Oct 2, 2004
    #2
    1. Advertising

  3. Tinker

    j Guest

    <-berlin.de> wrote in message
    news:...
    > Tinker <> 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 the
    > > 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 */ ;


    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
     
    j, Oct 2, 2004
    #3
  4. Tinker

    CBFalconer Guest

    Tinker 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 the
    > txt file is 11 12 13 and i only want the programme to display the
    > 12 on the screen?


    What are hv numbers? High voltage?

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
     
    CBFalconer, Oct 3, 2004
    #4
  5. On Sun, 3 Oct 2004, CBFalconer wrote:
    >
    > Tinker 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 the
    >> txt file is 11 12 13 and i only want the programme to display the
    >> 12 on the screen?

    >
    > What are hv numbers? High voltage?


    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
     
    Arthur J. O'Dwyer, Oct 3, 2004
    #5
  6. Tinker

    j Guest

    "j" <> wrote in message
    news:N0F7d.129562$...

    < snip>

    > specifier that is followed by the apprioriate
    > conversion specifier for the value that he wants.
    >


    I meant appropriate. I certainly butchered that word.



    --
    j
     
    j, Oct 3, 2004
    #6
  7. Tinker

    Tinker Guest

    "j" <> wrote in message news:<N0F7d.129562$>...
    > <-berlin.de> wrote in message
    > news:...
    > > Tinker <> 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 the
    > > > 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 */ ;

    >
    > 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.



    Thanks
    But how about if i want to read the secound number of each line?
    Please help
     
    Tinker, Oct 3, 2004
    #7
  8. Tinker

    j Guest

    "Tinker" <> wrote in message
    news:...
    > "j" <> wrote in message

    news:<N0F7d.129562$>...
    > > <-berlin.de> wrote in message
    > > news:...
    > > > Tinker <> 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

    the
    > > > > 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 */ ;

    > >
    > > 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.

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


    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(stdin, "%*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.
     
    j, Oct 3, 2004
    #8
    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. Ali Syed
    Replies:
    3
    Views:
    572
    Mark McIntyre
    Oct 13, 2004
  2. pkirk25
    Replies:
    50
    Views:
    1,141
    Bill Pursell
    Oct 4, 2006
  3. Replies:
    10
    Views:
    565
  4. Vijay
    Replies:
    2
    Views:
    609
    Öö Tiib
    May 7, 2010
  5. reema
    Replies:
    0
    Views:
    294
    reema
    Aug 26, 2008
Loading...

Share This Page