|
| bool | openFile (const std::string &fileName, const bool &create=true, const int &multiple=0, const size_t &size=0, const mode_t &mode=0666) |
| | Open a file. More...
|
| |
| bool | closeFile (const bool &del=false) |
| | Close the opened file. More...
|
| |
| | ~File () |
| | Destructor. More...
|
| |
| bool | isOpen () |
| | Check if the object has opened a file. More...
|
| |
| bool | isBinary () |
| | Check if the object has opened the file in binary mode. More...
|
| |
| std::string | getFileName () |
| | Get the name of the opened file. More...
|
| |
| uint64_t | getFileLine () |
| | Get the number of lines in the opened file. More...
|
| |
| size_t | getFileSize () |
| | Get the size of the file opened in binary mode. More...
|
| |
| size_t | getSize1 () |
| | Get the size of the file opened in binary mode in memory. More...
|
| |
| bool | lockMemory () |
| | Read data from disk into memory. More...
|
| |
| bool | unlockMemory (const bool &rec=false) |
| | Write data from memory to disk. More...
|
| |
|
| int | findC (const std::string &targetString, const int linePos=1) |
| | Find a line. More...
|
| |
| bool | appendLineC (const std::string &data, const int &linePos=0) |
| | Insert a line. More...
|
| |
| bool | deleteLineC (const int &linePos=0) |
| | Delete a line. More...
|
| |
| bool | deleteAllC () |
| | Delete all lines. More...
|
| |
| bool | chgLineC (const std::string &data, const int &linePos=0) |
| | Modify a line. More...
|
| |
| bool | readLineC (std::string &data, const int linePos) |
| | Read a single line. More...
|
| |
| std::string & | readC (std::string &data, const int &linePos, const int &num) |
| | Read multiple lines. More...
|
| |
| std::string & | readAllC (std::string &data) |
| | Read all lines. More...
|
| |
|
| bool | readC (char *data, const size_t &pos, const size_t &size) |
| | Read a data block. More...
|
| |
| bool | writeC (const char *data, const size_t &pos, const size_t &size) |
| | Write a data block. More...
|
| |
| void | formatC () |
| | Format the data. More...
|
| |
|
| int | find (const std::string &targetString, const int linePos=1) |
| | Find a line. More...
|
| |
| bool | appendLine (const std::string &data, const int &linePos=0) |
| | Insert a line. More...
|
| |
| bool | deleteLine (const int &linePos=0) |
| | Delete a line. More...
|
| |
| bool | deleteAll () |
| | Delete all lines. More...
|
| |
| bool | chgLine (const std::string &data, const int &linePos=0) |
| | Modify a line. More...
|
| |
| bool | readLine (std::string &data, const int linePos) |
| | Read a single line. More...
|
| |
| std::string & | read (std::string &data, const int &linePos, const int &num) |
| | Read multiple lines. More...
|
| |
| std::string & | readAll (std::string &data) |
| | Read all lines. More...
|
| |
|
| bool | read (char *data, const size_t &pos, const size_t &size) |
| | Read a data block. More...
|
| |
| bool | write (const char *data, const size_t &pos, const size_t &size) |
| | Write a data block. More...
|
| |
| void | format () |
| | Format the data. More...
|
| |
A class for reading and writing disk files.
- Note
- 1. The same object of this class can ensure synchronization and thread safety.
-
2. If the same file is operated by objects of this class, synchronization can also be ensured. However, note that when opening the file, use either an absolute path or a relative path consistently.
-
3. There are two modes when operating files with this class: 1. Read from disk + operate on data in memory + save to disk; these three steps are done separately, suitable for scenarios with complex custom operations. 2. Combine the three operations into one step.
| bool stt::file::File::openFile |
( |
const std::string & |
fileName, |
|
|
const bool & |
create = true, |
|
|
const int & |
multiple = 0, |
|
|
const size_t & |
size = 0, |
|
|
const mode_t & |
mode = 0666 |
|
) |
| |
Open a file.
- Parameters
-
| fileName | The name of the file to open (can be an absolute or relative path). |
| create | true: Create the file (and directory) if it does not exist. false: Do not create the file if it does not exist (default is true). |
| multiple | When >= 1, open the file in binary mode. This value is the ratio of the预定 file space size required for operating the file to the original file size. When < 1, open the file in text mode (default is 0, open in text mode). |
| size | When the file size is 0 and the binary mode is enabled by the parameter multiple, since 0 multiplied by any number is 0, the parameter multiple will be invalid. In this case, manually enter the预定 file space size (in bytes) (default is 0). |
| mode | If create is true and the file does not exist, use a bit mask to represent the permissions of the newly created file (default is 0666, rw- rw- rw-). |
- Returns
- true: File opened successfully.
-
false: Failed to open the file.
- Note
- Any operation requires opening the file first.
- Warning
- If the reserved space in binary mode is too small, it may cause a segmentation fault.
Example code 1: Open a file named "test" in the same directory as the program in text mode.
File f;
f.openFile("test");
Example code 2: Open a file named "test" in the same directory as the program in binary mode, intended for read-only.
File f;
f.openFile("test",true,1);
Example code 3: Open a file named "test" in the same directory as the program in binary mode, expecting to write data. The size after writing will not exceed twice the original file size.
File f;
f.openFile("test",true,2);
Example code 4: Open a file named "test" in the same directory as the program in binary mode, expecting to write data. The written size will not exceed 1024 bytes, and the original file size is 0.
File f;
f.openFile("test",true,1,1024);