Reading n characters at a time

So i have a file that has the following:

ABCDE
LMNOP

and i want to read 3 characters at a time. So for example i want to output:
ABC
BCD
CDE
DEL
ELM
LMN
...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Helper function to get a k-mer (it ignores newline characters)
void read(std::ifstream & in, std::string & string, int streamsize){
        int pos=0;
        // Read streamsize number of characters from stream discarting 
        // newline characters
        while(pos<streamsize){
                in >> string[pos];
                ++pos;
        }
}


 void read_file(char * file, int kmer){
        // open file
        std::ifstream input( file );
        // check that file if okay for operations
        assert(input.good());

        // Current string(character) being read from file
        std::string cursor;
        cursor.resize( kmer );

        // Position in stream
        int charPos=1;
        // Read file
        while( !input.eof() ){
                // Get a kmer from the file stream
                read( input, cursor, kmer );

                // If stream has not ended
                if (!input.eof()){
                        std::cout<< cursor <<'\n';
                } 
                else { break; }

                // Save place in stream
                input.seekg( charPos++ );
        }

        // Close file
        input.close();
}


But this outputs:
ABC
BCD
CDE
DEL
ELM
LMN
LMN
MNO
NOP

The problem is that I don't know how to avoid the function seekg step back but avoid newline chaaracters. Does anyone know how to overcome this?
After you go back in the stream check if the next thing in the stream is a newline character by adding:

'if (input.peek() == '\n') ++charPos;'
Topic archived. No new replies allowed.