basic problem about I/O function in STL

Started by
2 comments, last by cty41 13 years, 6 months ago
Hi,guys.
I want to make a config file using STL I/O classes(ifstream and ofstream),and I learn most from ogre ,but I simplified the code,but now I had some problems: when I launch the app first time ,everything is right, default config options is loaded, and saved correctly.But each timeI launch the app, the config file contain one more blank line after the right option time.
Just
first time the config file is:
option1=1
option2=2
option3=3

each time after:()
option1=1
(n blank lines)
option2=2
(n blank lines)
option3=3
(n blank lines)

I really confused, though I wrote a test .cpp file, but it has same problem, here is my test code:
#include <iostream>#include <string>#include <map>#include <fstream>using namespace std;typedef struct  _ConfigOption{	string		name;	string		value;		}ConfigOption;typedef std::map<string, ConfigOption> ConfigOptionMap;static string g_ConfigName = "config.cfg";static ConfigOptionMap g_ConfigOption;#define MAX_TEMP_STREAM_SIZE 128void setConfigOption(const string& first, const string& second){	ConfigOptionMap::iterator it = g_ConfigOption.find( first );	if(it != g_ConfigOption.end())	{		it->second.value = second;	}	else	{			cout<<"option : ' "<<first<<"' does not exist."<<endl;	}}void loadDefaultConfigOption(){	ConfigOption resolutionMode;	resolutionMode.name = "Resolution Mode";	resolutionMode.value = "800 x 600";	ConfigOption fullScreen;	fullScreen.name = "Full Screen";	fullScreen.value = "No";	ConfigOption windowTitle;	windowTitle.name = "Window Title";	windowTitle.value = "Titan";	g_ConfigOption[resolutionMode.name] = resolutionMode;	g_ConfigOption[fullScreen.name] = fullScreen;	g_ConfigOption[windowTitle.name] = windowTitle;}void loadConfig(){	std::ifstream fp;	fp.open(g_ConfigName.c_str(), std::ios::in | std::ios::binary );	if(fp == 0)	{		cout <<"'" <<g_ConfigName<< "' not found, create and use default config."<<endl;		return;	}	string line, first, second;	while(!fp.eof())	{		getline(fp, line)		if(line.length() > 0 && line.find('=') != string::npos)		{			first = line.substr(0,line.find('='));			second = line.substr(line.find('=') + 1, line.length());			setConfigOption(first, second);		}	}	fp.close();}void saveConfig(){	if(g_ConfigName.empty())		return;	std::ofstream of(g_ConfigName.c_str());	if(of == 0)		cout<< "can not write config text"<<endl;	for(ConfigOptionMap::const_iterator it = g_ConfigOption.begin();it != g_ConfigOption.end(); ++it)	{		of << it->first << "=" << it->second.value<<std::endl;	}	of.close();}int main(){	loadDefaultConfigOption();	loadConfig();	saveConfig();	return 0;}


But I found one clue: If I just comment loadConfig() in main, the config file will not have problems.
It seems to be a very basic problem, but I am a little strange about ifstream and ofstream, If anyone can give me some advices, I will be very appericated!

my best regard,
Frank
Advertisement
It looks like the lines you read in contain a '\n' at the end and when you go to write them back out, you add another one.
std::string::substr()'s second argument is a length, not a position.
Oh, how silly I am...Thanke, guys~~Sorry for disturbing~

This topic is closed to new replies.

Advertisement