#### Archived

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

# fstream problem with writing in file

This topic is 5194 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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)
{
{
MessageBox(NULL,"Warning please include cConfigFile.init() before using this command","Warning",IDOK);
return false;
}

tempSpace = " ";
tempReturn = "\n";
//int tempvalue = value;

char *tempvalue;
tempvalue = new char[256];

sprintf(tempvalue,"%i",value);
tempValue = tempvalue;
int Pointer;
{
{
INIFileWrite2.seekp(Pointer);
tempValue = tempSpace + tempValue;
INIFileWrite2 << tempValue;
INIFileWrite2.close();
return true;
}
}

}

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

##### Share on other sites
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 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 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 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 on other sites

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

• 40
• 15
• 10
• 23
• 19