Home > Blockchain >  Storing parameters as key value pairs in a text file and should be able to perform operations on the
Storing parameters as key value pairs in a text file and should be able to perform operations on the

Time:07-17

I'm trying to write a code in C which basically stores key value pairs in a file separated by a space. The condition is that if the key is already present, it should just update the value(increment by 1) and write that updated value to the text file. And if the key is not already present, it should write the entire key value pair to the text file(For eg: some_param 12). Also if there are many parameters as the example I've stated, it should take just the values of all the parameters and increment all of those values by 1. I am able to write the initial key value pair to the text file but after that I am not able to parse the file just for taking out the value to increment by 1.

This is the code I have:

fstream fn,fn1;
  key = "number_of_flights";
  value = 1;
  fstream tempfile;
    fn.open("samplefile.txt");

if(fn){
    cout << "File Exists" << endl;
    fn1.open("samplefile.txt", ios::in);
    if(fn1.is_open()){
        cout << "File is open...." << endl;
        while(getline(fn1, line)){
            line = "Hello";
            pos = line.find(" ");
            cout << pos << endl;
            str = line.substr(pos 1);
            value = stoi(str);
            cout << str << endl;
            line[pos 1] = value 1;
         
            fn1 << line << endl;
        }
        fn1.close();
    }
}

else{
    cout << "File doesn't exist, creating file......." << endl;
    fn.open("samplefile.txt", ios::out);
    if(fn.is_open()){
        cout << "File created and opened, writing to File........";
        fn << key << " " << value << endl;
        fn.close();
    }
    else{
        cout << "Cannot open file";
    }
}

Can someone please help as I'm trying to figure this out from a really long time. Thanks in advance.

CodePudding user response:

So the code is a bit confused with some debugging stuff mixed in. But if you look at what you've written you are missing a couple of pieces.

You convert the string to an integer value = stoi(str); so you can increment it. But you never convert the integer back to a string, instead you do this line[pos 1] = value 1; which makes no sense at all.

Secondly although you extract the value from the string pos = line.find(" "); str = line.substr(pos 1); you never extract the key. When you put the key and the incremented value back together you are going to need the key.

Here's a code fragment that does all these things

pos = line.find(" ");              // find a space
key = line.substr(0, pos);         // extract the key
value_str = line.substr(pos 1);    // extract the value
value = stoi(value_str);           // convert the value
  value;                           // increment the value
value_str = std::to_string(value); // value back to a string again
line = key   " "   value;          // combine the key and incremented value
fn1 << line << endl;               // write to file

This code is a bit long winded, I've broken it down so you can see all the steps. In reality you could combine some of the steps and get rid of some of the variables.

One simple improvement is to convert the value back to a string by writing it out to the file, replace the last three lines above with

fn1 << key << ' ' << value << endl; // write key and new value

Your code has one other problem. You are trying to open the "samplefile.txt" file for reading and writing simultaneously, that cannot work. Instead just choose a different file name for the output file, and then rename the file at the end.

Something like this (notice I use ifstream for input files and ofstream for output files).

ifstream fin("samplefile.txt");
if (fin) // does the file exist
{
    ofstream fout("samplefile.tmp"); // temporary filename
    ...
    fin.close(); // must close the files before trying
    fout.close(); // to delete and rename
    remove("samplefile.txt");  // delete the original file
    rename("samplefile.tmp", "samplefile.txt"); // rename the temporary file
}
else
{
    ofstream fout("samplefile.txt");
    ...
}
  • Related