• 文件创建与读写练习


    文件创建与读写练习

    RFile 可以读写一个文件,在 create 一个文件时,如果已存在同名的文件,则会出错,可以选择用 replace 方法

    RFile 使用简单,注意的是读写的方法有多种,其中涉及到 8 位与16位转换的问题,用完后需要 close(),以下面练习代码

    void writeFile()
    {
        User::LeaveIfError(ifs.Connect());
        RFile file;
        _LIT(KfileName,
    "c:\\mytxtfile\\aaa.txt");
        TBufC
    <20> filename(KfileName);
        
    // RFs ifs; 这个在前面已定义
        if (file.Create(ifs,filename,EFileWrite) != KErrAlreadyExists)
        {
            _LIT8(KFileContent,
    "Test File write");
            TBufC8
    <20> bufc(KFileContent);
            User::LeaveIfError(file.Write(KFileContent));
            User::LeaveIfError(file.Flush());
            file.Close();
        }
        
    // 写完后,读出来 
        /*
        读出来时要转为 16 位保存,否则显示乱码
        
    */
        TBuf8
    <4> aRead;
        TBuf
    <4>  aread16;
        
        file.Open(ifs,filename,EFileRead);
        _LIT(KC,
    "%S\n");
        TInt i
    =0;
        
    do 
        {
            aRead.Zero();
            aread16.Zero();
            file.Read(i,aRead,
    4);
            i
    +=4;
            file.Seek(ESeekStart,i);
            aread16.Copy(aRead);
            console
    ->Printf(KC,&aread16);
        } 
    while(aRead.Length()!=0);
        
        file.Close();
        ifs.Close();
    }
     

    关于 RFile 的说明,请看 SDK的帮助

    Location: f32file.h
    Link against: efsrv.lib

    Class RFile

    RFile

    Support

    Supported from 5.0

    Description

    Creates and opens a file, and performs all operations on a single open file. These include:

    • reading from and writing to the file

    • seeking to a position within the file

    • locking and unlocking within the file

    • setting file attributes

    Before using any of these services, a connection to a file server session must have been made and the file must be open.

    Opening Files:

    • use Open() to open an existing file for reading or writing — an error is returned if it does not already exist. To open an existing file for reading only, use Open() with an access mode of EFileRead and a share mode of EFileShareReadersOnly.

    • use Create() to create and open a new file for writing — an error is returned if it already exists.

    • use Replace() to open a file for writing, replacing any existing file of the same name if one exists or creating a new file if one does not exist,

    • use Temp() to create and open a temporary file with a unique name, for writing and reading.

    When opening a file, you must specify the file server session to use for operations with that file. If you do not close the file explicitly, it is closed when the server session associated with it is closed.

    Reading and Writing:

    There are several variants of both Read() and Write(). The basic Read(TDes8& aDes) and Write(const TDesC8& aDes) are supplemented by variants allowing the descriptor length to be overridden, or the seek position of the first byte to be specified, or asynchronous completion, or any combination.

    Reading transfers data from a file to a descriptor, and writing transfers data from a descriptor to a file. In all cases, the file data is treated as binary and byte descriptors are used (TDes8, TDesC8).

    Derivation

      • RSubSessionBase - Client-side handle to a sub-session
        • RFsBase - Base class that provides a Close() function for file related clean-up
          • RFile - Creates and opens a file, and performs all operations on a single open file

      Members

      Defined in RFile:
      Att(), ChangeMode(), Create(), Flush(), Lock(), Modified(), Open(), Read(), Read(), Read(), Read(), Read(), Read(), Read(), Read(), Rename(), Replace(), Seek(), Set(), SetAtt(), SetModified(), SetSize(), Size(), Temp(), UnLock(), Write(), Write(), Write(), Write(), Write(), Write(), Write(), Write()

      Inherited from RFsBase:
      Close()

      Inherited from RSubSessionBase:
      CloseSubSession(), CreateSubSession(), Send(), SendReceive(), SubSessionHandle(), operator=()



      安平2009@原创
      qi_jianzhou@126.com

    • 相关阅读:
      理解 Go interface 的 5 个关键点
      volcano networkpolicy
      通过 iptables log 日志追踪 Kubernetes 网络
      Network Policy Enforcement
      calico 容器编排之Kubernetes多租户网络隔离
      Kubernetes的原生多租户解决方案
      第四章 计算机基础知识详解
      vue兄弟组件通信
      vue父子组件通信
      Safari浏览器css兼容
    • 原文地址:https://www.cnblogs.com/zziss/p/1651981.html
    Copyright © 2020-2023  润新知