c++ 文件读写总结(stream 例子使用)

ostringstream : 用于执行C风格字符串的输出操作。

istringstream : 用于执行C风格字符串的输入操作。

stringstream : 同时支持C风格字符串的输入输出操作。

通常,ostringstream 类用来格式化字符串,避免申请大量的缓冲区,替代sprintf。该类能够根据内容自动分配内存,其对内存管理也是相当到位。

#include <string>
#include <sstream>//
#include <iostream>
using namespace std;
//ostringstream 用于执行C风格字符串的输出操作
void ostringstream_test()
{
    //ostringstream 只支持 << 操作符
    std::ostringstream oss;
    oss << "this is test" << 123456;

    //    cout << oss.str() << endl;        

    oss.str("");//清空之前的内容
    oss.clear();//并不能清空内存

                //浮点数转换限制
    double a, b;
    cout << "请输入两个数:";
    cin >> a >> b;
    //    double tmp = 123.1234567890123;

    oss.precision(12);
    oss.setf(std::ios::fixed);//将浮点数的位数限定为小数点之后的位数
                              //    oss << tmp;
    oss << a*b << endl;

    std::cout << oss.str() << "\r\n" << std::endl;
}

//istringstream 用于执行C风格字符串的输入操作
void istringstream_test()
{
    //istringstream 只支持 >> 操作符
    std::string str = "welcome to china 123";
    std::istringstream iss(str);

    //把字符串中以空格隔开的内容提取出来
    std::string out;
    while (iss >> out)
    {
        std::cout << out << std::endl;
    }
    std::cout << "\r\n" << std::endl;
}

//stringstream 同时支持C风格字符串的输入输出操作

void stringstream_test()
{
    //输入
    std::stringstream ss;
    ss << "hello this is kandy " << 123;
    std::cout << ss.str() << "\r\n" << std::endl;

    //输出
    std::string out;
    while (ss >> out)
    {
        std::cout << out.c_str() << std::endl;
    }
    std::cout << "\r\n" << std::endl;
}

int main()
{
    ostringstream_test();

    istringstream_test();

    stringstream_test();

    system("pause");

    return 0;
}

文件头
#include <iostream>#include <fstream>#include <string>#include <vector>#include <sstream>
using namespace std;


从文件中逐词读取

void getFromFile() 
{ 
    ifstream fin("C:\\data.txt"); 
    string s; 
    while (fin >> s)
    {
        cout << "Read from file:" <<  s << endl;
    }    
}

从文件中逐行读取

void getFromFileByLine() 
{ 
    ifstream fin("C:\\data.txt"); 
    string s; 
    while (getline(fin, s))
    {
        cout << "Read from file:" << s << endl;
    } 
}

从文件中逐词读取并写入vector容器中

void fileToVectorByWord() 
{ 
    ifstream infile;
    infile.open("C:\\data.txt");
    if (!infile) 
    {    //加入异常判断
 
        cout << "error : cannot open the file" << endl;
    } 
    vector<string> v;
    string s;
    while (!infile.eof())
    {
        infile >> s;
        v.push_back(s); 
    } 
    vector<string>::const_iterator it = v.begin();  //开始验证
    while (it != v.end()) 
    {
        cout << *it << endl;
        ++it;
    } 
}

从文件中逐行读取并写入vector容器中

void fileToVectorByLine() 
{ 
    ifstream infile;
    infile.open("C:\\data.txt");
    if (!infile) {
 
        cout << "error : cannot open the file" << endl;
    } 
    vector<string> v;
    string s;
    while (! infile.eof()) 
    {
        getline(infile, s);
        v.push_back(s); 
    } 
    vector<string>::const_iterator it = v.begin();
    while (it != v.end()) 
    {
        cout << *it << endl;
        ++it;
    } 
}

istringstream的使用

void testStringstream()
{
    string line;
    string word;
    while (getline(cin, line)) 
    {   //从控制台获取一行字符        
        istringstream stream(line);  //将line绑定成istringstream 
        while (stream >> word)
        {  //逐词输出
            cout << word << endl;  
        }    
    } 
}    

 

posted @ 2020-05-08 10:54  marblemm  阅读(927)  评论(0编辑  收藏  举报