## 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 on other sites
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 on other sites
Maybe try to use line[strlen(line)-1]=0 to delete (ignore) newline character.

##### 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 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

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627677
• Total Posts
2978590

• 11
• 12
• 10
• 12
• 22