Jump to content
  • Advertisement
Sign in to follow this  
Harryu

[C++] reading files

This topic is 4520 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
Advertisement
First your using the C way. C++ uses the <fstream> libraray if you want to search that. Second you can say if(char != '\n') then read in. I think thats how it worked on my groups text game.

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
Sign in to follow this  

  • 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!