Skip every two numbers

closed account (3796URfi)
Hi, I was wondering how to skip every two numbers read from a file?
For example I have a list of integer values (from a text file) say,
100 90 80 78 67 56 20 78 99 54 89 100 90 80
and I wanted to print
100 78 20 54 90 on the screen which means skipping two numbers
1
2
3
4
5
6
7
for(int i = 0, num; infile >> num; ++i)
{
    if(i%3 == 0)
    {
        std::cout << num << " " << std::flush;
    }
}
http://ideone.com/WPEHNG
Last edited on
closed account (E0M1hbRD)
Would this help?

for(int i(0); i != filesize; i+=3){ cout << i...}
Unless the file size is a perfect multiple of 3, that code will become an infinite loop and print garbage and (hopefully) crash.
closed account (E0M1hbRD)
right, thanks
closed account (3796URfi)
Thankyou for helping. Just one more question. If I wanted to read the file again from the beginning do I just close it and open again or is there another/better way?
Topic archived. No new replies allowed.