Write to many files at once
-
Hi. I'd like to write "hello" to 1000 files so that the filenames are generated automatically. So far I have this:
QString Filename = "filename"; QFile **db = new QFile*[100]; for(i=0; i<100; i++) { db[i]->setFileName(Filename.append(i.toString() )); if(!db[i]->open(QIODevice::Append | QIODevice::Text)) {exit(1);} QTextStream out_stream(&db[i]); out_stream << "hello"; db[i]->close();
However this doesn't work. May I ask someone to help me with this a bit. Thanks!
-
@t021 said in Write to many files at once:
this doesn't work
Think about what you are doing here:
QFile **db = new QFile*[100];
here lies the problem.BTW:
for(i=0; i<100; ++i){ QFile tempFile(QStringLiteral("filename%1").arg(i)); if(!tempFile.open(QIODevice::WriteOnly| QIODevice::Text)) continue; QTextStream out_stream(&tempFile); out_stream << "hello"; }
-
@VRonin said in Write to many files at once:
for(i=0; i<100; ++i){
QFile tempFile(QStringLiteral("filename%1").arg(i));
if(!tempFile.open(QIODevice::WriteOnly| QIODevice::Text)) continue;
QTextStream out_stream(&tempFile);
out_stream << "hello";
}Thank you very much. I cannot use QStringLiteral because I'm using Qt4.
My actual problem is more complicated. I don't just have to write "hello" to each file and forget about them. I need to periodically visit these 1000 files and write more to them. In the next step I may need to write "good bye" to files 217 and 389 only. This is why I need an array of pointers to handle my files. Thanks again!
-
-
@VRonin said in Write to many files at once:
QFile* db[100];
for(int i=0;i<100;++i){
db[i]=new QFile(QString::fromLatin1("filename%1").arg(i));
if(!db[i]->open(QIODevice::WriteOnly| QIODevice::Text)) Q_ASSERT(false);
}
for(i=0; i<100; ++i){
QTextStream out_stream(db[i]);
out_stream << "hello";
}Thank you very much VRONIN! This is exactly what I need, it solved my problem!