Linux、标准C、C++、QT中对文件I/的操作

来源:互联网 发布:视频格式转换软件下载 编辑:程序博客网 时间:2024/05/23 10:41

一、Linux中,文件I/O有5个函数:open、read、write、lseek、close(不带缓冲,即每个read和write都调用内核中的一个系统调用)  #include<unistd.h>

1)int open( const char * pathname,int flags);
   int open( const char * pathname,int flags, mode_t mode/*新建文件时,新建文件的权限*/);

   说明:flags 标识,主要有以下宏定义:

文件描述符的权限标识:O_RDONLY、O_WRONLY、O_RDWR,必选其一。
附加标识:O_APPEND 用追加的方式打开(从文件尾开始写,读文件一般不用)
创建标识:O_CREAT 存在就打开,不存在就新建;
 O_TRUNC 文件存在时清空所有数据(谨慎);
         O_EXCL  不存在就新建,存在不打开,而是返回-1,代表出错;
注:第三个参数是文件在硬盘上(ls)的权限(某些权限可能被系统屏蔽)。

        返回值:成功返回文件描述符,失败返回 -1。

-------------------------------------------------------------------

2)int read  (int fd, void* buf, size_t size/*为buf的大小,可能读不满*/)
   int write (int fd, void* buf, size_t length/*想写入的字节数*/)
返回值:

 正数:真实读到 / 写入的字节数。

   0 :读到文件尾 / 什么都没写。read()的0返回值,通常用于循环读文件的退出。

  -1 :出现错误。

-------------------------------------------------------------------

3)int lseek(int fd, int offset, int whence)

返回值:返回当前位置到文件头的偏移量,失败返回 -1

===================================================================

二、标准C中,主要函数:fopen、fread、fwrite、fseek、ftell、rewind、fflush、fclose

1)FILE *fopen( const char *fname, const char *mode );

mode :mode is used to determine how the file will be treated。

"r"Open a text file for reading
"w" Create a text file for writing
"a" Append to a text file
"rb" Open a binaryfile for reading
"wb" Create a binaryfile for writing
"ab" Append to a binary file
"r+" Open a text file for read/write
"w+" Create a text file for read/write
"a+" Open a text file for read/write
"rb+" Open a binary file for read/write
"wb+" Create a binary file for read/write
"ab+" Open a binary file for read/write

2)int fread( void *buffer, size_t size, size_t num, FILE *stream );

3)int fwrite( const void *buffer, size_t size, size_t count, FILE *stream );

4)int fseek( FILE *stream, long offset, int origin );

5)long ftell( FILE *stream );

6)void rewind( FILE *stream );

7)int fflush( FILE *stream );

8)int fclose( FILE *stream );

===================================================================

如果考虑通用性,用标准C,标C在用户层定义了I/O缓冲,累计到一定量进入内核读/写一次。

UC 函数都没有定义缓冲区,但可以由程序员自定义缓冲区提升效率。

===================================================================

三、C++中,主要函数:open、read、write、flush、close    #include <fstream>

1)void open( const char *filename );
   void open( const char *filename, openmode mode = default_mode );

/*open函数用于文件流。它打开“filename”,并且将其和当前“stream”进行关联*/

-------------------------------------------------------------------

可选项 I/O stream mode flags 允许以不同的方式访问文件:

Mode               Meaning
ios::app       append output
ios::ate       seek to EOF when opened
ios::binary      open the file in binary mode
ios::in                open the file for reading
ios::out       open the file for writing
ios::trunc       overwrite the existing file

------------------------------------------------------------------- 

如果打开失败,产生的流(resulting stream)在使用布尔表达式时,将被评定为(evaluate)为假:

ifstreaminputStream;
inputStream.open("file.txt");
if( !inputStream ) {
   cerr << "Error opening input stream" << endl;
   return;
}

2)istream& read( char* buffer, streamsize num );

/* 遇到 EOF 停止读取 */

3)ostream& write( const char* buffer, streamsize num );

/* 从流中得到num个字节写入到输出流中 */

4)void close();

5)ostream& flush();            

===================================================================

四、QT文件读写(和C++基本相同)

1、构造QFile对象:

QFile(const QString & name);
2、打开文件(设置访问文件的 mode):

bool QFile::open(OpenMode mode) [virtual]

enum QIODevice::OpenModeFlag>>>
QIODevice::ReadOnly
QIODevice::WriteOnly
QIODevice::ReadWrite
QIODevice::Append
QIODevice::Truncate
QIODevice::Text
QIODevice::Unbuffered(Any buffer in the device is bypassed/*绕过*/)

Note: 在只读和读写模式中,如果相应的文件不存在,函数将会在打开它之前试图创建一个新文件。
------------------------------------------------------------------

3、文本流 QTextStream提供一个方便读写文本的接口。

1.构造文本流:QTextStream(QIODevice * device)

2.读取文本流:QString QTextStream::read(qint64 maxlen)/* 从流中至多读取maxlen个字节,并且作为QString返回*/

3.也可以通过运算符重载的方式,进行读写。

QFile data("output.txt");
if (data.open(QFile::WriteOnly | QFile::Truncate)) {
    QTextStream out(&data);
    out << "Result: " << qSetFieldWidth(10) << left << 3.14 << 2.7;
    // writes "Result: 3.14      2.7       "
}
------------------------------------------------------------------

4.简单实例

逐字节读取 QTextStream 流中字符[A...Z] ,通过 QMediaPlayer 播放,实现简易“电子钢琴”。

基本代码(更多细节、需要完善):

QTextStreamstream = newQTextStream( file );

QMediaPlayer player = newQMediaPlayer ;

-----------------------------------------------------------------

QStringstr = stream->read(1);

if(str){

   player->addMedia(QUrl("://" + str + ".mp3")/*兼容网络媒体 */);

   /* Url是统一资源定位符,也称网页地址,是Internet标准的资源的地址 */

   player->play();

   /* 或者通过将所有的.mp3文件添加到QPLayList对象中,设置setCurrentIndex索引的方式进行播放 */

}

-------------------------------------------------------------------

0 0