zoukankan      html  css  js  c++  java
  • VC++ chap12 file

    file operation

    _______C语言对文件操作的支持

    fopen accepts paths that are valid on the file system at the point of execution;

    ____write

    FILE *pFile = fopen("1,txt","w");    //firstly, file should be opened
    fwrite("a carrot",1,strlen("a carrot"),pFile);
    fseek(pFile,10,SEEK_END);       //set the next write position
    fwrite("welcome angle",1,strlen("welcome angle"),pFile);
    fflush(pFile);              //write from buffer to file, because C apply file buffer system, data will first be stored in buffer until the buffer is full,if we want to write data into file immediately fflush is called. or fclose function to close the file.

    ____read 

    FILE *pFile = fopen("1.txt","r");
    char *pBuf;
    fseek(pFile,0,SEEK_END);    //move to end of file   
    int len = ftell(pFile);     //get the len of file
    pBuf = new char[len + 1];   //need '' for ending
    fread(pBuf,1,len,pFile);
    pBuf[len] = 0;
    fclose(pFile);
    MessageBox(pBuf);

    _

    AT: 1),读取文件数据时,如果是字符数据,在定义用来保存该数据的字符数组时,需多分配一个字节,用来存放表示字符串结尾的字符: ‘’. 或者将数组先清零  memset(ch, 0,100);

    2),读取文件内容时,应正确地设置文件指针的位置。

     fseek(pFile,0,SEEK_SET);

     rewind(pFile); 将文件指针重新放置到文件开始处。

    _________C++读文件 

    添加头文件 #include <fstream.h>

           ifstream ifs("4.txt");

           char ch[100];

           memset(ch,0,100);

           ifs.read(ch,100);

           ifs.close();

           MessageBox(ch);

    C++写文件

           ofstream  ofs("2.txt");

           ofs.write("sunxin",strlen("sunxin"));

           ofs.close();

    3,W32 API对文件的操作

    读文件

           HANDLE hFile;

           hFile=CreateFile("5.txt",GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);

           char ch[100];

           DWORD dwReads;

           ReadFile(hFile,ch,100,&dwReads,NULL);

           ch[dwReads]=0;

           CloseHandle(hFile);

           MessageBox(ch);

    写文件

           HANDLE hFile;

           hFile = CreateFile("5.txt",GENERIC_WRITE,0,NULL,CREATE_NEW,

                  FILE_ATTRIBUTE_NORMAL,NULL);

           DWORD dwWrites;

           WriteFile(hFile,"sunxinweb",strlen("sunxinweb"),&dwWrites,NULL);

           CloseHandle(hFile);

    _________ MFC 对文件的读取和写入

    读文件

           CFile file("6.txt",CFile::modeRead);

           char *pBuf;

           DWORD dwFileLen;

           dwFileLen = file.GetLength();

           pBuf=new char[dwFileLen+1];

           pBuf[dwFileLen]=0;

           file.Read(pBuf,dwFileLen);

           file.Close();

           MessageBox(pBuf);

    写文件

           CFile file("6.txt",CFile::modeCreate|CFile::modeWrite);

           file.Write("sunxin",strlen("sunxin"));

           file.Close();

    _______打开文件对话框,另存为对话框   CFileDialog

           CFileDialog    fileDlg(TRUE);

           fileDlg.m_ofn.lpstrTitle="my file open dialog";

           fileDlg.m_ofn.lpstrFilter="Text Files(*.txt)*.txtAll Files(*.*)*.*";

           fileDlg.m_ofn.lpstrDefExt="txt";

           if(IDOK==fileDlg.DoModal())

           {

                  CFile file(fileDlg.GetFileName(),CFile::modeRead);

                  char *pBuf;

                  DWORD dwFileLen;

                  dwFileLen = file.GetLength();

                  pBuf=new char[dwFileLen+1];

                  pBuf[dwFileLen]=0;

                  file.Read(pBuf,dwFileLen);

                  file.Close();

                  MessageBox(pBuf); 

           }

    Write:

           CFileDialog fileDlg(FALSE);              //build save as dialog

           fileDlg.m_ofn.lpstrTitle="my file save dialog";

           fileDlg.m_ofn.lpstrFilter="Text Files(*.txt)*.txtAll Files(*.*)*.*";

           fileDlg.m_ofn.lpstrDefExt="txt";

           if(IDOK==fileDlg.DoModal())

           {

                  CFile file(fileDlg.GetFileName(),CFile::modeCreate|CFile::modeWrite);

                  file.Write("hello rabbit",strlen("hello rabbit"));

                  file.Close();

           }

    Note: CFileDialog 提供两个函数: GetFileName,GetFilePath

    12.6 win.ini文件的访问

    ___________二进制文件和文本文件

    C中默认按照文本方式打开文件的。

    文件:计算机内存中以二进制表示的数据在外部存储介质上的另一种存放形式。

    分为文本文件和二进制文件。

           当按照文本方式向文件中写入数据时,一旦遇到“换行”(ascll码为10)会转换为“回车-换行”(ascll码分别为13,10),读的时候,则反过来。

    所以写入和读取时格式需统一。

    文本文件存放的每一个字节都可以转换为一个可读的字符。如打开一个文本格式文件,文件中存储的每一个字节的数据都要作为ascll码转换为相应的字符,如果它的某一个字节的数据转换为字符后是不可读的,就会显示乱码。

    itoa(98341, ch,10); //将整数转化为字符。

    
    
  • 相关阅读:
    轻奢侈品_百度百科
    什么是轻奢风?_百度知道
    Nitrous.IO融资665万美元 帮助开发者省去配置调试等工作-CSDN.NET
    Intro to Computer Science Class Online (CS101)
    (92) Web Crawling: How can I build a web crawler from scratch?
    zombie-phantom
    How to choose between zombie.js and PhantomJS for automated web testing? [closed]
    Zombie.js Insanely fast, headless full-stack testing using Node.js
    (92) Is there a better crawler than Scrapy?
    tpopela/vips_java
  • 原文地址:https://www.cnblogs.com/aprilapril/p/3301319.html
Copyright © 2011-2022 走看看