dat file

Discussion in 'C++' started by Robert desouza, Sep 28, 2004.

  1. Hello Friends,

    i am new to this group,i need some help regarding c++.i am having dat
    file which contains 25450 random integers such as 16 7 20 243 37 297
    3402 702 21405 304.............
    my question is,how to read alternate integer for example
    7,243,297,702..... from this dat file.anyones help will be
    appreciated.

    thanks in advance
    Robert desouza, Sep 28, 2004
    #1
    1. Advertising

  2. Robert desouza wrote:

    > Hello Friends,
    >
    > i am new to this group,i need some help regarding c++.i am having dat
    > file which contains 25450 random integers such as 16 7 20 243 37 297
    > 3402 702 21405 304.............
    > my question is,how to read alternate integer for example
    > 7,243,297,702..... from this dat file.anyones help will be
    > appreciated.
    >
    > thanks in advance

    You read every number and just throw away numbers that you do not need.
    You can use counter and operator%.

    --
    Regards,
    Slava
    Vyacheslav Kononenko, Sep 28, 2004
    #2
    1. Advertising

  3. Robert desouza

    Mike Wahler Guest

    "Robert desouza" <> wrote in message
    news:...
    > Hello Friends,
    >
    > i am new to this group,i need some help regarding c++.i am having dat
    > file which contains 25450 random integers such as 16 7 20 243 37 297
    > 3402 702 21405 304.............
    > my question is,how to read alternate integer for example
    > 7,243,297,702..... from this dat file.anyones help will be
    > appreciated.


    I'll assume the integers in your file are separated by
    whitespace character(s). All you need do is read each
    integer in succession, and 'throw away' the ones you
    don't want. Example:


    #include <cstdlib>
    #include <iostream>
    #include <fstream>

    int main()
    {
    std::ifstream input("file.dat");
    if(!input)
    {
    std::cerr << "Cannot open input file\n";
    return EXIT_FAILURE;
    }

    int one(0);
    int two(0);

    while(input >> one >> two)
    std::cout << two << '\n';

    if(!input.eof())
    std::cerr << "Error reading input file\n";


    return 0;
    }


    Input file 'file.dat':

    16 7 20 243 37 297
    3402 702 21405 304

    Output:

    7
    243
    297
    702
    304


    -Mike
    Mike Wahler, Sep 28, 2004
    #3
    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. Replies:
    0
    Views:
    511
  2. palmis

    file .dat into a window

    palmis, Jan 18, 2006, in forum: Java
    Replies:
    5
    Views:
    423
    palmis
    Jan 23, 2006
  3. Replies:
    20
    Views:
    3,972
    Oliver Wong
    Aug 14, 2006
  4. Jay

    DAT file compilation

    Jay, Sep 30, 2006, in forum: Python
    Replies:
    12
    Views:
    522
    Nick Craig-Wood
    Oct 2, 2006
  5. Vitaliy
    Replies:
    0
    Views:
    448
    Vitaliy
    Dec 8, 2006
Loading...

Share This Page