?
Solved

read from file and save data in QList

Posted on 2010-09-18
10
Medium Priority
?
1,947 Views
Last Modified: 2013-11-23
Hi,
I have file which has this format : 136 column and known rows
1 2 3 ......136
2
3
4
5
.
.
etc
I wrote the code below to read from file and save it in list but there is no result why?

#include "file.h"
#include"iostream.h"
#include<QFile>
#include<QTextStream>
#include<QStringList>

File::File()
{
}
void File::ReadFile()
{
    QFile file("/ReadFile1/debug/P19f-export.mul.txt");
    {
        QTextStream cout(stderr);
    }
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) // to open file
    {
        QTextStream cout(stderr);
    }
    QTextStream in(&file);
    QStringList list;
    QString content;
    for(int i=0;i<136;i++)
    {
        for(int j=0;j<list.size();j++)
        {
            double x;
            in >> x;
            content =in.readLine();
            list.append(content);
        }
    }
    int length = list.length();
    cout<< length;
}

Open in new window

0
Comment
Question by:obad62
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +1
10 Comments
 
LVL 33

Accepted Solution

by:
pgnatyuk earned 668 total points
ID: 33707582
for(int j=0;j
0
 

Author Comment

by:obad62
ID: 33707739
how can I use it after declare i am new in QList
0
 
LVL 33

Assisted Solution

by:pgnatyuk
pgnatyuk earned 668 total points
ID: 33707754
No.
The code is wrong. Sorry, it's not about a QList. The code is logically wrong.
Write the same in the standard C++. When it will work, try to "translate" to this QList/whatsoever.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Assisted Solution

by:JohnGaby
JohnGaby earned 664 total points
ID: 33707823
What are you trying to accomplish here?  Are you looking to have a 2 dimensional array which holds all your numeric data from the input file?
0
 

Author Comment

by:obad62
ID: 33707846
Actually , I have this array and I need to change the array to List

void readFile::readFileAscii()
{
    QFile file("/ReadFile1/debug/P19f-export.mul.txt");
    {
                QTextStream cout(stderr);
           }
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) // to open file
    {        QTextStream cout(stderr);
        cout<<"it is not open";
    }
    QTextStream in(&file);
        double data[count_of_rows][count_of_electrodes];
    for(int i= FirstRow ;i<count_of_rows;i++)
    {
        for (int k = FirstElectrod; k < count_of_electrodes; k++) {
            double x;
            in >> x;
            QString content;
            data[i][k] = content.setNum(x).toDouble();
        }
        in.readLine();
    }
   }

Open in new window

0
 
LVL 9

Assisted Solution

by:JohnGaby
JohnGaby earned 664 total points
ID: 33708524
So you want to read in the numbers in the file as doubles and then store them as a 2 dimensional array, but you want to use a QList rather than an array?  Note that QList has only 1 dimension, but you could create a QList with each element being another QList.  Is this what you are after?
0
 
LVL 12

Assisted Solution

by:HappyCactus
HappyCactus earned 668 total points
ID: 33710691
QList is a mono-dimensional -like container, so is like you having all the data in a single "line". It's not what you want.
Why are you choosing a list instead of a dynamic array?
You could either use:
1) a QVector of QVector (if you have to append data at the end)
2) a QList of QVector (you can put new lines inside the list but only append data to the end of qvector)
3) a QList of QList (best flexibility, but you can access all data sequentially by using iterators).


what is your case?

0
 

Author Comment

by:obad62
ID: 33714518
In my case I don't have any problem with array except that I need to get the maximum vale for each column I wrote code to get that Max but as you know should I mention the size of array and I don't have any idea about that size.
0
 
LVL 12

Assisted Solution

by:HappyCactus
HappyCactus earned 668 total points
ID: 33714541
You do not have this problem, QVector is a dynamic array, that can grow in O(1) when data is appended at the end. You simply declare QVector and then use push_back() to append data.
See std::vector as reference, if QT is compiled with stdlib compatible containers  support.
0
 

Author Closing Comment

by:obad62
ID: 33829219
thanks for all
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses
Course of the Month8 days, 15 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question