Reputation: 9283
Note that this is NOT a "better-than" discussion.
I'm a Java programmer, and it makes me feel incredibly dumb not to know how to do very much C++ file IO.
I need to make very simple adapter for XML parsers, just like code below says
In Java, I could just use:
BufferedReader reader = new BufferedReader(
new InputStreamReader(xmlInputStream));
String xml = "";
String line = null;
while ((line = reader.readLine()) != null) {
xml += line + "\n";
}
return xmlParser11.parse(xml);
Biggest question for me is what to do with this reader
in C++
Thanks very much!
edit cutted ;)
Upvotes: 1
Views: 3937
Reputation: 153899
Several comments on the edit:
Upvotes: 1
Reputation: 76778
If you are reading from a file, you could do it like this:
std::ifstream file("myfile.xml");
std::stringstream xml;
std::copy(std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>(),
std::ostream_iterator<char>(xml));
This will read the entire file into the std::stringstream
xml
, including line-breaks and all (like in your sample-code). You can then access it as an std::string
using xml.str()
.
Upvotes: 3
Reputation: 153899
You can do exactly the same thing in C++:
std::ifstream reader( xmlInputStream.c_str() );
if ( !reader.is_open() ) {
// error handling here...
}
std::string xml;
std::string line;
while ( std::getline( reader, line ) ) {
xml += line + '\n';
}
It's probably not the best solution, but it's already fairly good. I'd probably write something like:
std::string xml(
(std::istringstream_iterator<char>( reader )),
(std::istringstream_iterator<char>()) );
(Note that at least one set of the extra parentheses are needed, due to an anomaly in the way C++ would parse the statement otherwise.)
Or even:
std::string
readCompleteFile( std::istream& source )
{
return std::string(
std::istringstream_iterator<char>( source ),
std::istringstream_iterator<char>() );
}
(Look ma, no variables:-)!) Both of these solutions preserve the newlines in the original file, so you don't need to put them back in after reading.
Upvotes: 1
Reputation: 47408
To give a gentler introduction, the following C++ code is mimicking your Java code as much as sensible:
#include <iostream>
#include <fstream>
#include <string>
int main()
{
std::ifstream xmlInputStream("input.xml"); // or istringstream or istream
std::string xml;
std::string line;
while(getline(xmlInputStream, line))
{
xml += line + "\n";
}
//return xmlParser11.parse(xml);
std::cout << xml << '\n';
}
But of course one doesn't have to loop to read an input stream into a string in C++: the input stream can be represented as a pair of iterators, which can be used in many different ways:
#include <iostream>
#include <fstream>
#include <string>
#include <iterator>
int main()
{
std::ifstream xmlInputStream("input.xml");
std::istreambuf_iterator<char> beg(xmlInputStream), end;
std::string xml(beg, end);
std::cout << xml << '\n';
}
But often a temporary string object is not even needed: a C++ parser could operate on an input stream or on a pair of iterators directly.
Upvotes: 6
Reputation: 6124
This is using STL -- did you mean to ask about C++ or do you want the C equivalent (i.e. using fopen, fread etc)?
// main.cpp
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
string line;
string xml;
ifstream myfile("example.txt");
if( myfile.is_open() ) {
while( myfile.good() ) {
getline (myfile,line);
xml += line + "\n";
}
myfile.close();
}
else
cout << "Unable to open file";
return 0;
}
Upvotes: 1
Reputation:
I would implement a destructor for BufferedReader
that would destroy any used resources and free any memory used during that object's lifetime. After I did that, then I would call delete
on my BufferedReader
object.
Of course, that would be if you were using C++ only.
Upvotes: 0