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.
#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 = newchar[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.