How to edit text file data with c++ - c++

I have a program that create a text file of stock items, which contains detail of 'total production' , 'stock remaining' and so on. Now my question is how do I edit that text file with my program. For example if I mistake to enter a correct data (like production was 500 pieces but enter only 400) now how can I edit my file to make it correct without effecting other data.

You probably should not create a text file in the first place. Did you consider using sqlite (or indexed files à la GDBM ...) or some real database like PostgreSQL or MongoDb?
If you insist on editing programmatically a textual file, the only way is to process every line : either keep all of them in memory, or copy them (except the one you'll change) to some new file.... But there is no portable way to change the content of a file in the middle.
You might also be interested in textual serialization formats like JSON, YAML (or maybe even XML).

Related

Read-in df from csv before launching main app | Dash

I am trying to get my first dashboard with python dash running.
The whole thing is very similar to this https://github.com/dkrizman/dash-manufacture-spc-dashboard.
At the beginning a Dataframe is read in from a csv. My problem seems to be quite easy to solve but somehow I am not succeeding:
I want to create a initial window that allows the user to select (from e.g. dropdown) the csv file (or accordingly the path) that is read in. All the .csv files look the same but just have different values.
When using the modal components I get problems with the install of bootstrap and I thought there must be an easier way?
Thanks for your help!
Best,
Nik

How to make an HTML5 file grow?

I am writing a logger in C++ that generates an HTML5 output in real time. So the HTML file must be readable at any time, even while growing.
So far I open the file, I delete the last few lines that close the block (</body></html>), I add the new log messages and I close the block again.
Is this a good approach, or are there any better solutions?
Another approach would be to read (through XMLHttpRequest) the log file directly by JavaScript inside HTML file and generate HTML within the browser. This may end up being quite slow for large log files though (100 MB+).
If the only thing you need is just to wrap your textual log file with a header and a footer, you can just create a markup as needed and add an <iframe src="log.txt"> tag between a header and footer, which src attribute would point to the raw textual log file. This wouldn't suite you if you need to format the log somehow, of course.

File handling C++:Which is the ideal stream to be used while working with database files?

I wanted to know about the file handling stream that should be applied while working with database files.I want to create a database file i.e. a file which contains contents and these contents can be edited
Eg-Suppose the file contains data as following
Harshul 97 Jack 42 Sergey 69 Bill 96 Mark 92 Will 49
It is a database file which contains user's name along with the money in their account (which is stored after account's name).
Now suppose that I want to add a new account to my database for that I would have to first check that if an account already exists because if it exists then I will display error message else i will simply create a new account by appending data into the file.
Now I thought that I would need to edit the data so I should use fstream but while working with fstream I got the problem with the end of file marker which sets good bit to fail bit and stops file i-o operations I got a solution for that i.e. to clear the stream where ever necessary (whenever file pointer hits eof)
Eg-
fstream file("Filename.txt",ios::in|ios::ate|ios::out);
char str[80];
while(file>>str)
{
//do the required stuff
}
//clear the stream and reuse it
file.clear();
file.seekp(0);
But this was a little idiotic here so I thought that I should use the peek() function that tells us if the next bit is eof before it but got the result that it is not the right thing to do rather I should open the file again and again File Handling:What is the use of peek() function in c++?
Whereas I also got the suggestion to use ifstream and ofstream (with no trunc and ate modes) simultaneosly but I wanted to know if I would be able to edit data with it.Suppose I entered details of a new indivisual say "Finch 96" now the ofsteam variable will account for the new entry but for the ifstream object it had read the data of file into its buffer earlier and has no account for our new entry "Finch 96" untill we don't reopen the file in ifstream object
I have searched a lot about this matter but didn't got the result may be I was not able to express my problems properly and now I think that my objective is clear to all
A text file is nice if you want to be able to edit it by hand. If you call it a database, and only want to process it programmatically, you should considere a binary file. At the simplest level, you could have a direct file with fixed size records, that allows you to do in-place record edition. Or if you prefere not to re-invent oval wheels when round ones exist around, you could use a sqlite database which would deal with implementation details for you.
But if you really need a text file, you should read it once in a container of records, and save it all records at a time. A good practice is save to a temp file in same folder and rename it only when everything has successfully be written.
You probably should not create a text file in the first place. Did you consider using sqlite or some real database like PostgreSQL or MongoDb?
If you insist on editing programmatically a textual file, the only way is to process every line : either keep all of them in memory, or copy them (except the one you'll change) to some new file.... Which is not very efficient.
You might also be interested in textual serialization formats like JSON,it's quite easy to use and very powerful.

Creating users for game(reading and parsing CSV file)

So I am working on a planets vs zombies type mock up game for class, and am using Qt Creator GUI with C++. One of the things that we are required to do is, on start-up, the game window will attempt to read two files: "pvz_levels.csv" and "pvz_players.csv" from a pre-specified home directory.
The levels file is of the form "level:sequence:rows:start:interval:decrement" and "sequence" itself is a comma separated list of the form (1,1,1,2,3,1,3,1,3,3) which is the sequence in which zombies appear. If this file does not exist in the directory, the program exits with an error.
The players file is of the form "timestamp:player:level"; the time of last play, name of player, and last attempted level, respectively. If this file does not exist, the program silently skips this operation and starts as a new player. If it does exist, the file must be read and parsed, and then used in the program for calculations and such.
So, I am having much trouble with reading and parsing these files. Furthermore, we are required to save the user data in these files, and on the next start-up the user should have the option to continue their game by selecting their respective user from a drop-down list. They should also be able to delete any users.
I am proficient enough with c++ basics but this is my first GUI experience and my prof did not go over it in much detail, so I require quite a bit of help with this project.
Thank you to anyone who is able to help!
Look up the code to an available "csv parser" which stands for "comma separated variable". You need is almost identical, except you use a semi-colon instead of a comma. It seems that by changing one character you parsing is done for you.
You may be able to find a csv parser that accepts the character to be used as the parsing character (I've seen them before).
If you wish I can find a suitable csv parser for your use, but now that you know what you're looking for "csv parser c++ code" it should be a quick Google away.
Also, most csv parsers expect strings to be enclosed to double quotes (") but that is easily modifable.
Some hints:
Just open the File using QFile. Set up a QTextStream and use QTextStream::readLine() to read all Lines into QStringList. Now use QString::split() on the QString saved in the list to get the single values stored in this line. From there you can easily use QString::to* functions to cast the values into your desired type.
For saving just reverse the procedure.
Set up a line using: QString("%1,%2,%3").arg(timestamp).arg(player).arg(level) and put it into your QTextStream. If the stream is connected to a file, this will be written into the file.
I've implemented successfully the CSV reading like this:
std::unique_ptr<QFile> csv_worker(new QFile(resource_path));
QTextStream input_csv(csv_worker.get());
QList<QStringList> data
while(!input_csv.atEnd())
{
//removes the carriage return symbol and splits the elements
QStringList line = input_csv.readLine().remove(QRegExp("\r")).split(","); //replace here with :
data << line;
}
csv_worker->close();
It reads the complete file, each line generates a QStringList.
For the second element in the QStringList, let's say (1,1,1,2,3,1,3,1,3,3}, you have to additionally split that in a sub-QStringList, removing then brackets with something like this:
QStringList sequence = data[i][1].remove(QRegExp("{")).remove(QRegExp("}")).split(",");

what is the actual meaning of parsing a html file?

I am not able to understand what actually parsing the html means ?
As i understand -
- it means that suppose we have any html file by parsing we can have the contents of the html file and we can edit them using parsing. Am i right ?? (parsing simply gives the idea about the contents and structure inside the file.)
I have one more question-
- I also want to know that suppose i have html file contents stored in a stream suppose (inside IStream *HTMLContents - No matter for now that how i got these contents). Is there any process exist that using these file contents may i create the preview on any window/Dialog Box/Preview pane with the same way exactly as i get the view of that html file in the browser.(for now you can imagine that i have downloded the HTML File contents from any web page(or from any where-No matter- But i have contents of html file in my stream i am sure about it) and i want to render that html file view in my own created window/Dialog Box/Preview pane(i mean it should view exactly as it appears in browser-Yes i know it won't be avle to display some pictures in html file but thats not a problem for me). How to do that ?? (I am using Visual c++ for my accomplishing my task)
Parsing basically means analyzing any data. When you parse HTML, it could be that you are figuring out where all the various elements are located and what do they do.
As for displaying HTML, it depends on what do you want to do:
If you want to open the file in your browser, use something like this.
As for displaying HTML directly in your form, I don't really know of any other way than parsing the HTML and creating your own web rendering engine. Good luck and have fun with that I guess.
Parse HTML means build object model such as DOM: https://en.wikipedia.org/wiki/Document_Object_Model in your program