Sign in to follow this  

[C++] reading files

This topic is 4305 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

i'm using FILE structures to read in data for my game i use fgets, but i notice that it will include a newline character in the string i have a file containing all the save game names, and each is on a new line. my problem is, when i store these names in my program, the newline character is being included, meaning that i cannot load games correctly so basically, is there any function that does the same as fgets, but doesn't include the newline character? help will be greatly appreciated

Share this post


Link to post
Share on other sites
The C++ way to do this would be to use std::getline with std::ifstreams, if you want to read files in one line at a time. You could also read in your data directly from the stream into your variables. Perhaps you should invest in a good C++ book. I reccomend "The C++ Programming Language" by Stroustrup. Also see the C++ faq http://www.parashift.com/c++-faq-lite/.


#include <string>
#include <fstream>

// ...

std::ifstream file("myfile.txt");

std::string line;
std::getline(file, line);

// ...


Share this post


Link to post
Share on other sites
i didn't want to use fstream because ive found that FILEs work a bit better for some of the other stuff im doing, and i figured i might as well stick with the same thing

thanks BuBu LV, i tried that and it worked perfectly

so no more problems

thanks for the help everyone

Share this post


Link to post
Share on other sites

This topic is 4305 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this