//输入a、b、c三个数,得到最大数。新建一个文档,将结果写入文档。
//使用API函数:CreateFile、SetFilePointer、WriteFile、ReadFile
----------------------------------------dd.h-----------------------------------
char bufferstring[100];//全局变量.文档中的内容.
---------------------------------------dd.cpp-----------------------------------
# include "dd.h"
# include <windows.h>
# include <iostream>
using namespace std;
void inputint()
{
int a,b,c,Max;
char str[10];
for(int i=0; i<100;i++)
bufferstring=' ';//初始化,清空bufferstring的内容
::strcpy(bufferstring,"a( )、b( )、c( )中最大的数是:");
cout<<"请输入三个数值(整数):"<<endl;
cout<<"a:";
cin>>a;
itoa(a,str,10);//整型转换成字符串
bufferstring[3]=str[0];
cout<<"b:";
cin>>b;
itoa(b,str,10);
bufferstring[11]=str[0];
cout<<"c:";
cin>>c;
itoa(c,str,10);
bufferstring[19]=str[0];
Max=(a=(a>b?a:b))>c?a:c;
itoa(Max,str,10);
bufferstring[36]=str[0];
}
void main()
{
inputint();
HANDLE handle;//定义句柄
DWORD Num;
//在指定路径下创建新文档(注意:路径中用'\\'代替'\')
handle= ::CreateFile("C:\\sponge.txt",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
if(INVALID_HANDLE_VALUE!= handle )
{
::SetFilePointer(handle,0,0,FILE_BEGIN);//建立文件的句柄
char Buffer[100];//缓冲区
::strcpy(Buffer,bufferstring);
::WriteFile(handle,Buffer,sizeof(Buffer),&Num,NULL);//将buffer内容写到文档中(写文档)
ZeroMemory(Buffer,sizeof(Buffer));//清空buffer缓冲区
::SetFilePointer(handle,0,0,FILE_BEGIN);
::ReadFile(handle,Buffer,sizeof(Buffer),&Num,NULL);//将文档中的内容写到缓冲区(读文档)
cout<<Buffer<<endl;
::CloseHandle(handle);//释放句柄
}
}