Looping substring search

I'm just playing with substrings for the first time. At this point all I'm trying to do it take a command line argument and output the result.
It works but it prints it out multiple times. I assume there is a problem with the for loop, but I can't figure out what it is.

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
 #include <iostream>
#include <fstream>
#include <vector>
#include <string>

char* getFile( char* fileName ){
    std::fstream inFile( fileName );
    if( !inFile ) std::cout << "Could not open " << fileName << ".\n";
    else{
        inFile.seekg(0,inFile.end);
        int len = inFile.tellg();
        inFile.seekg(0,inFile.beg);
        char* buffer = new char[len];
        inFile.read( buffer, len);
        inFile.close();
        return buffer;
        }

    }
int main(int argc, char** argv){
    if(argc != 2) std::cout << "Parameter required\n";
    else{
        std::string f = getFile( argv[1] );
        size_t start = 0;
        size_t end = 1;
        while( end <= f.length() ){
            end = f.find( 0x0A, start );
            std::cout << f.substr(start,end)<<std::endl;
            start = ( end + 1 );
            }
    }
}



Update: I found a few mistakes and replaced the for loop with what I thought was a much more simple while loop. However I'm still getting some very odd behavior. Rather than looping the entire sequence over and over it seems to loop through portions of the sequence. It's like a data race with one process.

Last edited on
Topic archived. No new replies allowed.