QT file Operation _QT

Source: Internet
Author: User
Tags readline

This weekend, the morning friends bought a computer, last night deliberately down a netbook Meego 1.1, the morning up installed, the interface is OK, but I have been looking for a long time did not know how to shut down the machine. Fortunately found the terminal, input Shutdown command turned off the computer, then check the next found Meego is directly press power key off the machine.

Today learning Qt's file operation

1, Qiodevice
directly inherit from Qobject

The Qiodevice class is the base class for an input/output device.

Qiodevice provides a common implementation and an abstract interface for devices to read and write block data.
Qiodevice is an abstract class and cannot be instantiated.

Inherited by Q3socket,q3socketdevice,qabstractsocket,qbuffer,qfile,qlocalsocket,qnetworkreply,qprocess.


===============================================================================================

2, Qfile

Inherit from Qiodevice

The Qfile class is an input/output device for manipulating files.

Qfile is an input/output device used to read and write binaries and text files. Qfile can be used alone, but will be more convenient if used with Qdatastream or Qtextstream.

File names can usually be passed through constructors, but can also be set using SetName (). The directory delimiter is not supported on any operating system using the "/", "/". You can check whether a file exists by exists () and remove a file via remove (). More operating system-related advanced file system operations QT provides qfileinfo and Qdir classes.

The file can be opened with open (), closed with close (), and refreshed with flush (). Data can usually be read or written using Qdatastream or Qtextstream, but you can also use Read (), ReadLine (), ReadAll (), write (). Qfile also supports GetChar (), Putchar (), and Ungetchar ()

The size () can return the file to its sizes. You can use the POS () function to get the current file location or use Seek () to move to a new file location. If you go to the end of the file, AtEnd () returns True.

Example 1: Reading a file on one line

#include <QtCore/QCoreApplication> #include <QFile> #include <QDebug> #include <QTextCodec> int main (int argc, char *argv[]) {qcoreapplication A (argc, argv);//chinese support qtextcodec *codec = Qtextcodec::codecforname ("UT F-8 "); Qtextcodec::setcodecforcstrings (codec); Qfile file ("/home/administrator/testdir/test.txt"); if (!file.open (qiodevice::readonly | Qiodevice::text)) {qdebug () << "Can ' t open the file!" <<endl; while (!file.atend ()) {Qbytearray line = File.readline (); QString str (line); Qdebug () << str; return A.exec (); }

Using Qtextstream to read files

#include <QtCore/QCoreApplication> #include <QFile> #include <QDebug> #include <QTextCodec> int main (int argc, char *argv[]) {qcoreapplication A (argc, argv);//chinese support qtextcodec *codec = Qtextcodec::codecforname ("UT F-8 "); Qtextcodec::setcodecforcstrings (codec); QTEXTCODEC::SETCODECFORTR (codec); Qtextcodec::setcodecforlocale (codec); Qfile file ("/home/administrator/testdir/test.txt"); if (!file.open (qiodevice::readonly | Qiodevice::text)) {qdebug () << "Can ' t open the file!" <<endl; } Qtextstream in (&file); while (!in.atend ()) {QString line = In.readline (), Qdebug () << line;} return A.exec (); }

Qdatastream overloaded operator "<<" write data,>> read data

    #include <QtCore/QCoreApplication> #include <QFile> #include <QDebug> #include <QTextCodec> int main (int argc, char *argv[]) {qcoreapplication A (argc, argv);//chinese support qtextcodec *codec = Qtextcode C::codecforname ("UTF-8"); Qtextcodec::setcodecforcstrings (codec); QTEXTCODEC::SETCODECFORTR (codec); Qtextcodec::setcodecforlocale (codec); Qfile file ("/home/administrator/testdir/test.txt"); if (!file.open (Qiodevice::readwrite | Qiodevice::text)) {qdebug () << "Can ' t open the file!" <<endl; } Qtextstream Stream (&file); QString line_in; while (!stream.atend ()) {//line_in = Stream.readline ();//Qdebug () << line_in;//}//Stream.seek (Stream.pos () ); Stream.seek (File.size ());//Move the current read file pointer to the end of the file int count = 0; while (Count <) {Stream << Qobject::trutf8 ("New Line:") <<++count<< "/n"; stream.seek (0); Move the current read file pointer to the file start while (!stream.atend ()) {line_in = Stream.readline (); Qdebug () << line_in;} return A.exec (); } 

3, Qdir

The Qdir class provides access to the directory structure and their content.

The Qdir class provides a platform-independent way to access the directory structure and their content.
Qdir is used to manipulate path names, access information about paths and files, and manipulate the underlying file system.
Qdir uses a relative or absolute file path to point to a file. The absolute path is from the directory separator "/" or with a drive ID (except under Unix). If you always use the "/" as the directory delimiter, QT will convert your path to the underlying operating system. The relative filename is started by a directory name or file name and specifies a path relative to the current path.
For example, absolute path:
Qdir ("/home/administrator/soft");
Qdir ("D:/software");
We can use the isrelative () or Isabsolute () function to confirm whether the qdir is a relative or absolute path. Use Makeabsolute () to convert the qdir of the relative path to the qdir of the absolute path.

      #include <QtCore/QCoreApplication> #include <QDir> #include <QList> #include <QFileInfoList> #include <QDebug> #include <QTextCodec> int main (int argc, char *argv[]) {Qcor EApplication A (argc, argv); Qtextcodec *codec = Qtextcodec::codecforname ("GB2312"); Qtextcodec::setcodecforlocale (codec); Qtextcodec::setcodecforcstrings (codec); QTEXTCODEC::SETCODECFORTR (codec); Qdir d ("d:/"); D.setfilter (Qdir::files | Qdir::hidden | Qdir::nosymlinks | Qdir::alldirs);//listing files, listing hidden files (on Unix, starting with file), not listing symbolic links (operating systems that do not support symbolic connections will ignore) d.setsorting (Qdir::size | qdir::reversed)//sorted by file size, reverse sort order const Qfileinfolist list = D.entryinfolist ();//Return list of Qfileinfo objects for all directories and files in this directory Qfileinfolist::const_iterator iterator = List.begin (); Qdebug () << "Number of directories and files:" << d.count ()//Returns the number of directories and files found Qdebug () << "filename/t/t/tsize/t/t/t"; while (iterator!= list.end ()) {Qdebug () << (*iterator). FileName () << "/t/t/t" << (*iterator). Size (); iterator++; Qdebug () << "current directory:" << d.current ();//Returns the application's current directory. Qdebug () << "absolute path to current directory" << D.currentpath ();//returns the absolute path of the application's current directory. Const qlist<qstring> list = D.entrylist (); Returns a list of the names of all directories and files in this directory//Qlist<qstring>::const_iterator iterator = List.begin (); while (iterator!= list.end ()) {//Qdebug () << (*iterator);//iterator++;//} return A.exec (); }

Run Result:

4, Qfileinfo

The Qfileinfo class provides a platform-independent file information.

Qfileinfo provides information about the file name and location in the system, access rights and whether it is a symbolic link, file size and last modified/read/write time, and can also be used in QT resource files.

Qfileinfo can point to a file through a relative/absolute path.

You can use Isfile (), Isdir (), Issymlink (). Symlinktarget () function to provide the destination file for symbolic links.

Symbolic links in Unix (including Mac OS X) have the same size as the target file () because UNIX handles symbolic links transparently; Similarly, opening a symbolic connection uses the Qfile to effectively open the target of the link.

In Windows, the LNK file for symbolic links (shortcuts). Its size () is the size of the symbolic link (not the target of the link), opening a symbolic connection using the qfile and opening it. LNK file. For example

#include <QtCore/QCoreApplication> #include <QFileInfo> #include <QDebug> int main (int argc, char * Argv[]) {qcoreapplication A (argc, argv); Qfileinfo info1 ("E:/meego/vi_command.lnk"); Qdebug () << info1.issymlink (); Qdebug () << Info1.absolutefilepath (); Qdebug () << info1.size (); Qdebug () << info1.symlinktarget (); Qdebug () << "**********************"; Qfileinfo Info2 (Info1.symlinktarget ()); Qdebug () << info2.issymlink (); Qdebug () << Info2.absolutefilepath (); Qdebug () << info2.size (); return A.exec (); }

Run Result:

#include <QtCore/QCoreApplication> #include <QFileInfo> #include <QDebug> #include <qtextcodec > int main (int argc, char *argv[]) {qcoreapplication A (argc, argv); Qtextcodec *codec = Qtextcodec::codecforname ("GB2312"); Qtextcodec::setcodecforlocale (codec); Qtextcodec::setcodecforcstrings (codec); QTEXTCODEC::SETCODECFORTR (codec); Qfileinfo info ("e:/meego/basic Command and VI.Command.txt"); Qdebug () << info.basename ()//does not include suffix name qdebug () << info.bundlename ();//Only available on Mac OS, other platform is empty string Qdebug () < < Info.suffix ()//suffix name qdebug () << info.completebasename ();//not including suffix qdebug () << info.completesuffix (); /complete suffix name return a.exec (); }

Run Result:

    

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.