File Operations: Learning createfile and writefile

Source: Internet
Author: User

Today's task is to save a file. It usually depends on how others write data. I just read it myself, but I haven't written it myself. I don't know much about the parameters of each API. When I was using it today, I encountered some problems.

Let's talk about the problem first:
The first problem: when using writefile, I directly wrote the wide string into the file. The file shows as many garbled characters as you may think. But there are rules. So I quickly realized that this requires converting the wide string into ASCII code.
The second problem is that after I open the file, I write the file again. At this time, the operation fails. So there is no way to solve this problem. Is it because of some restrictions on the parameters of createfile?

Because of these two problems, I also took a good look at the SDK documentation.
Let's take a look at the prototype and parameter introduction of createfile and writefile:

Handle Createfile (
Lpctstr Lpfilename,//File Name
DWORD Dwdesiredaccess,//Access Method
DWORD Dww.mode,//Sharing Mode
Lpsecurity_attributes Lpsecurityattributes,//Set to null
DWORD Dwcreationdisposition,///Creation Method
DWORD Dwflagsandattributes,//Attribute
Handle Htemplatefile
);

Bool Writefile (

 Handle Hfile,//File handle

 Lpcvoid Lpbuffer,//Contains data written to a file

 DWORD Nnumberofbytestowrite,//Number of strings contained in the Data

 Lpdword Lpnumberofbyteswritten,

 Lpoverlapped Lpoverlapped

);

The program I wrote for the first time is very simple.

Bool writeownfile (tchar * pfilename, tchar * pbuffer, DWORD dwlen)

{

Handle hfile = createfile (pfilename,

Generic_write,

File_pai_write,

Null,

Create_always,

File_attribute_normal,

Null

);

 

If (invalid_handle_value! = Hfile)

{

DWORD dwsize = 0;

Writefile (hfile, pbuffer, dwlen, & dwsize, null );

Closehandle (hfile );

Return true;

}

Return false;

}

 

This is done, but the written file is garbled. Therefore, no character conversion is performed. We need to convert pbuffer. How can we use widechartomultibyte?

First of all, my method is stupid. I use it like this:

Char * pchbuffer = new char [dwlen + 1];

Widechartomultibyte (cp_acp, null, pbuffer,-1, pchbuffer, dwlen + 1, null, false );

Writefile (hfile, pbuffer, dwlen + 1, & dwsize, null );

Delete [] pchbuffer;

 

Note that dwlen + 1 is used in writefile. The result is a garbled code at the end of the file, which is exactly one more garbled code. Therefore, in writefile, nnumberofbytestowrite is the number of written strings and does not include '/0.

 

This method is stupid because our function can be reduced to two parameters. In this case, dwlen is the number of strings to be converted, and the string to be converted includes '/0.

DWORD dwlen = widechartomultibyte (cp_acp, null, pbuffer,-1, null, false );

 

So let's take a look at the code modified later.

Bool writeownfile (tchar * pfilename, tchar * pbuffer)

{

Handle hfile = createfile (pfilename,

Generic_write,

File_pai_write,

Null,

Create_always,

File_attribute_normal,

Null

);

 

If (invalid_handle_value! = Hfile)

{

DWORD dwsize = 0;

DWORD dwlen = widechartomultibyte (cp_acp, null, pbuffer,-1, null, false );

Char * pchbuffer = new char [dwlen];

Widechartomultibyte (cp_acp, null, pbuffer,-1, pchbuffer, dwlen, null, false );

Writefile (hfile, pbuffer, dwlen + 1, & dwsize, null );

Delete [] pchbuffer;

Closehandle (hfile );

Return true;

}

Return false;

}

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.