zoukankan      html  css  js  c++  java
  • 文件操作

    1、C语言的文件操作:(多)
    使用FILE* pFILE=fopen("test.txt","w")来打开写入文件,使用的是缓冲模式,即只有等程序关闭了,文件才被写入。所以可以使用fclose()来表示磁盘缓冲区已经充满了。
    当需要多次使用此文件指针的时候,可以用fflush()来刷新缓冲区。

    连续写入,文件指针自动移动:
    fwrite(testStr,1,strlen(testStr),pFILE);
    fwrite(testStr,1,strlen(testStr),pFILE);
    可以用fseek()来移动文件指针的位置;
    用ftell()来获得文件的大小;
    rewind()来把指针指到最开头==fseek(pFILE,0,SEEK_SET);
    FILE* pFILE=fopen("jude.txt","r");
    //char buf[100]={0};//保证没有乱码
    //memset(pFILE,0,100);等于上面那句话
    //fread(buf,1,100,pFILE);
    char* pBuf;//动态数组指针
    fseek(pFILE,0,SEEK_END);
    int len=ftell(pFILE);//获得文件大小
    pBuf=new char[len+1];
    //rewind(pFILE);
    fseek(pFILE,0,SEEK_SET);
    fread(pBuf,1,len,pFILE);
    pBuf[len]=0;
    //delete(pBuf);
    MessageBox(pBuf);
    fclose(pFILE);
    delete(pBuf);
    2、C++文件操作:(少)
    #include <fstream>
    using namespace std;
    写:
    ofstream ofs("yd.txt");
    ofs.write("love",strlen("love"));
    ofs.close();
    读:
    std::ifstream ifs("yd.txt");
    char ch[100]={0};
    ifs.read(ch,100);
    ifs.close();
    MessageBox(ch);

    3、win32文件操作:
    CreateFIle,WriteFile,ReadFile,CloseFile

    4、MFC:
    类:CFile
    写:
    CFile file("zz.txt",CFile::modeCreate|CFile::modeReadWrite);
    file.Write("www.baidu.com",strlen("www.baidu.com"));
    file.Close();
    读:
    CFile file("zz.txt",CFile::modeRead);
    char *buf;
    DWORD lenth=file.GetLength();
    buf=new char[lenth+1];
    buf[lenth]=0;
    file.Read(buf,lenth);
    MessageBox(buf);
    file.Close();
    delete buf;
    文件对话框:
    读:
    CFileDialog filedialog(TRUE);
    if(IDOK==filedialog.DoModal())
    {
    CFile file(filedialog.GetPathName(),CFile::modeRead);
    char *buf;
    int lenth=file.GetLength();
    buf=new char[lenth+1];
    buf[lenth]=0;
    file.Read(buf,lenth);
    MessageBox(buf);
    }

  • 相关阅读:
    Day26
    Day25
    day24
    day22
    DAY21
    Day20
    Day19
    Day18
    Day17
    RabbitMQ
  • 原文地址:https://www.cnblogs.com/judes/p/5910461.html
Copyright © 2011-2022 走看看