Dont know if it is what you're looking for friend, but I made some code trying to teach how to change a single value in a file without changing other values
The header file:
Code:
#ifndef dataToStoreHeader
#define dataToStoreHeader
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
using namespace std;
class dataToStore
{
private:
char someString[256];
int someInt;
std::fstream *dataStream;
public:
dataToStore(char *someString, int someInt){
strcpy(this->someString, someString);
this->someInt = someInt;
}
dataToStore(void){
}
int getInt(void){
return(this->someInt);
}
char *getStr(void){
return(this->someString);
}
void setStr(char *someString){
strcpy(this->someString, someString);
}
void setInt(int someInt){
this->someInt = someInt;
}
};
#endif
and the main file:
Code:
#include "dataToStore.h"
#define FILE "data.dnl"
#define OUT_MODE std::ios::out | std::ios::binary
#define IN_MODE std::ios::in | std::ios::binary
#define DATA_SIZE sizeof(dataToStore)
int main(void)
{
// here we declare a new dataToStore object
dataToStore *data = new dataToStore("Hello, ", 7);
// and here we declare a pointer to the begining of the
// memory area where the new data varibable is
char *dataPtr = (char *)data;
// here we make a ofstream to write data to,
// and a ifstream to read data from the file
std::ofstream out;
std::ifstream in;
// we open the file in binary mode, write the data to it and then close it
out.open(FILE, OUT_MODE);
out.write(dataPtr, DATA_SIZE);
out.close();
// change the values just to see that only the right
// values will appear on the screen
data->setInt(0);
data->setStr("this is a string");
// we now open the same file in binary mode to load the data
in.open(FILE, IN_MODE);
in.read(dataPtr, DATA_SIZE);
data = (dataToStore *)dataPtr;
in.close();
// print the values...
std::cout << data->getInt() << "\n" << data->getStr() << endl;
// ...and change the string value, keeping the int value unchanged
data->setStr("World!");
// Now lets save the new string value to the file
// Open the file and save the data
out.open(FILE, OUT_MODE);
out.write(dataPtr, DATA_SIZE);
out.close();
data = (dataToStore *)dataPtr;
// The file is saved. Lets change both values again.
// These values will never apear on the screen
data->setInt(666);
data->setStr("Mwahahahahaha!");
// Load the data
in.open(FILE, OUT_MODE);
in.read(dataPtr, DATA_SIZE);
in.close();
data = (dataToStore *)dataPtr;
/* Ok, now simply print the values.
As you can see, the int value is unchanged, but the string value
now contains "World", not "Hello" */
std::cout << data->getInt() << "\n" << data->getStr() << endl;
delete data;
getchar();
return(0);
}
You could write the content of the class to a text file instead of printing it on the screen...
If you wish to change a value inside a text file without a binary file to store the data(a normal txt file, for example), just create a function that will look for the desired value, then save the rest of the file, change the value, and append it again.