C-based file operations (FILE*, fstream, windowsAPI)

php是最好的语言
Release: 2018-08-10 11:27:39
Original
2465 people have browsed it

C-based file operations

In ANSI C, file operations are divided into two methods, namely streaming file operations and I/O file operations, which are introduced below.

1. Streaming file operation

This method of file operation has an important structure FILE. FILE is defined in the header file stdio.h as follows:

typedef struct {int level;unsigned flags;char fd;unsigned char hold;int bsize;unsigned char _FAR *buffer;unsigned char _FAR *curp;unsigned istemp;short token;
} FILE;
Copy after login

FILE This structure contains the basic attributes of file operations. File operations must be performed through the pointers of this structure. The commonly used functions for such file operations are shown in the table below. Function function
fopen() Open stream
fclose() Close the stream
fputc() Write a character to the stream
fgetc() Read a character from the stream
fseek() Locate the specified character in the stream
fputs() Write a string to Stream
fgets() Read a line or specify characters from the stream
fprintf() Output to the stream in format
fscanf() Read from the stream in format
feof() When the end of the file is reached Return true value
ferror() Return its value when an error occurs
rewind() Reset the file locator to the beginning of the file
remove() Delete the file
fread() Read the specified number from the stream characters
fwrite() writes the specified number of characters to the stream
tmpfile() generates a temporary file stream
tmpnam() generates a unique file name

The following is an introduction These functions

1.fopen()
The prototype of fopen is: FILE *fopen(const char *filename, const char *mode), fopen implements three functions

For use Open a stream
Connect a file to this stream
Return a FILR pointer to this stream
The parameter filename points to the file name to be opened, mode represents a string of open status, and its value is as shown in the following table

String meaning
r Open a read-only file, the file must exist.
 r Open a readable and writable file. The file must exist.
 rb opens a binary file for reading and writing, and only allows reading and writing data.
 rt Read and Write Opens a text file, allowing reading and writing.
 w Open a write-only file. If the file exists, the file length will be cleared to 0, that is, the file content will disappear. If the file does not exist, create the file.
 w Open a readable and writable file. If the file exists, the file length will be cleared to zero, that is, the file content will disappear. If the file does not exist, create the file.
 a Open a write-only file in append mode. If the file does not exist, the file will be created. If the file exists, the written data will be added to the end of the file, that is, the original content of the file will be retained. (EOF character is reserved)
 a Open a read-write file in append mode. If the file does not exist, the file will be created. If the file exists, the written data will be added to the end of the file, that is, the original content of the file will be retained. (The original EOF character is not retained)
wb Open or create a new binary file for writing only; only data writing is allowed.
 wb read and write opens or creates a binary file, allowing reading and writing.
 wt Read and write Open or create a text file; allows reading and writing.
 at opens a text file for reading and writing, allowing reading or appending data to the end of the text.
 ab opens a binary file for reading and writing, allowing reading or appending data to the end of the file.

A file can be opened in text mode or binary mode. The difference between the two is: in text mode, a carriage return is regarded as one character '\n', while in binary mode, it is regarded as two characters 0x0D, 0x0A; if 0x1B is read in the file, the text mode will think that this is the end of file character, that is, the binary model will not process the file, and the text mode will convert the data accordingly in a certain way.

The system default is to open in text mode. You can modify the value of all variables _fmode to modify this setting. For example, _fmode=O_TEXT; sets the default opening mode to text mode; and _fmode=O_BINARY; then Set the default opening mode to binary mode.

This function returns a FILE pointer, so there is no need to initialize it after declaring a FILE pointer. Instead, fopen() is used to return a pointer and connect it to a specific file. If it succeeds or fails, NULL is returned.

Example:

FILE *fp;
if(fp=fopen(“123.456”,”wb”))
puts(“Open file successfully”);
else
puts("Success or failure of opening the file");

2.fclose()
The function of fclose() is to close the file opened with fopen(). Its prototype is: int fclose( FILE *fp); If successful, return 0, fail to return EOF.

Be sure to remember to close the open file when the program ends, otherwise data may be lost. I have often made this mistake before.

Example: fclose(fp);

3.fputc()
Write a character to the stream, the prototype is int fputc(int c, FILE *stream); Return this character successfully , returns EOF on failure.

Example: fputc('X',fp);

4.fgetc()
Read a character from the stream, the prototype is int fputc(FILE *stream); Return successfully This character will return EOF if failed.

Example: char ch1=fgetc(fp);

  1. fseek()
    This function is generally used in files opened in binary mode. The function is to locate The specified position in the stream, the prototype is int fseek(FILE *stream, long offset, int whence); if it returns 0 successfully, the parameter offset is the number of characters moved, whence is the base of movement, and the value is

The base position of symbolic constant value
SEEK_SET 0 The beginning of the file
SEEK_CUR 1 The current reading and writing position
SEEK_END 2 The end of the file

Example: fseek(fp, 1234L,SEEK_CUR);//Move the read and write position backward 1234 bytes from the current position (L suffix indicates a long integer)

fseek(fp,0L,2);//Move the read and write position to End of file

6.fputs()
Write a string to the stream, prototype int fputs(const char *s, FILE *stream);

Example: fputs("I Love You”,fp);

7.fgets()
Read a line or specify characters from the stream, the prototype is char *fgets(char *s, int n, FILE *stream); from Read n-1 characters from the stream. Unless one line is read, the parameter s is used to receive the string. If successful, the pointer of s is returned, otherwise NULL is returned.

Example: If the text of the current position of a file is as follows

Love ,I Have

But……..

If you use

fgets(str1,4,file1);

After execution str1="Lov", 4-1=3 characters are read, and if

fgets(str1, 23,file1);

then execute str="Love,I Have" and read one line (excluding the '\n' at the end of the line).

8.fprintf()
Input to the stream according to the format, its prototype is int fprintf(FILE *stream, const char *format[, argument, …]); its usage is the same as printf(), But it is not written to the console, but to the stream.

Example: fprintf(fp,”-%s”,4,”Hahaha”);

9.fscanf()
Read from the stream in format, its prototype is int fscanf(FILE *stream, const char *format[, address, …]); its usage is the same as scanf(), but it is not read from the console, but Just read from the stream.

Example: fscanf(fp,”%d%d”,&x,&y);

10.feof()
Check whether the end of the file has been reached, and return true if it is, otherwise Returns 0, its prototype is int feof(FILE *stream);

Example: if(feof(fp))printf("The end of the file has been reached");

11.ferror()
The prototype is int ferror(FILE *stream); returns the latest error code of the stream, which can be cleared by clearerr(). The prototype of clearerr() is void clearerr(FILE *stream);

Example: printf(“%d”,ferror(fp));

12.rewind()
Return the current reading and writing position to the beginning of the file. The prototype is void rewind(FILE *stream); In fact, this The function is equivalent to fseek(fp,0L,SEEK_SET);

Example: rewind(fp);

13.remove()
Delete a file, the prototype is int remove(const char * filename); The parameter is the name of the file to be deleted, and 0 is returned if successful.

Example: remove(“c:\io.sys”);

14.fread()
Read the specified number of characters from the stream, the prototype is size_t fread(void ptr, size_t size, size_t n, FILE *stream); The parameter ptr is to save the read data. The pointer of void can be replaced by any type of pointer, such as char*, int *, etc.; size is the number of bytes per block; n is the number of blocks read. If successful, the actual number of blocks read (not the number of bytes) is returned. This function is generally used in files opened in binary mode.

Example:

char x[4230];
FILE *file1=fopen(“c:\msdos.sys”,”r”);

fread (x,200,12,file1);//Read a total of 200*12=2400 bytes

15.fwrite()
Corresponds to fread, writes specified data to the stream, prototype It is size_t fwrite(const void ptr, size_t size, size_t n, FILE *stream); the parameter ptr is the data pointer to be written. The pointer of void can be replaced by any type of pointer, such as char*, int * and so on to replace; size is the number of bytes of each block; n is the number of blocks to be written. If successful, return the actual number of blocks written (not the number of bytes). This function is generally used for binary mode opening in the file.

Example:

char x[]=”I Love You”;
fwire(x, 6,12,fp);//Write 6*12=72 bytes

Will write "I Love" to the stream fp 12 times, a total of 72 bytes

16.tmpfile()
The prototype is FILE *tmpfile(void); generates a Temporary file, opened in "w b" mode, and returns the pointer of this temporary stream, or NULL if failed. At the end of the program, this file will be automatically deleted.

Example: FILE *fp=tmpfile();

17.tmpnam();
The prototype is char *tmpnam(char *s); generates a unique file name, In fact, tmpfile() calls this function. The parameter s is used to save the obtained file name and return this pointer. If it fails, NULL is returned.

Example: tmpnam(str1);

2. Direct I/O file operation

This is another file operation provided by C, which is through direct storage/ Fetch the file to complete the file processing, and the streaming file operation mentioned in the previous article is performed through the buffer; the streaming file operation is performed around a FILE pointer, and this type of file operation is performed around the "handle" of a file Let's proceed, what is a handle? It is an integer and a unique token used by the system to identify a file (in WINDOWS, the concept of handle is extended to the identification of all device resources). The commonly used functions for such file operations are as follows. These functions and some of the symbols used are defined in io.h and fcntl.h. When using them, the corresponding header files must be added.

Function description
open() opens a file and returns its handle
close() closes a handle
lseek() locates the specified location of the file
read() block Read the file
write() Write the file in blocks
eof() Test whether the file is ended
filelength() Get the file length
rename() Rename the file
chsize() Change the file length

These functions are explained one by one below:

1.open()
Open a file and return its handle. If it fails, a value less than 0 will be returned. The prototype is int open(const char *path, int access [, unsigned mode]); The parameter path is the name of the file to be opened, access is the mode to open, and mode is optional. Indicates the attributes of the file and is mainly used in UNIX systems. This parameter has no meaning in DOS/WINDOWS. The file opening modes are as follows.

Symbol meaning Symbol meaning Symbol meaning
O_RDONLY Read-only mode O_WRONLY Write-only mode O_RDWR Read/write mode
O_NDELAY Used for UNIX systems O_APPEND Append mode O_CREAT Create the file if it does not exist
O_TRUNC Truncate the file length to 0. O_EXCL is used together with O_CREAT. If the file exists, an error O_BINARY is returned. Binary mode
O_TEXT Text mode

For multiple requirements, you can use the "|" operator to connect, such as O_APPEND|O_TEXT Indicates opening the file in text mode and append mode.

Example: int handle=open(“c:\msdos.sys”,O_BINARY|O_CREAT|O_WRITE)

2.close()
Close a handle, the prototype is int close (int handle); If successful, return 0

Example: close(handle)

3.lseek()
Locate to the specified location, the prototype is: long lseek(int handle, long offset, int fromwhere); The parameter offset is the amount of movement, fromwhere is the reference position of movement, the value is the same as fseek() mentioned earlier, SEEK_SET: file header; SEEK_CUR: current file position; SEEK_END: ​​file end. This function returns the new access location of the file after execution.

Example:

lseek(handle,-1234L,SEEK_CUR);//Move the access position forward 1234 bytes from the current position.
x=lseek(hnd1,0L,SEEK_END);//Move the access position to the end of the file, x=the position of the end of the file, which is the file length

4.read()
From the file Read a block, the prototype is int read(int handle, void *buf, unsigned len); the parameter buf saves the read data, and len is the byte read. The function returns the actual bytes read.

Example: char x[200];read(hnd1,x,200);

5.write()
Write a piece of data to the file, the prototype is int write(int handle, void *buf, unsigned len); The meaning of the parameters is the same as read(), and the actual written bytes are returned.

Example: char x[]=”I Love You”;write(handle,x,strlen(x));

7.eof()
Similar to feof(), Tests whether the file is ended, returns 1, otherwise returns 0; the prototype is: int eof(int handle);

Example: while(!eof(handle1)){……};

8.filelength()
Returns the file length, the prototype is long filelength(int handle); equivalent to lseek(handle,0L,SEEK_END)

Example: long x=filelength(handle);

9.rename()
Rename the file, the prototype is int rename(const char *oldname, const char *newname); The parameter oldname is the old file name, and newname is the new file name. Successfully returns 0

Example: rename(“c:\config.sys”,”c:\config.w40”);

10.chsize();
Change the file length , the prototype is int chsize(int handle, long size); the parameter size represents the new length of the file, returns 0 successfully, otherwise returns -1. If the specified length is less than the file length, the file will be truncated; if the specified length is greater than the file length, add '\0' at the end of the file.

Example: chsize(handle,0x12345);


Same as streaming file operation, this also provides functions for Unicode character operations, such as _wopen(), etc. It is used for wide character programming under 9X/NT. If you are interested, you can check BCB's help yourself.

In addition, this kind of operation also has lock(), unlock(), locking() and other functions for multi-user operations, but they are not used much in BCB, so I will not introduce them. , but if you want to use C to write CGI, these are necessary common sense. If you have requirements in this regard, you have to look at the help yourself.

C-based file operation

In C, there is a stream class. All I/O is based on this "stream" class, including what we need to know For file I/O, the stream class has two important operators:

1. Inserter (<<)
Output data to the stream. For example, the system has a default standard output stream (cout), which generally refers to the monitor. Therefore, cout<<"Write Stdout"<<'\n'; means the string "Write Stdout" and newline characters ('\n') to the standard output stream.

2. Extractor (>>)
Input data from the stream. For example, the system has a default standard input stream (cin), which generally refers to the keyboard. Therefore, cin>>x; means reading a specified type (that is, the type of variable x) from the standard input stream. data.

In C, file operations are implemented through the stream subclass fstream (file stream). Therefore, if you want to operate files in this way, you must add the header file fstream.h. Let's go through the file operation process one by one.

1. Open the file
In the fstream class, there is a member function open(), which is used to open the file. Its prototype is:

void open(const char* filename,int openmode,int access);

Parameters:

filename: The name of the file to be opened
mode: The way to open the file
access: Attributes of opening files
The way to open files is defined in class ios (which is the base class of all streaming I/O classes). Commonly used values ​​are as follows:

ios::app: Open the file in append mode
ios::ate: After the file is opened, it is positioned at the end of the file, ios:app contains this attribute
ios::binary: Open in binary mode File, the default mode is text mode. The difference between the two methods is shown in the previous article
ios::in: The file is opened in input mode (file=>program)
ios::out: The file is opened in output mode (program=>file)
ios::nocreate: Does not create the file, so opening fails when the file does not exist
ios::noreplace: Does not overwrite the file, so opening the file fails if the file exists
ios::trunc: If the file exists, replace the file The length is set to 0
You can use "or" to connect the above attributes, such as ios::out|ios::binary

The attribute value of the open file is:

0: Ordinary file, open access
1: Read-only file
2: Hidden file
4: System file
You can use "or" or " " to connect the above attributes, such as 3 or 1 | 2 is to open the file with read-only and implicit attributes.

For example: open the file c:\config.sys in binary input mode

fstream file1;
file1.open("c:\config.sys",ios::binary| ios::in,0);

If the open function has only one parameter, the file name, it is opened for reading/writing ordinary files, that is:

file1.open(“c:\config .sys”);<=>file1.open(“c:\config.sys”,ios::in|ios::out,0);

In addition, fstream also has the same functions as open( ) same constructor, for the above example, the file can be opened when it is defined:

fstream file1("c:\config.sys");

Specially proposed is , fstream has two subclasses: ifstream (input file stream) and ofstream (outpu file stream), ifstream opens the file in input mode by default (file => program), and ofstream opens the file in output mode by default.

ifstream file2(“c:\pdos.def”);//Open the file in input mode
ofstream file3(“c:\x.123”);//Open the file in output mode

So, in actual applications, choose different classes to define according to different needs: if you want to open in input mode, use ifstream to define; if you want to open in output mode, use ofstream to define; If you want to open it in input/output mode, use fstream to define it.

2. Close the file
The opened file must be closed after use. fstream provides the member function close() to complete this operation, such as: file1.close(); Just close the file connected to file1.

3. Reading and writing files
Reading and writing files is divided into text files and binary files. Reading text files is relatively simple, using inserters and extractors. That’s it; but for binary reading, it is more complicated. The following will introduce these two methods in detail

1. Reading and writing text files
Text file Reading and writing is very simple: use the inserter (<<) to output to the file; use the extractor (>>) to input from the file. Assume that file1 is opened as input and file2 is opened as output. The example is as follows:

file2<<"I Love You";//Write the string "I Love You" to the file
int i;
file1>>i;//From File input is an integer value.

This method also has a simple formatting capability, for example, you can specify the output to be hexadecimal, etc. The specific formats are as follows

Manipulator function input/output
dec formats decimal numeric data input and output
endl outputs a newline character and flushes this stream output
ends outputs a null character output
hex formats hexadecimal numeric data input and output
oct Formatted as octal numerical data input and output
setpxecision(int p) Sets the number of precision digits for floating point output

For example, to output 123 as hexadecimal: file1<<< ;123;To output 3.1415926 with 5-digit precision: file1<<<3.1415926.

2. Reading and writing of binary files
①put()
put() function writes a character to the stream. Its prototype is ofstream &put(char ch). Use It is also relatively simple, such as file1.put('c'); which is to write a character 'c' to the stream.

②get()
The get() function is more flexible and has 3 commonly used overloaded forms:

One is the form corresponding to put(): ifstream &get(char &ch );The function is to read a character from the stream, and the result is saved in the reference ch. If it reaches the end of the file, a null character is returned. For example, file2.get(x); means reading a character from the file and saving the read character in x.

The prototype of another overloaded form is: int get(); This form returns a character from the stream. If the end of the file is reached, EOF is returned, such as x=file2.get(); and The function of the above example is the same.

There is another form of prototype: ifstream &get(char *buf,int num,char delim='\n'); this form reads characters into the array pointed to by buf until it is read in num characters or the character specified by delim is encountered. If the delim parameter is not used, the default newline character '\n' will be used. For example:

file2.get(str1,127,'A');//Read characters from the file to string str1, terminate when character 'A' is encountered or 127 characters are read .

③Read and write data blocks
To read and write binary data blocks, use the member functions read() and write(). Their prototypes are as follows:

read(unsigned char *buf,int num );
write(const unsigned char *buf,int num);

read() reads num characters from the file into the buffer pointed to by buf. If num characters have not been read yet When the end of the file is reached, you can use the member function int gcount(); to obtain the actual number of characters read; and write() writes num characters from the cache pointed to by buf to the file. It is worth noting that the cache type is unsigned char *, sometimes type conversion may be required.

Example:

unsigned char str1[]=”I Love You”;
int n[5];
ifstream in(“xxx.xxx”);
ofstream out(“yyy.yyy”);
out.write(str1,strlen(str1));//Write all string str1 to yyy.yyy
in.read((unsigned char* )n,sizeof(n));//Read the specified integer from xxx.xxx, pay attention to type conversion
in.close();out.close();

four , Detect EOF
Member function eof() is used to detect whether the end of the file is reached. If the end of the file is reached, a non-0 value is returned, otherwise 0 is returned. The prototype is int eof();

Example: if(in.eof())ShowMessage("The end of the file has been reached!");

5. File positioning
and C The difference in file operations is that the C I/O system manages two pointers associated with a file. One is the read pointer, which indicates the location of the input operation in the file; the other is the write pointer, which indicates the location of the next write operation. Each time input or output is performed, the corresponding pointer changes automatically. Therefore, C's file positioning is divided into read position and write position positioning. The corresponding member functions are seekg() and seekp(). seekg() is to set the read position, and seekp is to set the write position. Their most common forms are as follows:

istream &seekg(streamoff offset,seek_dir origin);
ostream &seekp(streamoff offset,seek_dir origin);

streamoff is defined in iostream.h, Define the maximum value that can be obtained by offset. Seek_dir represents the base position of movement. It is an enumeration with the following values:

ios::beg: At the beginning of the file
ios::cur: File current position
ios::end: End of file
These two functions are generally used for binary files, because text files may differ from expected values ​​due to the system's interpretation of characters.

Example:

file1.seekg(1234,ios::cur);//Move the read pointer of the file backward 1234 bytes from the current position
file2.seekp( 1234,ios::beg);//Move the write pointer of the file back 1234 bytes from the beginning of the file


WINAPI-based file operations

WINAPI provides two One group of file operation functions is for compatibility with 16-bit programs and is relatively simple. The other group is specially designed for 32-bit programs and is more troublesome to use. Below I will list these two groups of functions. An introduction:

1. A set of functions compatible with 16-bit programs

⑴_lopen
Prototype: HFILE _lopen(
LPCSTR lpPathName, // file Name
int iReadWrite //File access method
);

Function: Open the file and return its handle successfully. Similar to this, there is also an OpenFile() function. You can check the help file by yourself. .

Parameter description: lpPathName is the file name to be opened, iReadWrite is the file access method, there are three main methods:

OF_READ: Open in read-only mode
OF_READWRITE: Open in read-only mode Open in write mode
OF_WRITE: Open in write-only mode
There are also attributes such as OF_SHARE_COMPAT. Since they are not commonly used, I will not introduce them one by one.

⑵_lclose()
Prototype: HFILE _lclose( HFILE hFile);

Function: Close the file, return 0 successfully

Parameter description: hFile: handle to be closed

⑶_lread()
Prototype: UINT _lread( HFILE hFile, // File handle
LPVOID lpBuffer, // Buffer to save data
UINT uBytes // Length to be read
);

Function: Read the file and return the actual number of characters read. Similar to this, there is a _hread() function, you can check the help file by yourself.

⑷_lwrite()
Prototype: UINT _lwrite( HFILE hFile, // File handle
LPCSTR lpBuffer, // Buffer to save data
UINT uBytes // Length to be written
);

Function: Write a file and return the actual number of characters written. Similar to this, there is a _hwrite() function, you can check the help file by yourself.

⑸_llseek()
Prototype: LONG _llseek( HFILE hFile, // File handle
LONG lOffset, // Amount of movement
int iOrigin // Base position of movement
) ;

Function: Move the read and write position of the file, and successfully return the read and write position of the file after the move

Parameter description: The value of iOrigin is one of the following three situations:

FILE_BEGIN: File header
FILE_CURRENT: Current file location
FILE_END: ​​File tail
⑹_lcreat()
Prototype: HFILE _lcreat( LPCSTR lpPathName, //The file name to be created
int iAttribute // File attribute
);

Function: Create a file and return its handle successfully

Parameter description: The file attribute is the sum of the following values:

0: Ordinary file
1: Read-only file
2: Hidden file
4: System file
The usage of these functions is similar to the listed BCB library functions. It is recommended to use BCB library functions. Please refer to the previous file operations based on BCB library functions.

2. 32-bit program compatible

CreateFile
Open file
To read and write files, you must first obtain a file handle, which can be obtained through this function File handle, this function is the door to the file world.

ReadFile
Read byte information from the file.
After opening the file and obtaining the file handle, you can read data through this function.

WriteFile
Write byte information to the file.
You can also pass the file handle to this function to write file data.

CloseHandle
Close the file handle.
After opening the door, you must remember to close it.

GetFileTime
Get the file time.
There are three file times available: creation time, last access time, and last write time.
This function also requires the file handle as the entry parameter.

GetFileSize
Get the file size.
Since the file size can be as high as several G (1G requires 30 bits), a 32-bit double-byte type cannot accurately express it, so the return code represents the lower 32 bits, and there is an exit parameter that can pass out the high 32 bits.
This function also requires the file handle as the entry parameter.

GetFileAttributes
Get file attributes.
You can get the archive, read-only, system, hidden and other attributes of the file.
This function only requires a file path as a parameter.

SetFileAttributes
Set file attributes.
If it can be obtained, it should naturally be able to be set.
You can set the archive, read-only, system, hidden and other attributes of the file.
This function only requires a file path as a parameter.

GetFileInformationByHandle
Get all file information
This function can obtain the information that all the above functions can obtain, such as size, attributes, etc., and also includes some information that cannot be obtained elsewhere, such as: files Label, index and link information.
This function requires the file handle as the entry parameter.

GetFullPathName
Get the file path. This function gets the full path name of the file.
It should be reminded that the result is correct only when the file is in the current directory. If you want to get the real path. The GetModuleFileName function should be used.

CopyFile
Copy file
Note: Only files can be copied, not directories

MoveFileEx
Move files
You can move both files and directories , but cannot span drive letters. (Setting the move flag under Window2000 can achieve cross-drive letter operations)

DeleteFile
Delete file

GetTempPath
Get the Windows temporary directory path

GetTempFileName
Create a unique temporary file in the Windows temporary directory path

SetFilePoint
Move the file pointer.
This function is used for advanced reading and writing operations on files.

Lock and unlock files

LockFile
UnlockFile
LockFileEx
UnlockFileEx

The above four functions are used to lock and unlock files. This enables asynchronous operation of files. You can perform separate operations on different parts of the file at the same time.

Compression and decompression of files

LZOpenFile
Open compressed file for reading

LZSeek
Find a location in the compressed file

LZRead
Read a compressed file

LZClose
Close a compressed file

LZCopy
Copy the compressed file and expand it during processing

GetExpandedName
Return the file name from the compressed file.

The above six functions are a small extension library in the 32-bit API and functions in the file compression extension library. File compression can be created with the command compress.

File imaging/mapping

The 32-bit API provides a feature called file imaging/mapping, which allows files to be mapped directly into an application's virtual memory space. This technology can be used to simplify and accelerated file access.

CreateFileMapping
Create and name the mapping

MapViewOfFile
Load the file mapping into memory

UnmapViewOfFile
Release the view and write the changes back to the file

FlushViewOfFile
Refresh the view changes and write them to disk

Related recommendations:

C# Write file operation-Geek Academy C# video tutorial

C# Examples of operations such as creating, inserting tables, and setting styles in Word documents

The above is the detailed content of C-based file operations (FILE*, fstream, windowsAPI). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template