Hi i am trying to pass a whole file into a string. This is my Code but the program is always exiting on the first if(). I just can't get behind what i am doing wrong here.
#include <iostream>
#include <fstream>
#include <string>
std::string readFile (std::string filename);
int main() {
std::string filename;
std::string Eingabestring;
std::cout << "Geben Sie eine Datei an" << std::endl;
std::cin >> filename;
Eingabestring = readFile(filename);
std::cout << Eingabestring << std::endl;
return 0;
}
std::string readFile (std::string filename)
{
std::string zeile,inhalt ;
std::ifstream quelle;
quelle.open(filename.c_str());
if (!quelle)
{
std::cerr << filename << " kann nicht geöffnet werden!\n";
return exit(-1);
}
while (!quelle.eof())
{
getline(quelle,zeile);
inhalt = inhalt + zeile;
}
return inhalt;
}
Already thanks for your help!
Edit: I just noticed that i put the file into a wrong folder.. But the code still isn't reading the whole file. Just the first line, but i thought with the loop i could get every line of my file into the string?
And i fixed the second return 0 to exit(-1). Thats better right?