Replace a single value in JSON file with JSONcpp - c++

Is there any way to change a single value in an already existing JSON file with JSONcpp?
I want to preserve the existing formatting (comments, spaces, line breaks) in the file.
Example: find the value with key "test_boolean" in C:/test.json and make its value "false", leaving everything else as it is.
I've tried using Json::StyledStreamWriter, both with the root of the value I want to change, and the value itself. In the first case, the entire file was rewritten, with the new value, but no formatting or comments were preserved. In the second case, only "false" was written in the entire file.

According to my tests/research, it is impossible to modify a single value in a JSON maintaining the current formatting, using JSONcpp.

Related

index a text file (lines with different size) in c++

I have to extract information from a text file.
In the text file there is a list of strings.
This is an example of a string: AAA101;2015-01-01 00:00:00;0.784
The value after the last ; is a non integer value, which changes from line to line, so every line has different lenght of characters.
I want to map all of these lines into a structured vector as I can access to a specific line anytime I need without scan the whole file again.
I did some research and I found some threads about a command called, which permit me to reach a specific line of a text file but I read it only works if any line has the same characters lenght of the others.
I was thinking about converting all the lines in the file in a proper format in order to be able to map that file as I want but I hope there is a better and quick way
You can try TStringList*. It creates a list of AnsiStrings. Then each AnsiString can be accessed via ->operator [](numberOfTheLine).

Overwriting a line in CSV file with C++

I'm writing a payroll program in c++ and need to be able to read lines in a file, do calculations, and then overwrite the read lines in the file. IS there a function/way i can simply overwrite specific lines, insert new lines, add onto the end of an existing file?
There are no C++ functionality to "insert" or "remove" text in a text-file. The only way to do that is to read the existing text in, and write out the modified text.
If the new text fits in the same space as the old one, all you need to do is to overwrite the existing text - and of course, you can always add extra spaces before/after a comma in a .CSV file, without it becoming part of the "field". But if the new data is longer, it definitely won't work to "overwrite in place".
Adding to the end is relatively easy by using the ios_base::ate modifier. But inserting in middle still involves basically reading until you find the relevant place, and then, if the new text is longer, you have to read all the following lines before you can write the new one(s) out.

ways to read a text file in golfscript and print out its content

'#{File.read("file")}' puts
Does not work. Is it possible to read in the content of a text file in GolfScript?
The #{...} expansion only occurs with double-quoted strings:
"#{File.read('file')}" puts
works fine.
However, there are some catches.
If you want 'file' to be a parameter, you have to delay the expansion.
The result is cached the first time, so if you want to read the same file more than once (e.g. to check for changes) you have to ensure that the expanded value changes. The easiest way I know to do this is to expand "#{File.read('file')#1}", "#{File.read('file')#2}", etc.

How to append to the beginning of a text fle in g++

I want to write to a file without overwriting anything. It is a text file containing records. When I delete a specific record, I do not actually remove it from the file, I just put information in the header saying that it is deleted. How can I do this?
You cannot append to the BEGINNING of a file without having to rewrite it from scratch. It has to go at the end (which makes sense, since that's what the word "append" means).
If you want to be able to flag a record as deleted without reserving space for that flag, you'll need to place the information at the end, or rewrite everything.
A more sensible approach is indeed to reserve the space upfront - for example by placing a "deleted" field in each record.
One possible solution is if there are certain characters which are normally dissallowed in records (it seems like each file is a record - please correct me if I'm wrong):
Use these characters in combination with some number of word flag (eg. #deleted#, or #000 if # is a character not normally allowed in records).
Then just overwrite whatever happens to be at the beginning of the record; it's deleted anyways so it shouldn't matter that you're overwriting part of it.
On the other hand, this probably isn't a good idea if you anticipate ever needing to recover 'deleted' files.
By the way - if you do append (at the end of the file) the deleted flag, note that it's very easy to check for it if you know the file size - just look at the end of the file.

wistringstream from an xml file to an integer?

const XMLDataNode *pointsNode = node->GetChildren().at(0);
std::wistringstream pointsstrm(*pointsNode->GetInnerText());
pointsstrm >> loadedGame.points;
This is code I've written to pull an int from an XML file and pass it into loadedGame.points (an int). However, this isn't working. It compiles but doens't give the right value. Why is that? XMLDataNode is a class that manipulates xmllite.dll.
Time for some wild guesses!
I'll bet you that the text you get from *pointsNode->GetInnerText() isn't what you think it is. Have you checked that it is indeed exactly the text you want? In particular, could it contain whitespace? Parsing a nicely formatted (i.e. indented, broken into lines, etc) XML file without a schema to reference ends up meaning that all sorts text nodes involving whitespace will end up in your DOM tree.