做网站服务器 自己电脑还是租,自己制作wordpress plugin,网站优化包括对什么优化,淘宝客网站建设的策略文章目录 一、类型转换C语言隐式类型转换强制类型转换 C类型转换的情况类型转换的函数#xff08;4个#xff09; 二、IO流1、缓冲区2、提高输入输出效率3、文件操作文件权限写操作 --- ofstream文本方式写入二进制方式写入 读操作 --- ifstream文本方式读取二进制方式读取 其… 文章目录 一、类型转换C语言隐式类型转换强制类型转换 C类型转换的情况类型转换的函数4个 二、IO流1、缓冲区2、提高输入输出效率3、文件操作文件权限写操作 --- ofstream文本方式写入二进制方式写入 读操作 --- ifstream文本方式读取二进制方式读取 其他操作判断文件是否成功打开? 一、类型转换 当两个类型不是严格的相同的时候就会有类型转换的问题。 C语言 隐式类型转换和强制类型转换。 隐式类型转换 适用于类型相似的内置类型整型、浮点数、字符型之间进行类型转化。比如整型和字符型之间整型和浮点数之间。 int a 10;
double b a; // 整型和浮点数之间
char c a; // 整型和字符型之间强制类型转换 适用于整型和指针之间指针和指针之间。 int a 10;
int* pa a;
int b (int)pa; // 整型和指针之间
double* pb (double*)b; // 指针和指针之间C
类型转换的情况 C不仅仅兼容C的类型转换还有一些自定义类型之间的转换。 内置类型 - 自定义类型 自定义类型 - 内置类型 自定义类型 - 自定义类型 // 内置类型转换成自定义类型
// 通过构造函数实现的
class A
{
private:int _a;int _b;
public:A(int a, int b):_a(a), _b(b){}
};
A a(1, 3);// 自定义类型转换成内置类型
// 通过get函数将A类型转换成int
class A
{
private:int _a;int _b;
public:friend int get(A a);A(int a, int b):_a(a), _b(b){}
};
int get(A a)
{return a._a;
}// 自定义类型转换成自定义类型
// 将子类赋值给父类也可以通过构造函数实现方法不唯一
class A
{
private:int _a;int _b;
public:A(int a 1, int b 1):_a(a), _b(b){}
};
class B:public A
{
private:int _c;
public:B(int c 1):_c(c){}
};
B b;
A a b;类型转换的函数4个
// 隐式类型转换static_castT
int a 10;
double b 3.4;
a static_castint(b);
// 强制类型转换reinterpretT
int a 10;
int* pa a;
int b reinterpret_castint(pa);
// const类型转换const_castT --- 用于删除变量的const属性
const int x 10;
int* px const_castint*(x);
*px 4;
std::cout *px std::endl;
// 向下转换dynamic_castT父类对象指针/引用 - 子类指针/引用
class A
{
public:virtual void print(){std::cout A: print() std::endl;}
};
class B : public A
{
public:virtual void print(){std::cout B: print() std::endl;}
};
A* a new B;
B* b dynamic_castB*(a);
b-print();二、IO流
1、缓冲区 输入输出是有缓冲区的概念的根据Linux中学的一切皆文件大概的也能明白因为文件里面有缓冲区。 // 根据下面的代码可以深刻的体会出缓冲区的概念。
// 一次性输入10x
// 下面会直接输出
// 10
// x
// 我们只输入的了一次为什么会直接都输出出来呢
// 答当我们输入10x的时候第一次因为变量a是int类型他只读到了10然后就直接退出了而x还在缓冲区中。当缓冲区不空的时候我们是没有办法继续往里面进行读数据的需要将其进行接收。然后ch正好是字符类型接收了x所以最后会直接打印出来而不需要我们输入两次。// 所以为了解决某种用户输入不正确的数据时需要我们进行判断然后对缓冲区进行清空以便不影响下一次输入。
#include iostream
int main()
{int a 0;std::cin a;std::cout a std::endl;char ch 0;std::cin ch;std::cout ch std::endl;return 0;
}2、提高输入输出效率
// 第一种方式--- 用C语言的输入输出
//printf
//scanf// 第二种方式 --- C
// 在main函数中加入这三句语句一个main函数只需要写一次
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);3、文件操作
进行文件操作的时候只需要包含fstream即可。
下面是文件头文件的结构图。 文件权限
ios::in以输入读取模式打开文件。ios::out以输出写入模式打开文件。ios::app以追加模式打开文件写入的数据会追加到文件末尾。ios::ate打开文件时立即将文件指针移动到文件末尾。ios::trunc如果文件已存在打开时清空文件内容。ios::binary以二进制模式打开文件而不是默认的文本模式。
写操作 — ofstream
文本方式写入
#include iostream
#include fstreamint main()
{// 以文本方式打开文件如果默认路径下没有文件则新建文件、// 默认的权限是ios::out// std::ofstream fout(test.txt, std::ios::out); --- 以写的方式打开文件// std::ofstream fout(test.txt, std::ios::out | std::ios::app); --- 以追加模式的写的方式打开文件// ...std::ofstream fout(test.txt);// 文本输入fout 12434\n;fout abcdefg;fout hijk;// 关闭文件fout.close(); return 0;
}二进制方式写入
#include iostream
#include string
#include fstream
#include vectorint main()
{std::vectorint v { 1, 2, 3, 4 };// 打开文件以写的方式std::ofstream fout(test.txt, std::ios::out | std::ios::binary);// 写入文件// ostream write (const char* s, streamsize n);for (auto e : v){fout.write(reinterpret_castchar*(e), sizeof(e));}// 关闭文件fout.close();return 0;
}读操作 — ifstream
文本方式读取
#include iostream
#include string
#include fstreamint main()
{// 打开文件以读的方式std::ifstream fin(test.txt);// 读取文件std::string tmp;while (fin tmp){std::cout tmp std::endl;}// 关闭文件fin.close();return 0;
}二进制方式读取
// 以什么方式写就以什么方式读
// 也要注意数据的格式如果是整型就整形方式读自定义类型就自定义类型方式读
#include iostream
#include string
#include fstream
#include vectorint main()
{// 打开文件以读的方式std::ifstream fin(test.txt, std::ios::in | std::ios::binary);// 读取文件char ch;while (fin.read(ch, sizeof(ch))){std::cout ch;}if (fin.good()){std::cout 读取成功 std::endl;}// 关闭文件fin.close();return 0;
}其他操作
判断文件是否成功打开?
#include iostream
#include string
#include fstream
#include vectorint main()
{// 打开文件以读的方式std::ifstream fin(test.txt, std::ios::in | std::ios::binary);// 判断是否成功打开文件if (!fin){std::cout 无法打开文件 std::endl;return 1;}// 关闭文件fin.close();return 0;
}谢谢大家