>any idea how you would be able to save the String to a text file and be able to load it back up without any problems?
An std::string is simply a list of characters, if you use just about any output routine to write to the text file then it should work:
fout<<Test;
If it doesn't then maybe the problem is not in how you read and write or in the data type:
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
int main()
{
// Write a string to file
std::ofstream out ( "info.txt" );
if ( !out.is_open() ) {
std::cerr<<"Error opening file for writing"<<std::endl;
exit ( EXIT_FAILURE );
}
std::string test = "This is a test";
out<<test;
out.close();
// Read the string back
std::ifstream in ( "info.txt" );
if ( !in.is_open() ) {
std::cerr<<"Error opening file for reading"<<std::endl;
exit ( EXIT_FAILURE );
}
std::string input;
std::getline ( in, input );
std::cout<<"The string we saved is: "<< input <<std::endl;
// Start over, but read into an array
in.seekg ( 0, std::ios::beg );
char a_input[15];
in.getline ( a_input, 15 );
std::cout<<"The string we saved is: "<< input <<std::endl;
return EXIT_SUCCESS;
}
-Prelude