Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Reading and Writing a CSV file
Forum Updated to NodeBB v4.3 + New Features

Reading and Writing a CSV file

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 4 Posters 7.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Davide00
    wrote on last edited by
    #1

    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.

    jsulmJ JonBJ 2 Replies Last reply
    0
    • D Davide00

      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.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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).

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      3
      • D Davide00

        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.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #3

        @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.

        D 1 Reply Last reply
        1
        • JonBJ JonB

          @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.

          D Offline
          D Offline
          Davide00
          wrote on last edited by
          #4

          @JonB Thank you for your answer. The problem is that i don't know how to do this with code. Could you write me a short example of writing to/reading from a CSV file?

          JonBJ jsulmJ 2 Replies Last reply
          0
          • D Davide00

            @JonB Thank you for your answer. The problem is that i don't know how to do this with code. Could you write me a short example of writing to/reading from a CSV file?

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #5

            @Davide00
            Well you need to know, or at least try, that's what coding involves. There are loads of examples of reading/writing files, on the web and in the Qt docs. There are many posts in this forum, including for CSV files.

            1 Reply Last reply
            0
            • D Davide00

              @JonB Thank you for your answer. The problem is that i don't know how to do this with code. Could you write me a short example of writing to/reading from a CSV file?

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Davide00 Please ask specific questions.
              What exactly is your current issue?

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              D 1 Reply Last reply
              0
              • jsulmJ jsulm

                @Davide00 Please ask specific questions.
                What exactly is your current issue?

                D Offline
                D Offline
                Davide00
                wrote on last edited by
                #7

                @jsulm My current issue is that i don't know how to read from/write to CSV, but i will follow @JonB 's advice and look for posts in this forum that can help me in some ways.
                I would like to thank you two for answering me.

                JonBJ jsulmJ 2 Replies Last reply
                0
                • D Davide00

                  @jsulm My current issue is that i don't know how to read from/write to CSV, but i will follow @JonB 's advice and look for posts in this forum that can help me in some ways.
                  I would like to thank you two for answering me.

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #8

                  @Davide00

                  1. Get write/read to/from a text file for some text, with multiple lines, working. Don't worry about CSV format.
                  2. 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.
                  3. 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.
                  D 1 Reply Last reply
                  2
                  • JonBJ JonB

                    @Davide00

                    1. Get write/read to/from a text file for some text, with multiple lines, working. Don't worry about CSV format.
                    2. 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.
                    3. 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.
                    D Offline
                    D Offline
                    Davide00
                    wrote on last edited by
                    #9

                    @JonB Got it, thanks a lot.

                    1 Reply Last reply
                    0
                    • D Davide00

                      @jsulm My current issue is that i don't know how to read from/write to CSV, but i will follow @JonB 's advice and look for posts in this forum that can help me in some ways.
                      I would like to thank you two for answering me.

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @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...

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      2
                      • S Offline
                        S Offline
                        SimonSchroeder
                        wrote on last edited by
                        #11

                        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.)

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved