zoukankan      html  css  js  c++  java
  • Windows编程中各种操作文件的方法

    windows编程中文件操作有以下几种常见方法:
    1.C语言中文件操作。
    2.C++语言中的文件操作。
    3.Win32 API函数文件操作。
    4.MFC CFile类文件操作。
    5.MFC CFileDialog类的文件操作。
    6.注册表文件操作。

    下面我来详细说明一下各种文件操作方法:
    1. C语言中文件操作.需要包含的头文件STDIO.H
      

    1. 写入文件:   
    2. FILE *pfile=fopen("C.txt","w");//以写的方式打开C.txt文件。   
    3. fwrite("Welcome to VCFans!",1,strlen("Welcome to VCFans!"),pfile);//将数据写入文件。   
    4. fflush(pfile);//刷新缓冲区。将缓冲区数据写入文件   
    5. fclose(pfile);//关闭文件   
    6.   读取文件:   
    7. FILE *pfile=fopen("C.txt","r");//以读的方式打开C.txt文件。   
    8. char FileContent[100];   
    9. memset(FileContent,0,100);//初始化FileContent   
    10. fread(FileContent,1,100,pfile);//将刚才C.txt文件中的内容读入到FileContent   
    11. MessageBox(FileContent);//输出结果   
    12. fclose(pfile);//关闭文件   

    #include <stdio.h>
    #include <afx.h>

    void main()
    {
     LPCTSTR lp = "hello";
     short s = 70;
     long l = 200;
     double d = 20.5;
     char buf[200];

     sprintf(buf, "%s %d %d %f\n", lp, s, l, d);

     FILE *fp;
     fp = fopen("c:\\ais1.txt", "wt");
     fprintf(fp, "%s", buf);
     fclose(fp);
    }

    2.C++语言中的文件操作。需要包含的头文件fstream.h
        

    C++代码
    1. 写入文件:   
    2.     ofstream ofs("C++.txt");//建立ofstream对像。   
    3. ofs.write("Welcome to VCFans!",strlen("Welcome to VCFans!"));//将数据写入文件   
    4. ofs.close();//关闭ofstream对象。   
    5.   读取文件:   
    6. ifstream ifs("C++.txt");   
    7. char FileContent[100];   
    8. memset(FileContent,0,100);//初始化FileContent   
    9. ifs.read(FileContent,100);//读取数据   
    10. ifs.close();//关闭ifstream对像   
    11. MessageBox(FileContent);//输出结果   

    3.Win32 API函数文件操作。需要包含的头文件winbase.h,需要类库:kernel32.lib
      

    C++代码
    1. 写入文件:   
    2.   HANDLE hFile;//定义一个句柄。   
    3. hFile=CreateFile("API.txt",   
    4.    GENERIC_WRITE,   
    5.    FILE_SHARE_WRITE,   
    6.    NULL,   
    7.    CREATE_NEW,   
    8.    FILE_ATTRIBUTE_NORMAL,   
    9.    NULL);//使用CreatFile这个API函数打开文件   
    10. DWORD Written;   
    11. WriteFile(hFile,"Welcome to VCFans!",strlen("Welcome to VCFans!"),&Written,NULL);//写入文件   
    12. CloseHandle(hFile);//关闭句柄   
    13.   读取文件:   
    14.   HANDLE hFile;//定义一个句柄。   
    15. hFile=CreateFile("API.txt",   
    16.    GENERIC_READ,   
    17.    FILE_SHARE_READ,   
    18.    NULL,   
    19.    OPEN_EXISTING,   
    20.    FILE_ATTRIBUTE_NORMAL,   
    21.    NULL);//使用CreatFile这个API函数打开文件   
    22. DWORD dwDataLen;   
    23. char FileContent[100];   
    24. ReadFile(hFile,FileContent,100,&dwDataLen,NULL);//读取数据   
    25. FileContent[dwDataLen]=0;//将数组未尾设零。   
    26. CloseHandle(hFile);//关闭句柄   
    27. MessageBox(FileContent);//输出结果   

    4.MFC CFile类文件操作。需要包含的头文件afx.h

    C++代码
    1. 写入文件:   
    2.    CFile file("CFile.txt",CFile::modeCreate| CFile::modeWrite);//构造CFile对象   
    3.    file.Write("Welcome to VCFans !",strlen("Welcome to VCFans !"));//写入数据到文件   
    4.    file.Close();//关闭CFile对象。   
    5. 读取文件:   
    6.    CFile file("CFile.txt",CFile::modeRead);//构造CFile对象   
    7.   char FileContent[100];   
    8.    memset(FileContent,0,100);//初始化FileContent   
    9.    file.Read(FileContent,100);//读入数据   
    10.    file.Close();//关闭文件对象   
    11.    MessageBox(FileContent);//输出数据   

    5.MFC CFileDialog类的文件操作。需要包含的头文件Afxdlgs.h

    C++代码
    1. 写入文件:   
    2.    CFileDialog fileDlg(FALSE,"txt","CFileDialog.txt");//建立CFileDialog对象   
    3. if(IDOK==fileDlg.DoModal())   
    4. {   
    5.    CFile file(fileDlg.GetFileName(),CFile::modeCreate| CFile::modeWrite);//构造CFile对象   
    6.    file.Write("Welcome to VCFans !",strlen("Welcome to VCFans !"));//写入数据到文件   
    7.    file.Close();   
    8. };   
    9. 读取文件:   
    10.    CFileDialog fileDlg(TRUE,"txt","CFileDialog.txt");//建立CFileDialog对象   
    11. if(IDOK==fileDlg.DoModal())   
    12. {   
    13.    CFile file(fileDlg.GetFileName(),CFile::modeRead);//构造CFile对象   
    14.   char FileContent[100];   
    15.    memset(FileContent,0,100);//初始化FileContent   
    16.    file.Read(FileContent,100);//读入数据   
    17.    file.Close();//关闭文件对象   
    18.    MessageBox(FileContent);   
    19. };   


    6.注册表文件操作。
      

    C++代码
    1.   写入注册表:   
    2.   HKEY hKey;   
    3.   DWORD dwSex=1;   
    4.    RegCreateKey(HKEY_LOCAL_MACHINE,"Software\\vcfans\\reg",&hKey);//打开注册表键   
    5.    RegSetValueEx(hKey,"sex",0,REG_DWORD,(CONST BYTE*)&dwSex,4);//写入注册表数据   
    6.    RegCloseKey(hKey);//关闭注册表键   
    7. 读注册表:   
    8.   HKEY hKey;   
    9.    RegOpenKey(HKEY_LOCAL_MACHINE,"Software\\vcfans\\reg",&hKey);//打开注册表键   
    10.   DWORD dwType;   
    11.   DWORD dwValue;   
    12.   DWORD dwSex;   
    13.    RegQueryValueEx(hKey,"sex",0,&dwType,(LPBYTE)&dwSex,&dwValue);//查询注册表数据   
    14.    RegCloseKey(hKey);//关闭注册表键   
    15.    CString str;   
    16.    str.Format("sex=%d",dwSex);   
    17.    MessageBox(str);   
  • 相关阅读:
    ThinkPHP中自定义常量
    【转】在Asp.net中弹出对话框,然后跳转到其他页面问题
    【转】SVN版本控制器的安装和配置
    【原】用上传控件进行文件上传时,页面程序代码都不执行,显示“页面信息无法显示”
    【转】net Web Service 方法重载
    【转】SQL里的EXISTS与in、not exists与not in
    【转】利用wsdl.exe生成webservice代理类
    【转】获取图片大小
    【转】用了AJAX后,不能用javascript弹出对话框
    【转】net Web Service 方法重载
  • 原文地址:https://www.cnblogs.com/chuncn/p/1380684.html
Copyright © 2011-2022 走看看