Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C++
Memory errors HELP!
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Daniel T., post: 2544102"] I gave you a way to fix, test if document and edition is NULL before trying to load them in a string. The above shouldn't work, swap takes a non-const reference, and you are sending it a temporary. Why not just out.clear()? What is the above for? Why the extra parens? Don't use a stringvec here. Just change your upper_case function to output a string or modify the cstring in place. Below is my version of your program. Look it over carefully, and ask questions about *any* line you don't understand. #include <string> #include <iostream> #include <fstream> using namespace std; const char* const movex_query = "movex.dat"; const char* const request_list = "request_list.txt"; // strips whitespace off front and back of string, // and converts string to uppercase. void clean( string& str ) { const char* const whitespace = " \t"; str.erase( 0, str.find_first_not_of( whitespace ) ); // strips whitespace off front // strip whitespace off back string::size_type pos = str.find_last_not_of( whitespace ); if ( pos != string::npos ) str.erase( pos + 1 ); // convert to uppercase for ( int i = 0; i < str.length(); ++i ) str[i] = toupper( static_cast<unsigned char>( str[i] ) ); } int main() { ifstream inf( movex_query ); ofstream out( request_list ); if ( inf && out ) { int lines_processed = 0; string document; string edition; // the below will break if there is a line with no commas, // should I guard against that? while ( getline( inf, document, ',' ) && getline( inf, edition ) ) { clean( document ); try { edition.erase( edition.find( ',' ) ); } catch (...) { } // above is for lines with extra commas: strips garbage clean( edition ); if ( document == "" || edition == "" ) cout << "Bad record: " << document << ", " << edition << '\n'; else if ( document == edition ) { out << document << ", " << edition << '\n'; } ++lines_processed; } cout << "Total lines processed: " << lines_processed << "\nProcessing complete!\n"; } else { // below is probably more fancy than necessary, // but the output looks good. cout << "Could not open file" << ( ( !inf && !out ) ? "s" : "" ) << ": "; if ( !inf ) cout << movex_query << ( ( !out ) ? ", and " : "" ); if ( !out ) cout << request_list; cout << '\n'; } }[/i][/i] [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C++
Memory errors HELP!
Top