我是靠谱客的博主 欢喜玉米,最近开发中收集的这篇文章主要介绍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

最后

以上就是欢喜玉米为你收集整理的C++ self-learning notes(4)的全部内容,希望文章能够帮你解决C++ self-learning notes(4)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(46)

评论列表共有 0 条评论

立即
投稿
返回
顶部