C++ self-learning notes(4)

This blog is about file operations. Here I only present a few functions and classes. I'll keep updating when I get new information.

Firstly, I'll introduce three objects: 1) ofstream (write, export from RAM to storage ), 2) ifstream (read, import from storage to RAM), 3) fstream (can both write and read);

1. ofstream defines an object (f). When you call the function open() and put file names in the parentneses, f would open or create(if there's no such file) the corresponding. And you can write in the file directly.

203357_FgNp_2968040.png

203549_mbxZ_2968040.png

1) if you want to operate files, you must include the fstream.h head file.

2) when you define an object with ofstream, you can then operate the object with its functions such as : open(), close(), clear()

3) flags: bad() if there is error in the process, returns true.

              fail() same as bad() plus return true when there is format mistake.

              eof() if read to the end of the file, returns true.

           good() if any of the flag above returns true, it returns false.

Useful links:

http://blog.csdn.net/kingstar158/article/details/6859379

http://blog.csdn.net/augusdi/article/details/8865378

http://blog.chinaunix.net/uid-21375345-id-3049692.html

 

2. ifstream defines an object that can be used to read contents from interested files to RAM.

211738_pzOb_2968040.png

RESULT:

213500_EyYM_2968040.png

By far, I just got these. This blog will be updated in the future when I encounter more contents about file operation.

 

update 1:

Read and Write.  Function calling formats are shown below

.read(unsigned char *buf,int num);  
.write(const unsigned char *buf,int num); 

The read function will take ’num‘ number of charactors in the file to the variable pointed by *buf.

The write function will put 'num' number of charactors in the variable pointed by *buf into the file.

 

 

Useful links:

http://www.cplusplus.com/reference/fstream/ifstream/

http://www.prglab.com/cms/pages/c-tutorial/file-inputoutput.php

http://blog.csdn.net/luo809976897/article/details/51442070

转载于:https://my.oschina.net/u/2968040/blog/994863

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值