C++ C_style string overview and basic Input funcitons

write in advance

最近在做题,遇到一个简单的将console的输入输出到文件中的简单题目,没有写出来。悔恨当初没有踏实地总结string 相关的 I/O 以及与文件的操作。这篇文章旨在记录基础的字符I/O, 简单常用的文件I/O操作函数。

当然,你会说C++ 已经有一个string class,我们只需要#include<string>就能够使用它带来的便捷性及强大的功能,无需烦恼细节。

但知道底层的具体情况在语言的学习阶段很重要,有利于打好基础。说不定以后需要自己写一个类似的string类呢?此外,#include的方式包含头文件会包含头文件中的所有信息,而有时我们可能只会用到其中的一小部分,这会使我们的文件变得很大。或者,我们可以自己写一个,只包含我们目前需要用的。当然,那是有一定积累之后的事情了。

此处我会试着提供一些代码片段(code snippet),作为例子。

简单的Input 函数

此处比较std::cin.get()函数,理解function overloading 概念。

function overloading指允许同名不同参数列表的函数存在,以不同的方式或不同的参数类型执行相同的基础操作。要重载一个函数,你必须提供不同的参数列表。

//function prototype
int std::cin.get(char ); // read a char argument, return a int type    
std::cin std::cin.get(char*,int);    // line-oriented function 
std::cin std::cin.get(char& ch); // the argument is char&, a reference
// any change to the ch modifys the original variable


// a simple output method
std::cout.put(char );    // display the type char character

此处给出几个简单的 line-oriented funcitons.

// both used to read a line, the so-called line-oriented method
// first argument is the name of the target, the second argument is a limit 
//  on the number of characters to be read

// when meeting the newline character, they behave differently
std::cin.get(char*, int );    // leave the newline character in the intput queue
std::cin.getline(char*, int ); // discards the newline character

// stop reading input when they reach the numeric limit or when they meet 
// a newline character, whichever comes first.


std::cin.get() // read the single next character, including the newline character
// it can be used to dispose of the newline character. 

两种方法都能够读取一整行内容。 

相比之下,getline() 更简单,get()更有利于错误检查。

when std::cin.getline() or std::cin.get() reads an empty line, both sets the failbit, which block further input. To recover from the state , std::cin.clear(); is needed.

 if the input line is longer than the num, std::cin.getline()  set the failbit and turn off further input while std::cin.get() do nothing. In additon, both of them leave the remaining characters in the input queue.  once again, the failbit block further input, which should be reset through statement

std::cin.clear();

值得注意到是,当混合数字、字符串输入时,有时也会遇到 failbit 阻止更多输入的情况。这种情况下,同样需要使用 std::cin.clear(); 

此处也能够将两个式子连接起来,因为函数的返回值为 std::cin 对象。

//both them returns a cin object
std::cin.get(char*,int);
std::cin.getline(char* int);

// so statement like this is available
std::cin.get(char*,int).get();
std::cin.getline(char*,int).get();

// in this situation, it is the concatenation

string class 中同样存在读取一整行的函数getline(), 它的两个参数及其意义与 std::cin.getline(char*,int )并不相同。第一个参数说明输入的来源,此处是控制台输入,也可以是文件输入;第二个参数说明要输入哪个string 对象。 注意到getline()是一个函数,不是方法,这是友元函数的概念。

#include<string>
string str;
getline(std::cin, str);    // a string class object to read a line
                           // it is a friend function, needn't to take care about the size
                         // cause the stirng class automatically resize when change occure
std::ifstream fin;
fin.open("mytest.txt");  // assume there exist a mytest.txt file, and it is not empty  
getline(fin, str);   // read a line from mytest.txt to str 

 以上是简单的line-oriented functions,后续也许会补充。

文件相关的 Input 函数

#include<iostream>
#include<fstream>  // for file I/O
 
std::ifstream infile;
std::ofstream outfile;
infile.open("test.txt");  infile.clost(); // outfile is the same

//check
infile.is_open();   // true if the file is open successfully
infile.good();  // input is good and not at EOF
infile.eof(); // return true when encounter the EOF
infile.fail(); // return ture when encounter the EOF 
// or when type mismatch, whichever come first

注意图中给出了部分用于定位错位原因,方便调试。

summary

line-oriented functions 读取一行输入,可以简单理解为读取句子。它和读取单词一样,都是字符串输入的简单函数。

文件是通常情况下我们需要使用的部分。与文件有关的I/O读写也是必须掌握的基础知识。

重定向 redirection 概念在console and file 的交互中产生。我们不仅能从standard input(你的键盘)读取输入,也能从文件中;类似的,不仅能将结果输出到 console (你的显示屏),也能输出到文件中。

现在回看,这篇文章实际内容并不多,是零零碎碎的语法总结。更多的目的,是为了防止以后再出现对简单问题无法正确答出的愚蠢情况。

后期,在更深入了解字符类的输入输出后,会重新回来更新这部分的内容。

added

编程语言 (C++, Python...)与文件有关的操作还与文件的格式、内部的书写相关。须知,.txt文件是最最简单的文本文件。而语言能对文件进行的操作也很繁多。比如 json文件, yaml文件,urdf文件,C++自然能够对它们进行操作。对文件的操作包括解析文件格式,提取特定内容等等部分,对特殊格式的文件读取,如解析json文件,网络上就有开源的用纯C编写的项目,是一项大工程。

所以,这些简单的,易得的,不需要如何动脑子的函数细节,自然需要牢固掌握,日后遇到大项目才不需要哪里都是漏洞。

相关推荐

最近更新

  1. TCP协议是安全的吗?

    2024-06-11 05:14:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-11 05:14:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-11 05:14:02       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-11 05:14:02       20 阅读

热门阅读

  1. Helm在线部署Longhorn(1.6.0版本)分布式存储

    2024-06-11 05:14:02       10 阅读
  2. 常用API

    常用API

    2024-06-11 05:14:02      12 阅读
  3. Python图库入门:从基础到进阶的全面解析

    2024-06-11 05:14:02       12 阅读
  4. Ubuntu24.04记录网易邮箱大师的安装

    2024-06-11 05:14:02       10 阅读
  5. 简单介绍Pacstall和 AUR软件管理工具

    2024-06-11 05:14:02       9 阅读