网站关键词优化排名怎么做,装修公司电话号码大全,微信小程序怎么做问卷,wordpress plugin开发文章目录 一、C语言的IO1.键盘与显示屏2. 文件与内存3.字符串与内存 二、CIO1.iostream1.1基本使用1.2operator bool 2. fstream2.1二进制的文件读写2.2字符串的文件读写 3. sstream3.1序列化与反序列化3.2拼接字符串3.3将数据类型转换为字符串 总结 一、C语言的IO
1.键盘与显… 文章目录 一、C语言的IO1.键盘与显示屏2. 文件与内存3.字符串与内存 二、CIO1.iostream1.1基本使用1.2operator bool 2. fstream2.1二进制的文件读写2.2字符串的文件读写 3. sstream3.1序列化与反序列化3.2拼接字符串3.3将数据类型转换为字符串 总结 一、C语言的IO
1.键盘与显示屏
//键盘与显示屏的交互
int main()
{//从键盘读取格式化字符串。int i 0;scanf(%d, i);//在显示屏上打印获取到的信息。printf(%d, i);return 0;
}2. 文件与内存
int main()
{FILE* fptr fopen(test.txt, w);//w只写、w可读可写。//向文件里输入格式化字符串fprintf(fptr, %d hello, 123);//调整文件指针的位置为开头rewind(fptr);//从文件里面读取字符串与数字。char arr[15] { 0 };int n 0;//只能以某种形式对字符串进行解读fscanf(fptr, %d%s, n,arr);printf(%s %d, arr, n);fclose(fptr);//关闭文件并刷新缓存区。return 0;
}3.字符串与内存
//字符串的输入输出对字符串按指定方式进行解读。
int main()
{//从内存向字符串输出可控制的格式化字符串char str[100];sprintf(str, hello %d, 11);//这里的11可变换为变量。cout 输出的字符串为 str endl;//从字符串向变量中读入可控制的格式化字符串int i 0;char buf[256];sscanf(str, %s %d,buf,i);cout输入的字符为: i buf endl;return 0;
}总结 printf 与 scanf 是显示屏与键盘的IO函数 sprintf 与 sscanf 是字符串与内存的IO函数 fprintf 与 fscanf 是内存与文件的IO函数 唯一不足的就是读取只能一个一个按照顺序的进行读取无法按照自己想法。
对输入输出即 printf 与 scanf 的英文理解 说明这是基于内存的理解方便理解printf即从内存往外输出scanf即从外面向内存读写。
补充程序在运行时默认打开三个流——stdin,stdout,stderror。 至于更多C语言的文件操作内容详见【进阶C语言】文件操作 二、CIO
c以继承的方式梳理了所有的流。 其中iostream是我们经常包的头文件现在看来竟然还是一个菱形虚拟继承不愧是实现库的大佬没有困难创造困难迎着困难上,不过也为我们提供一点的便利。
1.iostream
1.1基本使用
cin和cout是我们经常使用的。cerr是面向标准错误流的clog是应用于日志的不过日常我们使用前两个即可。
先来看最简便的一段C代码
int main()
{int x;cin x;//从键盘对x进行输入数据cout hello world endl;//向显示器上打印数据return 0;
}不知各位刚学这一段代码的时候是什么感觉我是感觉看起来比较便捷形象易懂只是最近再看其实就是一大堆的运算符重载形成的集合而且当我们想要用char*打印地址时它其实给我们呈现的是一段字符串,而不是地址, 不过用起来是真香。
对自定义类型输入和输出比如之前文章中出现的日期类
class Date
{friend ostream operator (ostream out, const Date d);friend istream operator (istream in, Date d);
public:Date(int year 1, int month 1, int day 1):_year(year), _month(month), _day(day){}
private:int _year;int _month;int _day;
};
istream operator (istream in, Date d)
{in d._year d._month d._day;return in;
}
ostream operator (ostream out, const Date d)
{out d._year d._month d._day;return out;
}
int main()
{Date d1;cin d1;//其原理就是调用自定义的运算符重载函数。cout d1 endl;//同理。return 0;
}稍微总结一下
使用运算符重载流插入和流提取进行实现更加形象易懂。自定义类型也可重载自己的流插入和流提取也可使用cin和cout。自动类型识别无需在进行格式化控制。
说明 运算符重载vs函数重载 有更严格的要求要尽可能在多种状况下都能正确运行。运算符重载则需要更多的了解系统中对表达式处理的各种默认形式那些是你无法去改变的只能努力适应。 实用小知识
同时回想在进行刷题时如果面临读入一整行的字符串数据有什么方法吗
gets
char* gets(char* str);缺陷这个函数比较危险是因为从缓存区往str读入数据时并不知道str所指向空间的大小因此可能会导致使str指向的空间进行越界访问因此是危险的。说明Cvs2019下此函数已删除。
fgets
char * fgets ( char * str, int num, FILE * stream );基本使用
int main()
{char buf[256];//从标准输入流键盘里读取最大不超过buf大小的一行数量到buf数组中fgets(buf, sizeof(buf), stdin);cout buf endl;return 0;
}scanf
int main()
{char buf[256];//从标准输入流键盘里读取一行数量到buf数组中scanf(%[^\n], buf);//直到遇到换行才停止。cout buf endl;return 0;
}缺陷是跟gets一样的不过因为比较好用编译器保留了下来可以用宏/预处理指令进行屏蔽错误。
4.getlineC
c里面的string里面提供了一个接口getline可以获取一行的字符串。
#includeiostream
#includestring
int main()
{string buf;//从标准输入流键盘里读取一行数量到buf中getline(cin, buf);cout buf endl;return 0;
}这是最便捷的也最安全的输入一行字符串的方法因为string内部会自动进行扩容也是C刷题最经常用的方法。
1.2operator bool 除此之外,我们可能对多组输入的cin会产生疑惑 , 比如如下一段代码
int main()
{int x;while (cin x){cout x endl;}return 0;
}istream operator (int val);
//返回的是cin也就是类型为istream的对象
//而while()里面进行判断的是bool值两者能进行转换吗答案是肯定能的
//那是如何进行转换的呢就比如
class A
{
public:
private:int _a 0;
};
int main()
{A a;if(a)//此处会报错表达式必须包含 bool 类型(或可转换为 bool) {}return 0;
}那如何转换为bool呢其实运算符重载就出现了。
class A
{
public:operator bool(){return _a 0 ? false : true;}
private:int _a 0;
};
int main()
{A a;if(a){}//这也间接说明iostream是实现了operator bool的。return 0;
}说明这里的实现的运算符重载没有返回值是有点奇怪的这里当做特殊情况对待即可不必深究。
cin转bool底层原理设置标记 补充在对循环终止时有两种方式ctrl z正常终止循环 / Ctrl c(退出当前程序)。 2. fstream
在进行输入/输出时可有几种模式 对于fostream的对象默认打开了out对于fistream的对象默认打开了in。操作可通过 | 进行结合起来。truncate——对文件之前的内容进行覆盖。对于有些对象比如fstream可以支持输入也可以支持输出。
2.1二进制的文件读写
struct PeopleInfor
{PeopleInfor(){}PeopleInfor(const char* id,const char* name,int age)//:_id(id){strcpy(_id, id);strcpy(_name, name);_age age;}char _id[256];//家庭住址char _name[256] {0};//名字int _age 0;//年龄
};
struct ManagePeoInf
{//初始化ManagePeoInf(const char* file):_FileName(file){}//向文件以二进制写入个人信息void WriteBianry(const PeopleInfor pe){ofstream of(_FileName, ios_base::out | ios_base::binary);//这里的out默认已经有了可以不写。of.write((char*)pe, sizeof(pe));}void ReadBinary(PeopleInfor pe){ifstream in(_FileName, ios_base::in | ios_base::binary);//这里的in默认已经有了可以不写。in.read((char*)pe, sizeof(pe));}//向文件以二进制读取个人信息string _FileName;
};void BinaryWrite()
{PeopleInfor pe1 { 河南省,舜华,19 };ManagePeoInf m1(text.txt);//向文件中输出字符串xm1.WriteBianry(pe1);
}
void BinaryRead()
{PeopleInfor pe2;ManagePeoInf m2(text.txt);m2.ReadBinary(pe2);cout pe2._id pe2._name pe2._age endl;
}
int main()
{//向文件里以二进制的形式写入数据BinaryWrite();//从文件中以二进制的形式读取数据BinaryRead();return 0;
}说明这里的_id与_name并没有采用string的形式进行使用而是char类型的数组进行的使用其原因在于拷贝的是对象的内存而string里面存的是指向字符串的地址string释放时读取在恢复时其实是一段野指针即使访问成功了也是幸运。 2.2字符串的文件读写
struct PeopleInfor
{PeopleInfor(){}PeopleInfor(const char* id, const char* name, int age):_id(id){//strcpy(_id, id);strcpy(_name, name);_age age;}string _id;//家庭住址char _name[256] { 0 };//名字int _age 0;//年龄
};
struct ManagePeoInf
{//初始化ManagePeoInf(const char* file):_FileName(file){}//向文件以二进制写入个人信息void WriteText(const PeopleInfor pe){ofstream of(_FileName, ios_base::binary);of pe._id pe._name pe._age;//这里的id是调用了string的流插入。}void ReadText(PeopleInfor pe){ifstream in(_FileName, ios_base::binary);in pe._id pe._name pe._age;//这里的_id是调用了string的流提取。//读取自动识别空格与换行与分割符。}//向文件以二进制读取个人信息string _FileName;
};
void WriteText()
{PeopleInfor pe1 { 河南省,舜华,19 };ManagePeoInf m1(text.txt);m1.WriteText(pe1);
}
void ReadText()
{PeopleInfor p2;ManagePeoInf m2(text.txt);m2.ReadText(p2);cout p2._id p2._name p2._age endl;
}
int main()
{WriteText();ReadText();return 0;
}3. sstream
3.1序列化与反序列化
此处说明这里存在序列化与反序列化因为在存储的形式中要么以二进制的形式进行存储人看不懂要么以字符串的形式进行存储序列化总之在计算机用于计算的类型比如intfloatdouble等形式在文件中不复存在因此计算机要想用存储的数据进行计算就需要将字符串转换为相应的格式再参与计算反序列化。
#includeiostream
#includestring
#includesstream
using namespace std;
class Date
{friend ostream operator (ostream out, const Date d);friend istream operator (istream in, Date d);
public:Date(int year 1, int month 1, int day 1):_year(year), _month(month), _day(day){}
private:int _year;int _month;int _day;
};
istream operator (istream in, Date d)
{in d._year d._month d._day;return in;
}
ostream operator (ostream out, const Date d)
{out d._year d._month d._day;return out;
}
int main()
{string str;Date d { 2023,10,21 };ostringstream os;os d;str os.str();cout字符串: str endl;//再将字符串转换为日期类Date d1;istringstream is(str);is d1;cout日期类: d1 endl;return 0;
}这里附上一道序列化与反序列化的题二叉树的序列化与反序列化
3.2拼接字符串
#includeiostream
#includestring
#includesstream
using namespace std;
int main()
{ostringstream os;os hello;//当然也可是一些变量比如intos world;//同理cout os.str() endl;return 0;
}说明 stringstream实际是在其底层维护了一个string类型的对象用来保存结果。多次数据类型转化时一定要用clear()来清空才能正确转化但clear()不会将stringstream底层的string对象清空。可以使用s. str(“”)方法将底层string对象设置为空字符串。可以使用s.str()将让stringstream返回其底层的string对象。stringstream使用string类对象代替字符数组可以避免缓冲区溢出的危险而且其会对参数类型进行推演不需要格式化控制也不会出现格式化失败的风险因此使用更方便更安全。 3.3将数据类型转换为字符串
int main()
{int a 12345678;string sa;// 将一个整形变量转化为字符串存储到string类对象中stringstream s;s a;s sa;s.str();//将s维护的字符串置为空s.clear();// 说明// stringstreams在转换结尾时(即最后一个转换后),会将其内部状态设置为badbit// 因此下一次转换是必须调用clear()将状态重置为goodbit才可以转换double d 12.34;s d;s sa;string sValue;sValue s.str();cout sValue endl;return 0;
}总结 今天的分享就到此结束了我是舜华期待与你的下次相遇