Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

gamedevrules

opening txt files?

This topic is 6163 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
Since this is such a basic question, I figure it would be a good learning experience for you to find out on your own. Use your head and most of all Google.

Hint: Look up C++ File Input Output, more specifically istream and ostream.

Have fun!

Share this post


Link to post
Share on other sites
In full:

#include <fstream>
#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::endl;
using std::getline;
using std::ifstream;
using std::string;
int main(int argc, char *argv)
{
ifstream iFile;
string str;
cout << "Please enter the name of the file to display:";
cin >> str;
iFile.open(str.c_str());
if(iFile.fail())
{
cout << "Failed to open file " << str << ". Exiting..." << endl;
return -1;
}
// grab data line by line
while(!iFile.eof())
{
getline(iFile, str);
cout << str;
}
cout.flush();
iFile.close();
return 0;
}

I originally wanted to do something fancy involving stream iterators, so it took me longer than 15 minutes.

[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!