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(",");
Related
I want something with C++ that can read what I type in the terminal and assign it to a string variable.
I want this for a project to make something like notepad, but in simple terminal form, so I need to have live user input available to get word count and stuff.
Unfortunately, I couldn't find anything on the internet about this, and I only found sites that talked about keyboard input, but I don't want that.
I tried to test this using the getch() function and there was no problem in writing and getting input.
But when the user wants to delete something, we don't know which character the user deleted, because we don't have the selected character, if we have it, when we print it after getting the character (if we don't print the user he just writes and doesn't know what he wrote) the problem is that the page must be cleaned once after deleting a letter and this causes the terminal to tick
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.
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).
Is there anyway to rename the "Source" button to something like "HTML", I ask this as users are confused at how to add html code using the editor?
Yes, inside of the "lang" folder you will see all of the various language files.
For my case, and probably yours, You will want to edit the file "en.js". The file is "compressed" to some degree so it may be difficult to read, but it's still not too difficult to change one string. If you do plan on changing multiple strings you will most likely want to use a service to format Javascript.
Search for the following segment of code. It was one of the very last lines in the file.
"sourcearea":{"toolbar":"Source"}
change it to
"sourcearea":{"toolbar":"HTML"}
Avoid This Method Unless Required
And as for a very unsuggested method, since you can't modify the language files for some reason, you can modify the ckeditor.js file and force a specific label.
Inside of "ckeditor.js" change the line below
a.ui.addButton("Source",{label:a.lang.sourcearea.toolbar,command:"source",toolbar:"mode,10"});
to the follow code
a.ui.addButton("Source",{label:"HTML",command:"source",toolbar:"mode,10"});
The only thing modified is the "label" value in the above line. We remove the reference to the a.language.sourcearea.toolbar and insert a string in it's place instead.
As stated from my title, how can i delete a specify line from a textfile.
My program has a HR user, which they can edit/remove users information.
I am able to write into a file, but to delete from a specific line, i am clueless.
Hopefully someone can give me an example of how to do it, thanks !
A example of my textfile
user;pass;1234;John;1111
user1;pass1;2345;May;2222
user2;pass2;3456;Mary;3333
user3;pass3;4567;Andy;4444
hr;hr;5678;Jonathan;5555
admin;admin;6789;Aili;6666
user10;pass10;7890;eggy;9999
and so i want to delete the contents of user3 which is at line 4 of my textfile,when the user inputs the username, which is user3.
Here is a pseudocode, I will let you work out the details:
1. read the entire file into a vector
2. delete that file
3. create and write back the data to the file skipping the line that isn't required.
Use std::getline() in a loop to read a line from the file.
Load the file content into memory,
delete the line there,
and write the content back to file.
You could somewhat optimize this process by not loading the portion of the file before the line being deleted (though you'd still need to scan it to find the "target" line), but you won't be able to do much better than that without a specialized data structure.
If this is really important performance-wise, consider using a database instead of the plain file.