Qfile class of QT

Source: Internet
Author: User

Qfile
Class is the input/output device class of the Operation file. Qfile is an input/output device used to read/write binary and text files. Qfile can be used by itself or a more convenient class
Use datastream or qtextstream. The file name is usually transmitted through construction, but can be modified through setname. You can use exists () to check
Check whether a file exists and remove it by removing. Files are opened through open (), closed by close (), and updated by flush. Data is usually
. Qdatastream or qtextstream can be read and written, but you can read it through readblock () and Readline ().
Writeblock () Write. Qfile also supports getch (), ungetch (), and putch (). The file size can be returned through size. You can use
Use the at () function to obtain the current file location or move it to a new file location. Here is a code snippet that uses qtextstream to read a text file in one row. It uses a new
The row number prints each row.
   

 

 1 QStringList lines;
2     QFile file(“file.txt”);
3     if ( file.open(IO_ReadOnly))
4     {
5         QTextStream stream( &file );
6         QString line;
7         int i = 1;
8         while ( !stream . eof() )
9         {
10             line = stream . readLine();
11             printf( "%3d:%s\n",i++,line.latin1());
12             lines += line;
13         }
14         file . close();
15     }


 

It is as easy to write as text. The following example shows how to write data in the string list that we read in the previous example:
    

Qfile file ("file.txt ");
If (file. Open (io_writeonly ))
{
Qtextstream stream (& file );
For (qstringlist: iterator it = lines. Begin (); it! = Lines. End (); ++ it)
Stream <* It <"\ n ";
File. Close ();
}

 

Qt uses Unicode as the file name. If you want to use your own input and output on a UNIX system, you should use encodename and decodename () to convert the file name to your local code.

Common Methods:

1. qfile: qfile ()

Construct a qfile object without a name

2. qfile: qfile (const qstring & name)

Construct a qfile object named by name.
Note: You can also use qfile: qfile () and then call the setname () method to implement similar actions.

3. Boot qfile: atend () const [virtual function]

True is returned if the end of the file is reached; otherwise, false is returned;

4. Void qfile: Close () [virtual function]

Close an open file.
If the file is opened by an existing file handle, it cannot be closed. If the existing file handle is a file *, the file will be refreshed. If the existing file handle is an int-type file descriptor, nothing will be done for this file.

5. qstring qfile: decodename (const qcstring & localfilename) [static function]

This is an operation that uses the opposite of localfilename and qfile: encodename.

6. qcstring qfile: encodename (const qstring & filename) [static function]

When
When you use qfile, qfileinfo, and qdir to access the QT file system, you can use the Unicode file name. On UNIX systems, these file names are transferred
To an 8-bit encoding format. If you want to implement your own input/output file on UNIX, you need to use this function to convert the file name. In the window
On NT/2000, the file system directly supports Unicode-named files, and this sweat does not apply. In Windows 95
These events are not supported. By default, the function converts the file name to 8 as the local encoding format, depending on the user's work scenario. This provides users with a wide range of options for file naming. Text in the Application
The hardcoded file name should only use the 7-bit ASCII code as the file name character. The conversion scheme can be changed by using setencodingfunction. If you want
Can use UTF-8 encoding to name the right to store the file, it should be very useful, but you need to know such a file name may not be known when other programs are used.

7. bool qfile: exists (const qstring & filename) [static function]

If the file with the given filename name exists, true is returned; otherwise, false is returned.
Note: Another overload function is Boot qfile: exists () Const. If the file set by setname () exists, true is returned; otherwise, false is returned.

8. Void qfile: flush () [virtual function]

Refresh the files stored in the buffer and output them to the disk.
Note: Close () also writes files to the disk and clears the File Buffer.

9. Int qfile: getch () [virtual function]

Read a byte/character from a file.
Returns the byte/character to be read. -1 is returned if it reaches the end of the file.

10. Int qfile: handle () const

The handle of the returned file.
This is a short integer, similar to the use of C library functions such as fopen () and fcntl (), and similar to qsocketnotifier.
If the file is not opened or has an error, handle () returns-1.

11. qstring qfile: Name () const

Returns the name set by setname.

12. bool qfile: open (INT m) [virtual function]

13. bool qfile: open (int m, file * F)

This is an overloaded member function for convenience. It essentially resembles the above functions. It uses an existing file handle and opens it in M mode. If it succeeds, true is returned; otherwise, false is returned.
Example:
   

 

1 # include <stdio. h>
2
3 void printerror (const char * MSG)
4 {
5 qfile F;
6 F. Open (io_writeonly, stderr );
7 F. writeblock (MSG, qstrlen (MSG); // write data to stderr
8 F. Close ();
9}


 

When qfile uses this function to open a file, close () cannot actually close the file, only flushes.

Warning If F is stdin, stdout, stderr, you cannot locate the seek.

14. bool qfile: open (int m, int F)

This is an overloaded member function for convenience. Essentially, it is similar to the above functions. It uses an existing file descriptor to open the file in M mode. If the call succeeds, true is returned. Otherwise, false is returned. When qfile uses this function to open a file, close () cannot actually close the file.

The qfile file opened by this function will be automatically set to the mode without buffer, which means that the file input and output operations will be slow. If you reach release execution, you should try to use other open functions.

Warning If F is 0 (stdin), 1 (stdout), 2 (stderr), you cannot locate the seek. Size () is set to int_max (in limits. h)

15. Int qfile: putch (INT ch) [virtual function]

Write the CH character in the file.
Returns CH, or-1 if an error occurs.

16. qbytearry qiodevice: readall () [virtual function]

This convenient function returns all the data remaining on the device.

17. q_long qfile: Readline (char * P, q_ulong maxlen) [virtual function]

Read a row of data from the text.

The data read from the file is stored at the starting position of the character pointer P until a row ends or reaches the length of the maxlen byte. Returns the length of the read bytes or-1 if an error occurs. The returned content contains a line break.

This function is only effective when files are stored in the buffer. Avoid using Readline () to operate files after opening files with the io_raw mark.

18. q_long ofile: Readline (qstring & S, q_ulong maxlen)

This is an overload member function provided to facilitate operations. Its functions are similar to those above.

Reads a row of data from a file.

Read multiple bytes from the file and store them in string s until one row ends or reaches the length of the maxlen byte. Returns the length of the read bytes or-1 if an error occurs. The returned content contains a line break.

This function is only effective when files are stored in the buffer. Avoid using Readline () to operate files after opening files with the io_raw mark.

Note: This string is read in plain Latin1 mode, not Unicode.

19. bool qfile: Remove ()

Delete an object based on the currently set file name. If the call succeeds, true is returned. Otherwise, false is returned.
The file will be closed before it is removed.

20. bool qfile: Remove (const qstring & filename) [static function]

This is a useful member function that can be reloaded. Essentially, it functions similar to the above functions. It removes the file with the specified name. If the call succeeds, true is returned. Otherwise, false is returned.

21. Void qfile: setdecodingfunction (decoderfn f) [static function]

Warning: This function cannot be reloaded.
Sets the function for decoding 8-bit file names to F. The default uses the locale-specific 8-bit encoding.

22. Void qfile: setencodingfunction (encoderfn f) [static function]

Sets the function for encoding Unicode file names to F. The default encodes in the locale-specific 8-bit encoding.

23. Void qfile: setname (const qstring & name)

Set the object name to name. This name does not contain paths, relative paths, or absolute paths. If the file has been opened, do not call this function. If the file name does not contain the path or relative path, the current directory of the application will be used no matter when the open () call is executed.
Example:
  

  

1 qfile file;
2 qdir: setcurrent ("/tmp ");
3 file. setname ("readme.txt ");
4 qdir: setcurrent ("/home ");
5 file. Open (io_readonly); // the location where the file is opened "/home/readme.txt"


24. offset qfile: size () const [virtual function]

The size of the returned file.

25. Int qfile: ungetch (INT ch) [virtual function]

The output string ch is returned to the file, and the numeric value is reduced if it is not zero.

This function is usually called to cancel the getch () operation.

Return CH, or-1 when an error occurs.

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.