美文网首页
c++快速入门5:文件处理1读写及getline

c++快速入门5:文件处理1读写及getline

作者: python测试开发 | 来源:发表于2021-07-25 11:11 被阅读0次

本章演示了如何在文本文件中存储和检索数据,并说明了如何避免C++程序中的错误。

写文件

写文件的格式可以被指定为人类可读的纯文本格式或机器可读的二进制格式。

标准的C++ <fstream>库提供了处理文件的功能,可以通过在程序开始时添加#include <fstream>指令来实现。

对于每个要打开的文件,首先必须创建文件流对象。这将是"ofstream"(输出文件流)对象,用于向文件写入数据,或"ifstream"(输入文件流)对象,用于从文件读取数据。ofstream对象的使用方式类似于向标准输出写入的cout函数,而ifstream对象的工作方式类似于从标准输入读取的cin函数。

默认情况下ofstream覆盖而没有警告。

#include <fstream>
#include <string>
#include <iostream>
using namespace std ;

int main()
{
  string poem = "\n\tI never saw a man who looked" ;
  poem.append( "\n\tWith such a wistful eye" ) ;
  poem.append( "\n\tUpon that little tent of blue" ) ;
  poem.append( "\n\tWhich prisoners call the sky" ) ;

  ofstream writer( "poem.txt" ) ;

  if( ! writer )
  {
    cout << "Error opening file for output" << endl ;
    return -1 ;
  }
  
  writer << poem << endl ;
  
  writer.close() ;

  return 0 ;
}

追加

创建文件流对象时,名称后可含额外的参数,指定一系列的文件 "模式 "来控制该文件流对象的行为。这些文件模式是ios命名空间的一部分。

使用using namespace std; 的预处理器指令允许省略std命名空间前缀--因此cout指的是std::cout函数。ios命名空间存在于std命名空间中--所以文件模式也可以明确地使用两个命名空间前缀来处理,例如std::ios::out。

用"|"管状字符分隔,可以指定多个模式。当没有明确指定模式时,默认行为是将文件视为文本文件,重写。
最常的模式是ios::app,它不会覆盖原来内容

append.cpp

#include <fstream>
#include <string>
#include <iostream>
using namespace std;

int main()
{
  string info = "\n\tThe Ballad of Reading Gaol" ;
  info.append( "\n\t\t\tOscar Wilde 1898" ) ;

  ofstream writer( "poem.txt" , ios::app ) ;

  if( ! writer )
  {
    cout << "Error opening file for output" << endl ;
    return -1 ;
  } 
  
  writer << info << endl ;
  
  writer.close() ;

  return 0 ;
}

读取字符和行

ifstream filestream对象的get()函数,可以用来读取文件。

read.cpp

//#include <string>
#include <fstream>
#include <iostream>
using namespace std ;

int main()
{
  char letter ;
  int i ;
  //string line ;

  ifstream reader( "poem.txt" ) ;

  if( ! reader )
  {
    cout << "Error opening input file" << endl ;
    return -1 ;
  }
  else
  for( i = 0; ! reader.eof() ; i++ )
  {
    reader.get( letter ) ;
    cout << letter ;

    //getline( reader , line ) ;
    //cout << line << endl ;
  }

  reader.close() ;
  
  cout << "Iterations: " << i << endl ;
  
  return 0 ;
}

使用getline进行格式化

getline()函数可以选择有第三个参数,以指定停止读取行的分隔符。这可以用来分隔从数据文件中的表格列表中读取的文本。

format.cpp

#include <fstream>
#include <string>
#include <iostream>
using namespace std ;

int main()
{
  const int RANGE = 12 ; // Create a size constant.
  string tab[ RANGE ] ; // Create a string array.
  int i = 0 , j = 0 ; // Create counter variables.

  ifstream reader( "records.txt" ) ; // Create input file object.

  if( ! reader ) // Always check this.
  {
    cout << "Error opening input file" << endl ;
    return -1 ;
  }
  
  while( ! reader.eof() ) // Loop through data...
  {
    if( (i+1) % 4 == 0 )
      getline( reader, tab[ i++ ], '\n' ) ; // If last item on line stop at \n.
    else 
      getline( reader, tab[ i++ ], '\t' ); // Otherwise stop at \t.
  }
  reader.close() ;
  
  i = 0 ; // Reset the counter.

  while( i < RANGE ) // Display each record.
  {
    cout << endl << "Record Number: " << ++j << endl ;
    cout << "Forename: " << tab[ i++ ] << endl ;
    cout << "Surname: " << tab[ i++ ] << endl ;
    cout << "Department: " << tab[ i++ ] << endl ;
    cout << "Telephone: " << tab[ i++ ] << endl ;
  }
  
  return 0 ;
}

相关文章

  • c++快速入门5:文件处理1读写及getline

    本章演示了如何在文本文件中存储和检索数据,并说明了如何避免C++程序中的错误。 写文件 写文件的格式可以被指定为人...

  • C++ 语法基础

    C++ 语法入门 第1行: #include 是一个头文件库,允许我们处理输入和输出对象,例如cout(在第5...

  • 文件读写总结

    1. C++文件读写详解 1.1. 文件读写操作 使用方式 1.1.1. 打开文件 文件操作通过成员函数open(...

  • 第十三单元 python的文件操作和异常处理

    目录具体内容文件操作1:文件操作介绍2:文件的打开与关闭 3:文件的读写4:文件及文件夹的相关操作异常处理1:异...

  • c++快速入门5:文件处理2格式化及异常处理

    输入和输出 输入和输出流的行为可以用cout和cin函数的 "插入运算符 "来修改。为它们的width()函数指定...

  • QT-参考书

    《Qt Creator快速入门》《Qt5编程入门》 Qt进阶书籍推荐官方的《C++ GUI Qt4编程》

  • Python之路7:文件处理

    文件处理的操作 基础语法: python打开文件常用模式: 只读,读写,二进制只读及读写模式 只读模式打开一个文件...

  • 入门(七)异常处理和IO

    1、异常处理 语法 (1)、示例 2、自定义异常 3、IO操作 (1)、文件读写 模式介绍 (2)、文件读写的简写...

  • 2019-03-06 C++二进制文件结构体读取问题

    C与C++的二进制文件读写 参考下面的文章,C/C++读写文本文件、二进制文件 https://blog.csdn...

  • 01c中的文件操作

    代码及解释如下 可新建一个c++类来测试代码理解更加深刻 之后加载shader文件会用到c文件读写

网友评论

      本文标题:c++快速入门5:文件处理1读写及getline

      本文链接:https://www.haomeiwen.com/subject/vbvbmltx.html