samedi 21 février 2015

.txt file to std::string

I'm trying to get a string to copy everything on a .txt file. I don't care about returns or anything fancy, I just need everything ever typed on the file.


Here I have a .txt file called Default.txt.



x:-100
y:-30
z:30
w:100


It is in a folder located in my project folder, where I keep all my .cpp and .h files.



Macintosh HD/.../Project/Text_Files/Default.txt


Here is my code for putting it into a string (well, technically it's not my code).



using namespace std;

string TextFileToString(string filename)
{
string line;
stringstream dosString;
ifstream inFile;
inFile.open (filename.c_str());

while(std::getline(inFile, line))
{
dosString<<line<<"\r\n";
}

return(dosString.str());
}

// In my main function

string s = TextFileToString("Default.txt");
cout << s;


Why won't this work? I've looked up several other solutions and they all are something like this. Is it because the file path needs to be more than "Default.txt"? I don't know. Please help!


Aucun commentaire:

Enregistrer un commentaire