C++ self-learning notes(4)
2017-06-21 00:00
369 查看
摘要: file operations
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.
![](https://static.oschina.net/uploads/space/2017/0621/203357_FgNp_2968040.png)
![](https://static.oschina.net/uploads/space/2017/0621/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.
![](https://static.oschina.net/uploads/space/2017/0621/211738_pzOb_2968040.png)
RESULT:
![](https://static.oschina.net/uploads/space/2017/0621/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
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.
![](https://static.oschina.net/uploads/space/2017/0621/203357_FgNp_2968040.png)
![](https://static.oschina.net/uploads/space/2017/0621/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.
![](https://static.oschina.net/uploads/space/2017/0621/211738_pzOb_2968040.png)
RESULT:
![](https://static.oschina.net/uploads/space/2017/0621/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
相关文章推荐
- C++ self-learning notes(5)
- C++ self-learning notes(9)
- C++ self-learning notes(2)
- C++ self-learning notes(7)
- C++ self-learning notes(3)
- C++ self-learning notes (1)
- C++ self-learning notes(6)
- C++ self-learning notes(8)
- LeetCode 238. Product of Array Except Self 题解(C++)
- leetcode_c++:Divide and Conquer:Count of Smaller Numbers After Self(315)
- Self Summary: C++函数返回引用和指针的问题,局部对象与new对象的问题
- Self Summary: C++函数返回引用和指针的问题,局部对象与new对象的问题
- C++ self Q/A
- C++中的this和Python的self对比
- 场景中self其实就相当于Java,C++中的this对象
- leetcode 238 Product of Array Except Self C++
- Self Print in C++
- Self Summary: C++函数返回引用和指针的问题,局部对象与new对象的问题
- Can a C++ class have an object of self type?
- C++的this,Objective-C的self