......................... QString dirname = "C:\\Local Web\\"; QDir directory = QDir( dirname ); if ( fileName.isEmpty() ) fileName = "*"; files = directory.entryList( QStringList( fileName ), QDir::Files | QDir::NoSymLinks ); unitName = "uniter.txt"; findFiles( directory, files, unitName );...............................void NewspaperWindow::findFiles( const QDir &directory, const QStringList &files, const QString &text){ QProgressDialog progressDialog( this ); progressDialog.setCancelButtonText( tr( "&Cancel" ) ); progressDialog.setRange( 0, files.count() ); progressDialog.setWindowTitle( tr( "Find Files" ) ); QStringList foundFiles;QFile userfile(text); if ( userfile.open( QIODevice::WriteOnly | QIODevice::Text ) ) { QTextStream out(&userfile); for ( int i = 0; i < files.count(); ++i ) { progressDialog.setValue( i ); progressDialog.setLabelText( tr( "Searching file number %1 of %2..." ) .arg( i ).arg( files.count() ) ); qApp->processEvents(); if ( progressDialog.wasCanceled() ) break; QFile file( directory.absoluteFilePath( files[ i ] ) ); if ( file.open( QIODevice::ReadOnly ) ) { QTextStream in(&file); while ( !in.atEnd() ) { if ( progressDialog.wasCanceled() ) break; //userfile->write( file.readAll() ); QByteArray data = file.readAll(); QTextCodec *codec = Qt::codecForHtml( data ); QString str = codec->toUnicode( data ); QTextDocument * doc = new QTextDocument(this); if ( Qt::mightBeRichText( str ) ) { doc->setHtml( str ); str = doc->toPlainText(); } else { str = QString::fromLocal8Bit( data ); } out << str; foundFiles << files[ i ]; } } } } userfile.close();
QFile p_file = new Qfile("x:/tempdir/example.txt");
struct MyFile{ int count; QFile ** files;};MyFile * file = new MyFile;file->count = 1;file->files = new (QFile *)[count];QByteArray * data;if ( file->count < 1 ){ return; // what? memory leak?}file->files[0] = new QFile( "a:/b/c.txt" );if ( !file->files[0]->open( QIODevice::ReadOnly ) ){ // return? what? memory leak again? // no! goto destruct section goto destruct;}data = new QByteArray( file->files[0]->readAll() );...destruct:delete data; // what? forgot to NULL pointer?delete file->files[0];delete file->files; // what? forgot to use delete[] ?delete file;
data = new QByteArray( file->files[0]->readAll() );
delete data; // what? forgot to NULL pointer?delete file->files[0];delete file->files; // what? forgot to use delete[] ?delete file;
file->files = new (QFile *)[count];
QFile file( a:/b.c.txt );if ( !file.open( QIODevice::ReadOnly ) ) return;QByteArray data = file.readAll();
/tmp/delfirst/example1.txt/tmp/delfirst/example2.txt/tmp/delfirst/example3.txt.../tmp/delfirst/example50.txt
for(int i = 1; i <=ui.numberofPages->text().toInt(); i++){ delete fileList[i];}
qDeleteAll( fileList );fileList.clear();