How to read one text file and store that in one variable in C++?
Feb 4, 2018 at 5:34am UTC
I am trying to read one text file and store all data from that text file into one string variable but how can I do that?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
string ch;
ifstream file("E:\\textfile1.txt" );
if (!file)
{
cout << "Error in opening file!!!" << endl;
return -1;
}
while (!file.eof())
{
file >> noskipws >> ch;
cout << ch;
}
file.close();
How can I store all data in one variable? anyone give solution. Thanks in advance.
Last edited on Feb 4, 2018 at 5:37am UTC
Feb 4, 2018 at 5:49am UTC
How can I store all data in one variable? anyone give solution. Thanks in advance.
1 2 3 4 5 6 7 8 9
# include <fstream>
# include <sstream>
int main() {
std::ifstream file{"E:\\textfile1.txt" };
std::string const file_contents = static_cast <std::ostringstream&>
(std::ostringstream{} << file.rdbuf()).str();
}
Edit: the code I found the above in credits someone else:
/* Thanks, http://cpp.indi.frih.net */
Last edited on Feb 4, 2018 at 6:03am UTC
Feb 4, 2018 at 6:14am UTC
How to read the entire file by iterating every character in the file?
Feb 4, 2018 at 9:00am UTC
You could try one of these. The last one takes a bit of lateral thinking.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <iterator>
using namespace std;
int main()
{
string allFile;
ifstream file( "temp.cpp" );
copy( istream_iterator<char >{ file >> noskipws }, {}, back_inserter( allFile ) );
file.close();
cout << "File contents:\n" << allFile;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <iterator>
using namespace std;
int main()
{
char c;
string allFile;
ifstream file( "in.txt" );
file >> noskipws;
while ( file >> c ) allFile += c;
file.close();
cout << "File contents:\n" << allFile;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
#include <fstream>
#include <string>
#include <numeric>
#include <iterator>
using namespace std;
int main()
{
ifstream file( "in.txt" );
string allFile = accumulate( istream_iterator<char >{ file >> noskipws }, {}, string{} );
file.close();
cout << "File contents:\n" << allFile;
}
Feb 4, 2018 at 10:02am UTC
Another option:
1 2 3 4 5 6 7 8
string read_file(const char *filename)
{
stringstream buffer;
buffer << ifstream(filename).rdbuf();
return buffer.str();
}
Feb 4, 2018 at 2:08pm UTC
Directly accessing the stream buffer and performing unformatted input is the most straightforward way to read the entire contents of a text stream into a string. It also happens to be the most efficient way of doing so.
1 2 3 4 5
std::string contents_of( std::string path_to_file )
{
std::ifstream file(path_to_file) ;
return { std::istreambuf_iterator<char >(file), std::istreambuf_iterator<char >{} } ;
}
Feb 4, 2018 at 7:03pm UTC
Summary of methods above in a common form. My apologies if it doesn't reflect your intended style of coding.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <algorithm>
#include <numeric>
#include <iterator>
using namespace std;
//======================================================================
string readFile1( string filename ) // @JLBorges : direct from stream buffer
{
ifstream file( filename );
return { istreambuf_iterator<char >(file), {} };
}
//======================================================================
string readFile2( string filename ) // @mbozzi / @Thomas1965 : via stringstream buffer
{
stringstream buffer;
buffer << ifstream( filename ).rdbuf();
return buffer.str();
}
//======================================================================
string readFile3( string filename ) // @lastchance: copy / stream_iterator / back_inserter
{
ifstream file( filename );
string s;
copy( istream_iterator<char >{ file >> noskipws }, {}, back_inserter( s ) );
return s;
}
//======================================================================
string readFile4( string filename ) // @lastchance: just add chars
{
char c;
ifstream file( filename );
string s;
file >> noskipws; // needed to prevent loss of spaces and linefeeds
while ( file >> c ) s += c;
return s;
}
//======================================================================
string readFile5( string filename ) // @lastchance: add chars with accumulate
{
ifstream file( filename );
return accumulate( istream_iterator<char >{ file >> noskipws }, {}, string{} );
}
//======================================================================
void write( string s )
{
cout << "File contents:\n" << s << "\n\n" ;
}
//======================================================================
int main()
{
string filename = "in.txt" ;
string s;
s = readFile1( filename ); write( s );
s = readFile2( filename ); write( s );
s = readFile3( filename ); write( s );
s = readFile4( filename ); write( s );
s = readFile5( filename ); write( s );
}
Last edited on Feb 4, 2018 at 8:20pm UTC
Feb 4, 2018 at 8:04pm UTC
How to read an entire file into memory in C++
http://cpp.indi.frih.net/blog/2014/09/how-to-read-an-entire-file-into-memory-in-cpp/
Feb 4, 2018 at 9:23pm UTC
That's the one - thanks
Topic archived. No new replies allowed.