zoukankan      html  css  js  c++  java
  • VC中常用文件操作(三)

    一、使用MFC的CFile类。

    二、使用MFC的CFile派生类CStdioFile

      CFile的派生类CStdioFile提供了对文件进行流式的操作功能。其中函数void CStdioFile::WriteString( LPCTSTR lpsz )写入一个字符串,需要给字符串lpsz的末尾加上换行标志”\r\n”;函数bool CStdioFile::ReadString(CString &rString )从文件中读取一行,如果文件未读完返回true,否则返回false。

      比如:写入文件的例子

      

    View Code
     1 //创建文件 2 
    3
    4 CStdioFile file;
    5 file.Open("ts.txt",CFile::modeCreate|CFile::modeWrite);
    6
    7 //写入文件
    8
    9 CString str;
    10 str.Format("%s\r\n","hello!I am talkingmute!");
    11 file.Seek(0,CFile::end);
    12 file.WriteString( str );
    13
    14 //关闭文件
    15
    16 file.Close();
    17
    18 比如:读文件的例子
    19
    20 CString strText = “”;
    21 CString szLine = “”;
    22
    23 //打开文件
    24 CStdioFile file;
    25 file.Open("ts.txt",CFile::modeRead);
    26
    27 //逐行读取字符串
    28 while( file.ReadString( szLine ) )
    29 {
    30 strText += szLine;
    31 }
    32
    33 MessageBox(strText);
    34
    35 //关闭文件
    36
    37 file.Close();

    Life is like a box of chocolate, you never know what you are going to get.
  • 相关阅读:
    mysql 主从服务器配置
    Linux命令
    Kali
    Python进阶
    性能测试工具
    sphinx搜索
    页面静态化
    PHP API接口
    线程的生命周期
    多线程的创建
  • 原文地址:https://www.cnblogs.com/mars9/p/2159395.html
Copyright © 2011-2022 走看看