Reading and Writing a CSV file
-
Hi, I am new to Qt and I need some help.
I want to make a login application, which takes in input (through some lineEdit) name, surname, email and password of the user and save them in a *.csv file. Also i would be able to read from that csv file to check if the user has been already registered or not.
So the question is, how can i create a csv file, save the informations (writing on it) and reading from the csv file in order to check if the user has already been registered? (also I don't want to show the user the csv file, so there must be no dialog).
I will thank whoever decides to help me. -
@Davide00 CSV is simply a text file where columns are separated by some character (like ,). So, read/write it using QFile. To get elements for the columns split each line using the separator character (https://doc.qt.io/qt-6/qstring.html#split-1).
-
@Davide00
Hello and welcome.there really is not much to say. Use QFile. Specify a pathname, write to it, read from it, as needed. 100% standard file access. Qt does not know about CSV files, so you must send the lines with the separator between columns (like
,
, depending), and split on reading back, yourself. Not sure what else to say or where your issue is. -
- Get write/read to/from a text file for some text, with multiple lines, working. Don't worry about CSV format.
- After that, look at a sample CSV file to see what the required format is for the output, and change the strings you output accordingly.
- After that, read in the lines from such a CSV file and split them up into their columns if you want to get a each cell's content.
-
@Davide00 But what exactly is your current issue? How to open and read from a file? If so: did you read documentation (https://doc.qt.io/qt-6/qfile.html)? There are examples...
-
Is there a reason you have decided on a CSV file? BTW, a CSV file is just a text file with some conventions (and these conventions might even be locale specific). A much simpler approach would be to use
QSettings
and store the information as INI file. (Still, most likely not the best approach, but much easier.)With a CSV file you always need to read in the whole file and keep the info in memory. Then you can check if a user is already registered. It is not worth it to search while reading the file every single time. If you make changes, you just write the entire file with the added information. Hopefully, you don't have to handle that someone else changes the file while your app is running.
One addition: Don't store the password in plain text. This is really, really bad practice. Instead store a salted hash. To check for the password when someone wants to login just recompute the hash with the same salt and compare the hashes. (You can store plain passwords while you are trying to figure out how to write and read your file, but should quickly switch to storing the salted hash instead once you get the file access part working.)