Solved

read from file and save data in QList

Posted on 2010-09-18
10
1,771 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

912 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now