Skip every two numbers

Oct 20, 2013 at 6:32pm
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
Oct 20, 2013 at 6:34pm
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 Oct 20, 2013 at 6:37pm
Oct 20, 2013 at 6:43pm
closed account (E0M1hbRD)
Would this help?

for(int i(0); i != filesize; i+=3){ cout << i...}
Oct 20, 2013 at 6:45pm
Unless the file size is a perfect multiple of 3, that code will become an infinite loop and print garbage and (hopefully) crash.
Oct 20, 2013 at 7:00pm
closed account (E0M1hbRD)
right, thanks
Oct 20, 2013 at 7:05pm
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?
Oct 20, 2013 at 7:13pm
Topic archived. No new replies allowed.