The handle of the file is converted to file *!!!

Source: Internet
Author: User

Convert the file handle to file *

Recently, I asked a question in the Forum about converting the file handle into file. Unexpectedly, the C Runtime provides two functions: _ open_osfhandle and _ fdopen. Let's share it with you.

Void osfiletocrtfile ()
{
Handle hfile = createfile ("C: // test. dat", generic_read | generic_write, 0, null,
Open_always, file_attribute_normal, null );

Char sztext [] = "Hello world! /N ";
DWORD dwwritten;
Writefile (hfile, sztext, strlen (sztext), & dwwritten, null );
 
File * pfile = NULL;
Int nhandle = _ open_osfhandle (long) hfile, _ o_text | _ o_append );
If (nhandle! =-1)
Pfile = _ fdopen (nhandle, "WT ");
 
If (pfile)
{
Int n = fputs ("write by file *! ", Pfile );
Fflush (pfile); // write the file immediately
// N = 0
}
 
Closehandle (hfile );
}

However, the _ open_osfhandle function can only convert the handle of a physical file, but cannot convert the handle of a memory file. We do not know how to implement the following functions.If you have the answer, hurry up to the csdn community.Get the score.

/*
Problem Overview:
There is such a third-party library function void writedatatofile (File * pfile), which writes data to a file.
Now you need to use the writedatatofile function to generate temporary data, but cannot generate Disk Files (operations must be performed in memory ).
Then read the data in the memory in some way.

Process concept:
1,
Create a memory file
Get the file of this memory file *
Write Data
2,
Open the memory file
Get the file of the memory file *
Read the content of a memory file
Close
3,
Close
**************************************** **************************************** *
Problems to be Solved:
1. Can handle of the memory file be converted to file * (_ open_osfhandle is not allowed)
2. For memory file operations, you need to call pvoid pview = mapviewoffile (...); Through pview, you can change the content of the memory file.
Can I use file * to manipulate pview content in some way?
3. functions to be implemented (see the following test code)
File * getfileptrbymemfilehandle (handle hmemfile)
Void updatememfile (handle hmemfile)
4. If there is a problem with the solution, you can change it.

**************************************** **************************************** **
Implementation:
1. To facilitate testing and use the global file handle
2. Simulate and implement the writedatatofile Function

1 */
2 # define mf_name text ("mf_file_name ")
3 # define mf_size 4*1024
4 handle g_hmemfile = NULL;
5 // simulate the library function writedatatofile
6 void writedatatofile (File * pfile)
7 {
8 assert (pfile );
9 fputs ("write by LIB function! /N ", pfile );
10}
11
12 bool creatememfile ()
13 {
14g_hmemfile = createfilemapping (invalid_handle_value,
15 null, page_readwrite, 0,
16mf_size, mf_name );
17if (g_hmemfile! = NULL)
18 {
19if (getlasterror () = error_already_exists)
20 {
21 trace ("STL mapfile already exists .");
22 closehandle (g_hmemfile );
23 return false;
24}
25 return true;
26}
27 return false;
28}
29
30file * getfileptrbymemfilehandle (handle hmemfile)
31 {
32 //.
33 // specific implementation
34
35 //.
36 return NULL;
37}
38 void updatememfile (handle hmemfile)
39 {
40 //..
41 // implementation required
42}
43
44 void test ()
45 {
46 // 1 Write the memory file
47if (creatememfile () = false)
48 return;
49
50 file * pfile = getfileptrbymemfilehandle (g_hmemfile );
51 if (null = pfile)
52 {
53 closehandle (g_hmemfile );
54 return;
55}
56
57 writedatatofile (pfile );
58 // update the memory file
59 updatememfile (g_hmemfile );
60
61 // 2 read memory files
62 handle hfile = openfilemapping (file_map_read | file_map_write, false, mf_name );
63 If (hfile! = NULL)
64 {
65 file * preadfile = getfileptrbymemfilehandle (hfile );
66 If (preadfile)
67 {
68 char strbuffer [bufsiz];
69 int nptcount = 0;
70 while (fgets (strbuffer, bufsiz, preadfile )! = NULL)
71 {
72 trace (strbuffer );
73}
74}
75 closehandle (hfile );
76}
77 // 3 close
78 closehandle (g_hmemfile );
79}

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.