Solved

read from file and save data in QList

Posted on 2010-09-18
10
1,850 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
  • 4
  • 2
  • 2
  • +1
10 Comments
 
LVL 33

Accepted Solution

by:
pgnatyuk earned 167 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 167 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
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!

 
LVL 9

Assisted Solution

by:JohnGaby
JohnGaby earned 166 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 166 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 167 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 167 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to convert MFC APP to Win32 APP. 19 109
Unable to start eclipse ? 17 162
What is the Best Editor for PHP Development ? 5 88
IntelliJ and Eclipse Neon 2 installations 4 32
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

685 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