Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Trouble writing to file
Qt 6.11 is out! See what's new in the release blog

Trouble writing to file

Scheduled Pinned Locked Moved Solved C++ Gurus
8 Posts 4 Posters 3.0k Views
  • 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.
  • C Offline
    C Offline
    Christianvs
    wrote on last edited by
    #1

    Hi

    I got a problem with saving some data to a file on my system.

    0_1520336827512_7d79d5fd-80b0-437e-a24d-980e753774ab-image.png

    As the picture shows i got a vector of float in which i store some data. This vector is then stored in another vector which is declared in my header file:
    0_1520336913006_669dc4d5-2cb2-43e1-a6a2-6cff008e55ee-image.png

    I do a n++ to keep track of the number of vectors that is stored in the vector rtlData.

    When i press a button called saveRTLData i write the vector rtlData to a txt or preferably a csv file. And here is where my program crashes.

    0_1520336994629_92850cf2-5229-400f-981c-62632a946438-image.png

    To begin with i wrote the whole path "/home/user/.../rtlData.csv" in my code and when i pressed the button i got the message "Stream not open". Then i tried changing the path so it's just rtlData.csv or rtlData.txt as the picture shows, which resultet in the following error:

    [rtl_temp_node-3] process has died [pid 15572, exit code -11, cmd /home/christian/catkin_ws/devel/lib/rtl_temp/rtl_temp_node __name:=rtl_temp_node __log:=/home/christian/.ros/log/4c8dc630-2133-11e8-b647-b8ee65ea785f/rtl_temp_node-3.log].
    log file: /home/christian/.ros/log/4c8dc630-2133-11e8-b647-b8ee65ea785f/rtl_temp_node-3*.log

    Does anyone here know where my mistake is?

    JonBJ 1 Reply Last reply
    0
    • C Christianvs

      Hi

      I got a problem with saving some data to a file on my system.

      0_1520336827512_7d79d5fd-80b0-437e-a24d-980e753774ab-image.png

      As the picture shows i got a vector of float in which i store some data. This vector is then stored in another vector which is declared in my header file:
      0_1520336913006_669dc4d5-2cb2-43e1-a6a2-6cff008e55ee-image.png

      I do a n++ to keep track of the number of vectors that is stored in the vector rtlData.

      When i press a button called saveRTLData i write the vector rtlData to a txt or preferably a csv file. And here is where my program crashes.

      0_1520336994629_92850cf2-5229-400f-981c-62632a946438-image.png

      To begin with i wrote the whole path "/home/user/.../rtlData.csv" in my code and when i pressed the button i got the message "Stream not open". Then i tried changing the path so it's just rtlData.csv or rtlData.txt as the picture shows, which resultet in the following error:

      [rtl_temp_node-3] process has died [pid 15572, exit code -11, cmd /home/christian/catkin_ws/devel/lib/rtl_temp/rtl_temp_node __name:=rtl_temp_node __log:=/home/christian/.ros/log/4c8dc630-2133-11e8-b647-b8ee65ea785f/rtl_temp_node-3.log].
      log file: /home/christian/.ros/log/4c8dc630-2133-11e8-b647-b8ee65ea785f/rtl_temp_node-3*.log

      Does anyone here know where my mistake is?

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

      @Christianvs
      It helps if you paste actual textual code, not screenshots...

      for (int i = 0; i <= n; i++)

      If n is the number of rows ("vectors"), don't you mean i < n, as is standard in all C-type languages?

      C 1 Reply Last reply
      1
      • JonBJ JonB

        @Christianvs
        It helps if you paste actual textual code, not screenshots...

        for (int i = 0; i <= n; i++)

        If n is the number of rows ("vectors"), don't you mean i < n, as is standard in all C-type languages?

        C Offline
        C Offline
        Christianvs
        wrote on last edited by
        #3

        @JonB
        I will remember that!

        It seems to have been the problem, what a stupid mistake! :)

        But thanks for the answer!

        JonBJ 1 Reply Last reply
        0
        • C Christianvs

          @JonB
          I will remember that!

          It seems to have been the problem, what a stupid mistake! :)

          But thanks for the answer!

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

          @Christianvs
          Yes, get used to the fact that in C-type an array with n elements is always index from 0 to n - 1, inclusive. Trying to access the n-th element will crash (if you're lucky) or exhibit completely unknown behaviour (if you're unlucky).

          1 Reply Last reply
          1
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #5

            since rtlData is a std::vector there's no need to keep track of n. just use rtlData.size()

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            1 Reply Last reply
            2
            • mranger90M Offline
              mranger90M Offline
              mranger90
              wrote on last edited by
              #6

              Or, since you're using a container, use the appropriate iterators.

              VRoninV 1 Reply Last reply
              3
              • mranger90M mranger90

                Or, since you're using a container, use the appropriate iterators.

                VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by
                #7

                @mranger90 said in Trouble writing to file:

                Or, since you're using a container, use the appropriate iterators.

                Which would mean:

                for(const auto& singleRow : rtlData){
                for(const auto& singleVal : singleRow){
                rtlDataStream << singleVal; 
                }
                rtlDataStream << std::endl;
                }
                

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                1 Reply Last reply
                2
                • C Offline
                  C Offline
                  Christianvs
                  wrote on last edited by
                  #8

                  Thanks for all the replies folks!

                  I feel quite stupid for not seeing that .size() actually solves the entire problem, but I am still quite green when it comes to programming.

                  Thanks for the tip with iterators @mranger90 @VRonin ! I'll look into that

                  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