dat file

R

Robert desouza

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
 
V

Vyacheslav Kononenko

Robert said:
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%.
 
M

Mike Wahler

Robert desouza said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top