zoukankan      html  css  js  c++  java
  • 7、C++ Primer 4th 笔记,标准IO库(2)

    1、文件的输入输出

    由于历史原因,IO 标准库使用 C 风格字符串而不是 C++strings 类型的字符串作为文件名。

    如果要把fstream对象与另一个不同的文件关联,则必须先关闭(close)现在的文件,然后打开(open)另一个文件。

    2、读文件中文件流的状态

    如果程序员需要用文件流读写多个文件,必须在读另一个文件之前调用clear清除该流的状态。

    示例程序

    // for each file in the vector
    while (it != files.end()) {
    	ifstream input(it->c_str()); // open the file;
    	// if the file is ok, read and "process" the input
    	if (!input)
    		break; // error: bail out!
    	while(input >> s) // do the work on this file
    		process(s);
    	++it; // increment iterator to get
    	next file
    }
    //check out the difference between these two programs.
    ifstream input;
    vector<string>::const_iterator it = files.begin();
    // for each file in the vector
    while (it != files.end()) {
    	input.open(it->c_str()); // open the file
    	// if the file is ok, read and "process" the input
    	if (!input)
    		break; // error: bail out!
    	while(input >> s) // do the work on this file
    		process(s);
    	input.close(); // close file when we're done with it
    	input.clear(); // reset state to ok
    	++it; // increment iterator to get next file
    }
    

    3、文件模式

    每个fstream类都定义了一组表示不同模式的值,用于指定流打开的不同模式,与条件状态标志一样,文件模式也是整形常量,在打开指定文件时,可用位操作符,设置多个模式。

    表示 打开模式

    in

    打开文件做读操作

    out

    打开文件做写操作

    app

    在每次写之前找到文件尾

    ate

    打开文件后立即将文件定位在文件尾

    trunc

    打开文件时清空已存在的文件流

    binary

    以二进制模式进行IO操作

        outtrunc app 模式只能用于指定与 ofstream fstream 对象关联

    的文件;in 模式只能用于指定与 ifstream fstream 对象关联的文件。所有

    的文件都可以用 ate binary 模式打开。ate 模式只在打开时有效:文件打

    开后将定位在文件尾。以 binary 模式打开的流则将文件以字节序列的形式处

    理,而不解释流中的字符。

    out模式打开的文件会被清空。当文件同时以in,out打开时不清空。

    模式是文件的属性不是流的属性。

    表示 打开模式的有效组合

    out

    打开文件做写操作,删除文件中已有的数据

    out | app

    打开文件做写操作,在文件尾写入

    out | trunc

    out 模式相同

    in

    打开文件做读操作

    in | out

    打开文件做读、写操作,并定位于文件开头处

    in | out | trunc

    打开文件做读、写操作,删除文件中已有的数据

    4、字符串流

    表示 字符串流的操作

    stringstream strm;

    创建自由的 stringstream 对象

    stringstream strm(s);

    创建存储s的副本的stringstream对象,其中sstring类型的对象

    strm.str()

    返回 strm 中存储的 string 类型对象

    strm.str(s)

    string 类型的 s 复制给 strm,返回 void

    字符串流的经典操作示例

    string line, word; // will hold a line and word from input,respectively
    while (getline(cin, line)) 
    { // read a line from theinput into line
    		// do per-line processing
    	istringstream stream(line); // bind to stream to the line we read
    		while (stream >> word)
    		{ // read a word from line
    			// do per-word processing
    		}
    }
    

    1stringstream提供的转换和格式化

        stringstream 对象的一个常见用法是,需要在多种数据类型之间实现自动格式化时使用该类类型。sstream 输入和输出操作可自动地把算术类型转化为相应的

    string表示形式,反过来也可以。

    典型示例

    int val1 = 512, val2 = 1024;
    ostringstream format_message;
    // ok: converts values to a string representation
    format_message << "val1: " << val1 << "\n"
                   << "val2: " << val2 << "\n";
    //the following is how to read
    //str member obtains the string associated with a stringstream
    istringstream input_istring(format_message.str());
    string dump; // place to dump the labels from the formatted message
    // extracts the stored ascii values, converting back to arithmetic types
    input_istring >> dump >> val1 >> dump >> val2;
    cout << val1 << " " << val2 << endl; // prints 512 1024
    

    为了读取input_string,必须把该string对象分解成若干部分,必须读取和忽略处于所需数据周围的标号。因为输入操作符读取的是有类型的值,因此读入的对象类型必须和由stringstream 读入的值的类型一致。一般情况下,使用输入操作符读string时,空白符将会忽略。

    参考:

    [1] http://blog.163.com/zhoumhan_0351/blog/static/39954227201002945157577/

    [2] http://blog.163.com/zhoumhan_0351/blog/static/39954227201003005237697/

    [3] http://blog.163.com/zhoumhan_0351/blog/static/399542272010030103055465/

    [4] http://blog.163.com/zhoumhan_0351/blog/static/39954227201002192139554/

    [5] http://blog.163.com/zhoumhan_0351/blog/static/3995422720103171303182/

    [6] 深入输入输出流

    http://blog.163.com/zhoumhan_0351/blog/static/399542272010495432659/

    http://blog.163.com/zhoumhan_0351/blog/static/39954227201032392545410/

  • 相关阅读:
    正则表达式匹配可以更快更简单 (but is slow in Java, Perl, PHP, Python, Ruby, ...)
    ++i? i++? i+=1? i=i+1? 何必纠结?
    数独题的生成与解决方法
    VIM常用设置
    我的“MIT Challenge”
    NDK开发之javaVM
    十二月寒冬
    Linux epoll 笔记(高并发事件处理机制)
    Linux之我见
    半夜惊醒
  • 原文地址:https://www.cnblogs.com/mydomain/p/1988313.html
Copyright © 2011-2022 走看看