. 1 the QFile FP; // to include the necessary header file is omitted here 2 the QDir (the dir); . 3 QString path ( " ./ " ), filename ( " test.txt " ); . 4 QDebug qdebug (QtWarningMsg); . 5 FP. the setFileName (path + filename); // specified path comprising a file name for the fp . 6 IF (fp.exists ()) // if present, is read . 7 { . 8 QString (text); . 9 // char * tmp = NULL; 10 qdebug << " file exists "; 11 qdebug << dir.current (); // current path is not necessarily the path imagined 12 fp.open (QIODevice :: ReadOnly); // open and close should be closely related to 13 the while (! Fp.atEnd () ) 14 { 15 text = fp.readAll (); 16 } . 17 qdebug << " file content: " << endl << text << endl; 18 is fp.close (); . 19 } 20 is the else // if there , by the open operation of the new file 21 { 22 << qdebug " file does not exist " ; 23 24- fp.open (QIODevice :: ReadWrite | QIODevice :: Text); // absence, open the file that contains the new operating 25 fp.write ( " the I AM Writing file " ); 26 qdebug << " writing files " ; 27 fp.close (); 28 }
Note: The new veteran should pay attention to the opening and closing files to be paired, can not appear alone.