Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt Creator and other tools
  4. How to read number from file(casting- the numbers has readen as string)- c++ qt
QtWS25 Last Chance

How to read number from file(casting- the numbers has readen as string)- c++ qt

Scheduled Pinned Locked Moved Unsolved Qt Creator and other tools
c++filestringintcasting
3 Posts 3 Posters 480 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.
  • R Offline
    R Offline
    RuWex
    wrote on last edited by RuWex
    #1

    Let's say I want to read this file:
    ///
    readLine25
    goToLine1
    ....
    ///
    now,
    How can I treat int as int? and the string as string?
    I want to get the number,
    for example in line 1 I want to get 25
    an important note.
    the string allways 8 chars- not less not more!
    thanks:)

    jsulmJ 1 Reply Last reply
    0
    • R RuWex

      Let's say I want to read this file:
      ///
      readLine25
      goToLine1
      ....
      ///
      now,
      How can I treat int as int? and the string as string?
      I want to get the number,
      for example in line 1 I want to get 25
      an important note.
      the string allways 8 chars- not less not more!
      thanks:)

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

      @RuWex said in How to read number from file(casting- the numbers has readen as string)- c++ qt:

      How can I treat int as int?

      https://doc.qt.io/qt-6/qstring.html#toInt

      To get the number string simply use https://doc.qt.io/qt-6/qstring.html#sliced

      So, first call https://doc.qt.io/qt-6/qstring.html#sliced to get the part of the string which contains the int and then convert that string to int using https://doc.qt.io/qt-6/qstring.html#toInt

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

      JonBJ 1 Reply Last reply
      1
      • jsulmJ jsulm

        @RuWex said in How to read number from file(casting- the numbers has readen as string)- c++ qt:

        How can I treat int as int?

        https://doc.qt.io/qt-6/qstring.html#toInt

        To get the number string simply use https://doc.qt.io/qt-6/qstring.html#sliced

        So, first call https://doc.qt.io/qt-6/qstring.html#sliced to get the part of the string which contains the int and then convert that string to int using https://doc.qt.io/qt-6/qstring.html#toInt

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

        @jsulm
        OoI, why did Qt6 QString feel the need to introduce sliced() when we have had mid() for years?
        EDIT Oh, mid() docs now say

        If you know that position and n cannot be out of bounds, use sliced() instead in new code, because it is faster.

        1 Reply Last reply
        1

        • Login

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