Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

getline, my compiler, or my code is giving me a headache

MadifierMadifier Posts: 10Member
I'm sure this is a minor problem to many people, but I just don't know what is going on other than my compiler is haunted.

I have a program written in Dev-C++ that works in conjunction with two files. One file (srcP) is strictly an input file whereas the other is both an input and output (dst8). The input file has some universal data on it which is copied to the output AFTER a string of text called "//". The purpose of this, is that each of my output files has unique data before the designated string. For those that wonder why, I'm using c++ to make my angelscript code easier to work with.

The problem is getline seems to pick and choose where it wants to start and stop.
Here, I'm searching through output.txt for "//", so I can begin copying from input.txt.
"output.txt" {
hello world

//
I'm universal
texting test
}

Inside the compiler, I print out "if the file previously existed", "debug checkpoint 1", and the following code dst8.tellg, dst8.tellp, and the string "S" from getline( dst8, S ).

"debug_error.txt" {
entering... file did exist
debug point 1
0:0 hello world
19:19
26:26 auto-generated>
46:46 universal
60:60 xting test
73:73
}

this is my code in a nutshell.

std::ifstream src2("input.txt"); // one of a few inputs.
std::ifstream * srcP; // by changing pointer to different inputs, it makes my code much more condensed.
std::fstream dst8; // variable will open a file, modify it, close it, and move on to the the next file. I have like 100+ files to deal with.
// yes, angelscript is a pain at the moment.
std::ofstream debug("log file.txt", std::ios::out );

srcP = &src2; // I have multiple inputs to grab from.
dst8.open( str.c_str(), std::ios::in | std::ios::out | std::ios::ate ); // I do this to see if the file existed previously or not.

std::string searching_for( "//" ); // string to look for in files.
std::string s; // place to store getline data.

debug << "debug point " << 1 << "
"; // checkpoint

while( true ) { // I'm fully aware of while( getline( dst8,s ) ). I did this temporarily to isolate the problem.
debug << dst8.tellg() << ":" << dst8.tellp() << " ";
std::getline( dst8, s ); // delimiter is by default '
'
debug << s << "
";
if( s == searching_for ) { // if I found "//<auto-generated>", stop searching so that the file writing pointer knows where to begin.
debug << "found " << s << " at " << dst8.tellg() << "
";
dst8.tellp( dst8.tellg() );
break;
}
if( dst8.eof() ) { // if I did not find the string, something is wrong with the current file, so continue on with the remaining files.
break;
}
}



I hope this is easy enough to read. Something else I'd like info on is, if my input.txt is smaller than the output.txt, I'll end up with trash code at the end of my output.txt. Any suggestions to emptying the remaining output.txt after I'm done copying the input.txt into it? I thought about dst8 << ''; but that didn't appear to work when I opened the text file up to find everything was still there.

Comments

Sign In or Register to comment.