C Language Programming File Operation Method Example (CreateFile and fopen) _c language

Source: Internet
Author: User

Instance:

(1) The first method of CreateFile

Copy Code code as follows:

#include "stdafx.h"
#include <windows.h>
void Main (int argc, char* argv[])
{
HANDLE hdevice = CreateFile ("C://s.txt",
generic_read| Generic_write,
0,
Null
Open_existing,
File_attribute_normal,
NULL);
if (Hdevice = = INVALID_HANDLE_VALUE)
{
printf ("Failed to obtain file with%d error code!/n", GetLastError ());
Return
}
DWORD dwsize = GetFileSize (hdevice,null);
printf ("%d/n", dwsize);

Char chbuffer[10] = "5469";
DWORD dwwritesize = 0;
BOOL BRet = WriteFile (hdevice,chbuffer,4,&dwwritesize,null);
if (BRet)
{
printf ("Write file success/n");
}
FlushFileBuffers (Hdevice); Write buffer data to disk

LONG idistance = 0;
DWORD dwptr = SetFilePointer (Hdevice,idistance,null,file_begin); Adjust file pointer to start of file
DWORD dwreadsize = 0;
BRet = ReadFile (hdevice,chbuffer,10,&dwreadsize,null);
if (BRet)
{
printf ("Chbuffer is%s/n", chbuffer);
}
CloseHandle (Hdevice);
return;
}

(2) The second method of streaming file operation files

Copy Code code as follows:

Char datain[101];
FILE *fp_sys;
Fp_sys = fopen ("FileName to open", "RB"); The second argument is open method, R is read, B represents binary
if (Fp_sys = = NULL) {
AfxMessageBox ("Unable to open the Recharge card file");

Related Article

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.