Solved

read from file and save data in QList

Posted on 2010-09-18
10
1,750 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Best IDE for PHP starters 11 101
Eclipse Neon and jdk 1.8.0 11 72
White board coding practice 3 61
eclipse formatting 6 43
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
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…

744 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

11 Experts available now in Live!

Get 1:1 Help Now