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. Reading numbers from a file
Forum Update on Monday, May 27th 2025

Reading numbers from a file

Scheduled Pinned Locked Moved Solved Qt Creator and other tools
qtcreator 6.0.2helpqfile
2 Posts 2 Posters 476 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.
  • K Offline
    K Offline
    Killua_Zoldyck
    wrote on last edited by Killua_Zoldyck
    #1

    I have a .txt document with 2 column numbers like this:

    5.4 2.1
    2.4 2.5
    3.4 6.4
    ... ...
    and so on.

    How can i store them in QVectors x,y using Qfile and QTextStream?

    current code:

    QFile file(file_name);
        if(!file.open(QIODevice::ReadOnly))
        {
            qDebug() << "didnt open";
        }
    
        QTextStream stream(&file);
    
        QVector<float> x,y;
    
        
        while(!stream.atEnd())
        {
            QString line = stream.readLine();
        }
    
        for(int i=0;i<x.size();i++)
        {
            file >> x[i] >> " " >> y[i];
        }
        
    

    thank you and have a good day!

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mpergand
      wrote on last edited by
      #2
      QFile file(file_name);
         file.open(QFile::ReadOnly);
      
         QVector<float> x,y;
      
         while(!file.atEnd())
            {
             QList<QByteArray> list = file.readLine().split(' ');
             if(list.count()==2)
                 {
                 x<<list[0].toFloat();
                 y<<list[1].toFloat();
                 }
            }
      
         qDebug()<<x<<y;
      
      1 Reply Last reply
      3

      • Login

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