发新话题
打印

VC——用API函数实现文件操作

VC——用API函数实现文件操作

//输入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);//释放句柄
}
}

TOP

整理整理格式吧看得我眼花………………………………
S 是开始
E 是结束
SE 是我的FIRST,也是我的LAST
SE 是我最爱的女孩
            
我的:http://dt-skyse.blog.sohu.com/

TOP

谢谢指教,整理后……

也许有一天可以用得到:)
最重要的是主函数,关键的语句都在那。子函数是控制输入的,意义不大。



//输入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_  A LWAYS,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);//释放句柄
        }
}

TOP

不错
没有你的日子里,我憔悴如一个陌生的过客,存在和不存在都那么简单

TOP

发新话题