PDA

View Full Version : How to use QList to add many QFiles.



weixj2003ld
15th July 2020, 03:54
Hi,
I need write many files at the same time,so I define many files before use them and put them in QList. code as follows:

QList<QFile> writeFileList;

for(int i=0;i<count;i++)
{
QString writeFileName = writeDir+"/"+fileName[i];
QFile writeFile(writeFileName);
writeFile.open(QIODevice::WriteOnly);
QDataStream out(&writeFile);
writeFileList.append(writeFile);
}

but error occur:
C2248 "can not access private member declared in class 'QFile'" .

How to solve it ?thank you.

Ginsengelf
15th July 2020, 07:23
Hi, try storing pointers to QFile in the list.
By the way, your QDataStream object is a local variable for each iteration, and is destroyed again when a loop iteration is finished.

Ginsengelf

d_stranz
15th July 2020, 15:29
C2248 "can not access private member declared in class 'QFile'" .

This line:



writeFileList.append(writeFile);


is trying to execute the QFile copy constructor. Copying and assignment is not allowed for any class derived from QObject, so the copy constructor and operator=() are private in the QObject class and all classes derived from it. So basically, you cannot construct a QList of QFile objects.

Even if you stored QFile pointers as Ginsengelf suggested, your design of keeping a lot of open files around does not seem like a very good one. In Windows, there are a limited number of resource handles available to any program. If your list is large, then you could run out of handles (which are used for other things beside files) and your program would break because it can't open all the files in the list.

You should probably consider a different design for your program.

weixj2003ld
16th July 2020, 00:11
Thank you very much.How to design my program?Could you give me some advice?

ChrisW67
16th July 2020, 09:22
How to design my program?Could you give me some advice?
Only you know your requirements. Consider these things:

How many is "many files"? 5 is easy, 5000 would need a different approach.
What is being written? Text or binary blobs require different approaches.
How is it being written and how often? In response to an external event, or under your control. Every 50 milliseconds or every day. Different approaches needed.
The same thing to every file or different things? All the same... write one and duplicate it later
How close to real-time does it need to be? If you can accept a few seconds lag then other options are available.
Target files local or over a network? One performs better than the other.

weixj2003ld
16th July 2020, 12:04
Thank you for your answer very much.My requirement is :
First I read about 10-15 column dignal datum (about 50000 rows) from one text file,and then write them into 10-15 (the same as column number) binary files differently.All the operations are in a local computer.

Lesiok
16th July 2020, 12:43
First step : read all data to memory creating one list/vector for one column.
Second step : write data to out files one after the other. In this case, you need a list of file names and you only have one file open at a time.

d_stranz
16th July 2020, 15:59
First step : read all data to memory creating one list/vector for one column.
Second step : write data to out files one after the other. In this case, you need a list of file names and you only have one file open at a time.

This will work as long as the file can fit into memory, and a 15 x 50000 entry table is probably OK. If the files get to be too big to fit into memory, then there are at least two more options:

1 - Open the file using memory mapping and let the OS take care managing the memory. If the data is stored in the file in row x column order, then you might want to take your original approach of keeping 10 - 15 files open so the memory mapping will be efficient.

2 - Don't load the file into memory, but read through it 10 - 15 times, once for each column.

It really depends on how often you want to do this conversion. If it is something you do once for each batch of data, then it really doesn't matter if it takes 1 second or 10 seconds. If it is something you will do a lot (eg. you are trying to keep up with real-time data acquisition), then you want it to be as fast as possible.

weixj2003ld
17th July 2020, 00:24
Thank you all,I will try it.