A
arnuld
I want to read a file into std::string. I am basically a C Programmer so
it was quite hard for me to understand how to do it in C++. I did C++
long time back (if you guys remember my name but I do remember Shiva and
Victor Bazarov and others).
I googled for it and this is the best what I could come up with. Do you
guys have any suggestion for improvement ? and whether this is really a
correct C++ program. (Compiled with "g++ -ansi -pedantic -Wall -Wextra")
#include <iostream>
#include <fstream>
#include <string>
int main()
{
std::string my_contents, tmp_contents;
std::ifstream my_file("reference.cpp");
if(!my_file)
{
std::cerr << "Error Opening file" << std::endl;
exit(EXIT_FAILURE);
}
while(my_file)
{
std::getline(my_file, tmp_contents);
my_contents += tmp_contents;
my_contents += "\n";
}
std::cout << "String contents are: "<< "\n"
<< my_contents << std::endl;
my_file.close();
return 0;
}
-- arnuld
www.LispMachine.Wordpress.com
it was quite hard for me to understand how to do it in C++. I did C++
long time back (if you guys remember my name but I do remember Shiva and
Victor Bazarov and others).
I googled for it and this is the best what I could come up with. Do you
guys have any suggestion for improvement ? and whether this is really a
correct C++ program. (Compiled with "g++ -ansi -pedantic -Wall -Wextra")
#include <iostream>
#include <fstream>
#include <string>
int main()
{
std::string my_contents, tmp_contents;
std::ifstream my_file("reference.cpp");
if(!my_file)
{
std::cerr << "Error Opening file" << std::endl;
exit(EXIT_FAILURE);
}
while(my_file)
{
std::getline(my_file, tmp_contents);
my_contents += tmp_contents;
my_contents += "\n";
}
std::cout << "String contents are: "<< "\n"
<< my_contents << std::endl;
my_file.close();
return 0;
}
-- arnuld
www.LispMachine.Wordpress.com