开发者

What stream do files use?

开发者 https://www.devze.com 2023-04-13 07:01 出处:网络
When reading data 开发者_如何转开发through cin, the stdin stream is used. How about files? Do they use a particular stream object?There is a complete hierarchy of stream classes, you can see the doccu

When reading data 开发者_如何转开发through cin, the stdin stream is used. How about files? Do they use a particular stream object?


There is a complete hierarchy of stream classes, you can see the doccumentation here.

Most notable amongst them are ifstream and ofstream for reading and writing to files respectively.


They use what you choose :)

#include <fstream>
#include <iostream>


std::ifstream input("test.txt");

int i;
if (input >> i)
    std::cout << i << std::endl;


std::string s;
if (std::getline(input, s))
    std::cout << s << std::endl;

You could replace the name input by a name of your own chosing: it's just an identifier.


You can use std::ifstream and std::ofstream for reading and writing files. You have to include fstream.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号