开发者

What stream do files use?

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜