一、IO流基本操作
#include <iostream>
#include <iomanip> //輸出流控制字符
#include <string>
using namespace std;
struct MM
{
string name;
int age;
int num;
};
/*
流:流是由若干字節組成的字節序列
代表信息從源到目的的流動
流中內容:可以是二進位;ASCII碼;或其他形式
用類實現所有流操作---》流類體系結構
*/
int main()
{
cout << "ILoveyou" << endl;
int num;
cin >> num;
//輸出流對象:
//1.成員函數的使用方式
//字符和字符串
cout.put('A');
char cNum = 'C';
cout.put(cNum);
cout << endl;
char name[] = "女生";
cout.write(name,5);
//2.其他兩個輸出流對象 cerr;clog -->cout
int size = 0;
if (size == 0)
cerr << "棧不能為空" << endl; //表示錯誤輸出
clog << "一樣的效果" << endl; //表示錯誤輸出
//輸入流
//1.字符輸入
fflush(stdin);
cNum = cin.get();//輸入一個字符
cout << cNum << endl;
fflush(stdin);
cin.getline(name, 5); //輸入一個字符串
cout << name << endl;
//流控制字符 等於C語言中的格式空字符
//1.包含頭文件 iomanip
bool bNum = false;
//boolalpha //流控制字符
cout << boolalpha << bNum << endl;
struct MM array[3] = { "name1", 18, 1001, "name2", 28, 1002, "name3", 48, 1003 };
//設置寬度
//默認右對齊
cout << setiosflags(ios::left)<<setw(10) << "name"
<< setw(4) << "age"
<< setw(5) << "num" << endl;
for (int i = 0; i < 3; i++)
{
cout << setiosflags(ios::left) << setw(10) << array[i].name
<< setw(4) << array[i].age
<< setw(5) << array[i].num << endl;
}
//設置精度 :指有效位數,並不是小數位
cout << setprecision(3) << 343123.23 << endl;
cout << hex << 32 * 18 << endl;
cout.width(10); //設置寬度
cout.setf(ios::right);
cout << name << endl;
system("pause");
return 0;
}
二、字符流
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
//1.處理字符串
string info("ip地址: 192 168 12 1");
cout << info << endl;
//stringstream 構建對象
//stringstream 類名
//object: 對象名 標識符而已
stringstream object(info);//用info初始化字符流
//str();函數得到我們字符流對象中的字符串
cout << object.str() << endl;//通過字符流對象獲取字符流中內容
cout << "通過空格拆分每一部分:" << endl;
//把字符流當做是一次用戶的數據輸入,從字符流流向變量
string tempData;
cout << "字符流對象充當輸入的功能" << endl;
object >> tempData; //等價於cin
//cin>>tempData; 鍵盤-->輸入流對象--->tempData;
//int num;
//cin >> num;//用戶輸入鍵盤 數據流到cin對象中,cin對象流到num中
//12 234 34 34
cout << tempData << endl;
int a[4];
for (int i = 0; i < 4; i++)
{
object >> a[i];
cout << a[i] << " ";
}
cout << endl;
string score("12345");
stringstream strToInt(score);
int result = 0;
strToInt >> result;
cout << result << endl;
system("pause");
return 0;
}
三、文件流操作
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
/*
fstream: 類 可讀可寫
ifstream :read 只讀
ofstream :write 只寫
1.打開文件
1.1 定義一個文件對象
fstream file;
1.2 打開文件
file.open(char *fileURL,int mode);
mode: //讀寫方式
ios::in 讀
ios::out 寫
ios::app 追加
注意點:組合方式:
ios::in|ios::out 位或
2.讀寫文件
2.1 流運算符來操作 >> <<
2.2 成員函數
write(char *dst,size_t size);
寫的首地址
寫多少個字節
read(char *dst,size_t size);
3.關閉文件
file.close();
4.操作文件指針
//fseek
seekg(size_t length,int position)
ios::beg
ios::cur
ios::end
*/
struct MM
{
string name;
int age;
int num;
};
int main()
{
//流的方式操作文件
fstream file1;
file1.open("流運算符讀寫.txt", ios::out | ios::in | ios::trunc);
int num = 1001;
file1 << "ILoveyou" <<' '<<num<< endl;
file1.seekg(0, ios::beg); //移動到開始位置
char str[10];
int result = 0;
file1 >> str>>result;
cout << str<<":"<<result << endl;
file1.close();
//通過成員函數去讀寫
struct MM array[3] = { "name1", 18, 1001, "name2", 28, 1002, "name3", 48, 1003 };
fstream fMM;
fMM.open("MM.txt", ios::out | ios::in | ios::trunc);
fMM.write((char *)&array[0], sizeof(struct MM) * 3);
fMM.seekg(0, ios::beg);
MM readInfo[3];
fMM.read((char *)&readInfo[0], sizeof(struct MM) * 3);
for (int i = 0; i < 3; i++)
{
cout << setiosflags(ios::left) <<
setw(6) << readInfo[i].name <<
setw(4) << readInfo[i].age <<
setw(5) << readInfo[i].num << endl;
}
fMM.close();
system("pause");
return 0;
}
四、異常處理
#include <iostream>
#include <string>
using namespace std;
/*
異常:什麼是異常?萬物即可是異常 包含錯誤
拋出異常:throw 可以拋出任何東西
捕獲異常:try
處理異常:catch 根據捕獲的異常的類型去處理
注意:拋出的異常沒有被處理,會調用默認的abort函數終止程序
*/
void print(int a, int b)
{
//以前做法:當前有問題當前解決
if (b == 0)
return;
printf("%d", a / b);
}
void printError(int a, int b)
{
if (b == 0)
throw b;
printf("%d", a / b);
}
void printError(int a)
{
throw string("任何東西都是異常");
}
int main()
{
print(1, 0);
//{}必須存在
try
{
printError(2, 0);
printf("ILoveyou!");
}
catch (int)//
{
cout << "除數不能為0!" << endl;
}
catch (double)
{
cout << "double" << endl;
}
try
{
printError(12);
}
catch (string object) //string object=拋出的內容
{
cout << object << endl;
}
try
{
printError(2, 0);
}
catch (...)
{
printf("任何東西都是異常!\n");
}
system("pause");
return 0;
}
五、自定義類型的異常
#include <iostream>
#include <string>
using namespace std;
class stackEmpty
{
public:
stackEmpty(string str = "棧為NULL") :str(str){}
void print()
{
cout << str << endl;
}
protected:
string str;
};
void pop(int size)
{
if (size == 0)
throw stackEmpty("棧為NULL,無法操作!");
}
//不存在的異常的函數
int Max(int a, int b) throw()
{
return a > b ? a : b;
}
int main()
{
try
{
pop(0);
}
catch (stackEmpty object)
{
object.print();
}
system("pause");
return 0;
}
六、C++庫中異常
#include<exception>
#include<string>
#include<iostream>
using namespace std;
/*
exception: 異常的基類
what方法用來列印相關異常的信息
//異常的派生類
bad_alloc new的異常
out_of_range 溢出
runtime_error 運行時問題
range_error
length_error
*/
int main()
{
try
{
int **pMomery = new int*[100000000];
for (int i = 0; i < 1000; i++)
{
pMomery[i] = new int[100000000];
}
}
catch (bad_alloc object)
{
cout << object.what() << endl;
}
int array[5] = { 1, 2, 3, 4, 5 };
int *p = &array[4];
//p[0]
//cout<<p[0]<<endl;
cout << p[-3] << endl;
system("pause");
return 0;
}