Jump to content
  • Advertisement

Archived

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

madcow

fstream problem with writing in file

This topic is 5283 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 am having a problem with fstream and writing in a file. I have made it so it searches for the point where it needs to write and then write the new value there. But when the new value is bigger as the old value it destroys the newline char and the next line what is on there. How can I make it so that it only replaces the value?
bool CINIFile::WriteINIItem(string thing, int value)
{
	INIFileRead.open("Madcow's App.ini");
	if (!INIFileRead.is_open())
	{
		MessageBox(NULL,"Warning please include cConfigFile.init() before using this command","Warning",IDOK);
		return false;
	}
	//INIFileWrite.open("Madcow's App.ini");

	string tempType, tempValue, tempReadinType, tempAdjust, useless, tempSpace, tempReturn;
	INIFileRead >> tempReadinType;
	tempAdjust = ":";
	tempSpace = " ";
	tempReturn = "\n";
	//int tempvalue = value;

	char *tempvalue;
	tempvalue = new char[256];
	
	sprintf(tempvalue,"%i",value);
	tempType = thing + tempAdjust;
	tempValue = tempvalue;
	int Pointer;
	while (!INIFileRead.eof())
	{
		if (!strcmp(tempType.c_str(),tempReadinType.c_str()))
		{
			Pointer = INIFileRead.tellg();
			INIFileRead.close();
			fstream INIFileWrite2("Madcow's App.ini", ios::in|ios::out );
			INIFileWrite2.seekp(Pointer);
			tempValue = tempSpace + tempValue;
			INIFileWrite2 << tempValue;
			INIFileWrite2.close();
			return true;
		}
		INIFileRead >> useless;
		INIFileRead >> tempReadinType;
	}
	


}
 
[edited by - madcow on March 4, 2004 9:12:19 AM]

Share this post


Link to post
Share on other sites
Advertisement
You can not insert into C++ streams (or C FILEs either). You must copy everything after the insertion point into a buffer, add your new content, then append the save buffer.

Share this post


Link to post
Share on other sites
I dont really want to put a extra value in it just replace a value that is already there by what is 2 numbers not 1.
Is there maybe then another easyer way to do it or is that the only way?

[edited by - madcow on March 4, 2004 1:08:43 PM]

Share this post


Link to post
Share on other sites
Nope, theres no easier way. If you want to do a search / replace where the replace is a different length to the search, you need to read the whole file, and write it (simultaniously, not loading the whole file into memory)

Share this post


Link to post
Share on other sites
Depends on your definition of easier.

One alternative is to open a new file, copy the original file up to the insertion point into the new file, add your data and then copy the rest of the data to the new file. Then replace the old file with the new file.

Or you could read the whole file into a data structure that supports insertion in the middle, insert into that data structure and then copy out the data structure to the file. (This probably involves the fewest lines of code, but would perform the worst.)

Or if your INI file is in standard windows format and you''re on a windows platform, you could just try to use the WritePrivateProfileString() function.

Share this post


Link to post
Share on other sites
madcow,

The easiest way to do this is to have CINIFile load the entire file into memory in the constructor (or when initialized) and write the entire file back to disk in the deconstructor (or when shutdown). CINIFile should contain a list of Keys/values for referencing or altering.

Another way to do this (if you want to save yourself some work) is to use the Win32 functions GetPrivateProfileString(), GetPrivateProfileInt(), WritePrivateProfileString(), and WritePrivateProfileInt(). These are located in "windows.h"

Hope this helps

[edited by - enfekted on March 4, 2004 1:21:28 PM]

Share this post


Link to post
Share on other sites
tnx I will try the profile commands from win 32 else I just do the other way because that is just as easy because my app already auto reads all the variables and ints.

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!