当前位置: 首页 > news >正文

张店网站建设哪家好wordpress 滑动菜单

张店网站建设哪家好,wordpress 滑动菜单,重庆网红打卡景点排行榜,WordPress获取用户的标签write in advance 最近在做题,遇到一个简单的将console的输入输出到文件中的简单题目,没有写出来。悔恨当初没有踏实地总结string 相关的 I/O 以及与文件的操作。这篇文章旨在记录基础的字符I/O, 简单常用的文件I/O操作函数。 当然,你会说C…

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/Ostd::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编写的项目,是一项大工程。

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

http://www.yayakq.cn/news/523247/

相关文章:

  • 怎样做有效的黄页网站做财税的网站有哪些
  • 网站开发数据库问题国土局网站建设方案
  • 本地资讯网站做的最好的常州天宁区做网站公司
  • 十大国外室内设计网站做h5那个网站好
  • 哪种浏览器可以打开所有网站做公司网站合同
  • 咨询公司工资一般多少seo运营学校
  • 建设网站什么语言重庆龙华网站建设公司
  • 电商网站设计思维导图短网址生成系统源码
  • 娄底公司网站建设社区电商平台排名
  • iis7建设网站如何进行个人网页制作
  • 湛江网站seo外包linux 下启动 wordpress
  • 大连做网站孙晓龙宝安网站多少钱
  • 如何建立团购网站2345网址大全参数
  • 福州网站制作有限公司网站海外推广方法
  • 营销型网站建设成为企业发展新趋势开发区人才市场官网
  • 网络营销的主要形式有建设网站做书评的网站
  • 温州优化网站方法可以建设个人网站
  • 济南手机建站价格兰州市建设厅官方网站
  • 莆田网站建站建设天津实体店网站建设
  • 宣传网站制作方案网页设计与制作模板免费
  • 烟台h5网站制作朋友圈广告30元 1000次
  • 教学网站的设计简短的营销软文范文
  • 建设网站的运行费包括什么地方品牌全案策划
  • 网站开发个人简历给别人做网站会连累自己吗
  • 做网站相册本地wordpress
  • cps推广网站wordpress插件cdn
  • 乐清建网站公司哪家好单位网站建设费用什么会计科目
  • 网站建设什么最重要攻击自己做的网站吗
  • 做钓鱼网站教程视频教程保健品 东莞网站建设
  • 什么网站上可以做简历网址搜索器