#include<iostream>
#include<fstream>
#include<iomanip>
#include<sstream>
#include<strstream>
using namespace std;
void String_stream_test()
{
cout << "以下为字符串输入输出流和基本数据类型的转化" << endl;
char str[] = " 12 3 45 6789 -1";
int data[5];
istrstream data_in(str);
for (int i = 0; i < 5; i++)
{
data_in >> data[i];
data_in.clear();
}
cout << "接下来我们输出data数组" << endl;
for (int i = 0; i < 5; i++)
{
cout << data[i] << endl;
}
cout << "与预期结果相符" << endl;
stringstream ss("123test q c");//用istrstream的派生类stringstream同样可以进行输入
int a;
ss >> a;
cout << a << endl;//输出123
cout << "输出123,与预期结果相同" << endl;
ss.clear();
string b;
ss >> b;
cout << b << endl;//输出test
cout << "输出test,与预期结果相同" << endl;
ss.clear();
char c;
ss >> c;
cout << c << endl;
cout << "输出q,与预期结果相同" << endl;
ss.clear();//频繁的输入输出要记得调用clear清除错误信息
cout << "以下进行将长字符串以空格为分割符,依次输出的测试" << endl;
ss.str("the test is in the corner");
string receive_ss;
while (ss >> receive_ss)
{
cout << receive_ss << endl;//依次以空格符为标识符,进行输出
}
//测试调用字符串流的函数
cout << "测试调用字符串流的函数" << endl;
cout << ss.str() << endl;//返回一个string
//测试输入流的插入
cout << "测试输入流的插入" << endl;
ss.clear();
ss.str("");
ss << "hello";
cout << "调用str函数得到"<<ss.str() << endl;
ss << "world";
cout << "调用str函数得到"<<ss.str() << endl;
cout << "可以看出,往字符串流里插入,是一种类似append的操作,而不是一种覆盖" << endl;
//stringstream还可以与fstream结合,从文件中读取数据
cout << "测试stringstream与fstream的融合" << endl;
ifstream in("C:\\Users\\4399\\stringstream.txt");
ss.clear();
ss.str("");
//将 in所关联的文件中的内容输入到ss中
ss << in.rdbuf();
cout << ss.str()<<endl;
//跟预期结果相同
}
void Binary_file_read_write_test()
{
cout << "进行二进制的读写测试" << endl;
int arr[50];
for (int i = 0; i < 50; i++)
{
arr[i] = i;
}
ofstream ofs("C:\\Users\\4399\\scy.txt", ios::binary);
ofs.write(reinterpret_cast<char*>(arr), sizeof(arr));
ofs.close();//打开之后要关闭
ifstream ifs("C:\\Users\\4399\\scy.txt", ios::binary);
int file_arr[50];
ifs.read(reinterpret_cast<char*>(file_arr), sizeof(file_arr));
ifs.close();
bool jud = 1;
for (int i = 0; i < 50; i++)
{
if (file_arr[i] != arr[i])//判断以下是不是读写成功了
{
jud = 0;
break;
}
}
if (jud)
{
cout << "读写成功" << endl;
}
else
{
cout << "读写失败" << endl;
}
}
void File_write_test_test()
{
//二进制的读写
int vec1[] = { 0,1,2,3,4,5,6,7,8,9 };
ofstream os("C:\\Users\\4399\\test.txt", ios_base::binary);
if (os.fail())
{
cout << "open failure" << endl;
exit(1);
}
os.write(reinterpret_cast<char*>(vec1), sizeof(vec1));
os.close();
int vec2[] = { 0,1,2,3,4,5,6,7,8,9 };
ifstream is("C:\\Users\\4399\\test.txt", ios_base::binary);
is.read(reinterpret_cast<char*>(vec2), sizeof(vec2));
for (int i = 0; i < 10; i++)
{
cout << vec2[i] << endl;
}
//文本的写
ofstream os2("C:\\Users\\4399\\test2.txt");
for (int i = 0; i < 10; i++)
{
os2 << vec2[i] << endl;
}
os2.close();
//文本的读取
cout << "reading txt" << endl;
ifstream is2("C:\\Users\\4399\\test2.txt");
for (int i = 0; i < 10; i++)
{
is2 >> vec2[i];
cout << vec2[i] << endl;
}
is2.close();
//测试append
cout << "接下来进行append的测试,结尾处我们插入了'append successfully' " << endl;
ofstream os3("\\Users\\4399\\test2.txt", ios::app);
os3 << "append successfully";
os3.close();
ifstream is3("\\Users\\4399\\test2.txt");
cout << is3.rdbuf()<<endl;
is3.close();
}
void setw_test()
{
cout << "setw测试" << endl;
cout << "a" << setw(8) << "b" << "a" << endl;
cout << "可以看出setw只对后面紧跟的输出起作用" << endl;
cout << "以下再次验证" << endl;
for (int i = 0; i < 3; i++) {
cout << "a" << setw(8) << "b" << setw(8);
}
cout << "\n\n\n";
}
void setfill_test()
{
cout << "setfill测试" << endl;
cout << setfill('*') << setw(5) << "a" << endl;
cout << "在控制台没有第二次手动输入setfill时候,再来调用setw函数" << endl;
cout << setw(5) << "a" << endl;
cout << "可以看出setfill函数具有后效性" << endl;
cout << "\n\n\n";
}
void setprecision_test()
{
cout << "setprecision测试" << endl;
double s = 12.3456;
cout << s << endl;
cout << "可以看出这个时候输出的值还是12.3456" << endl;
cout << "接下来我们调用一下setprecision(2),输出了" << setprecision(2) << s << endl;
cout << "在调用了setprecision(2)之后,这个时候输出的是12" << endl;
cout << "我们在不调用setprecision函数的情况下再输出一下s" << endl;
cout << s << endl;
cout << "依然输出了12,可以看出setprecision函数具有后效性质" << endl;
cout << "接下来我们调用一下setprecision(4),输出了" << setprecision(4) << s << endl;
cout << "在调用了setprecision(4)之后,输出了12.35,符合我们的预期" << endl;
cout << "接下来我们调用一下setprecision(1),输出了" << setprecision(1) << s << endl;
cout << "可以看出,当小数点的位数多于要保留的位数时候,会采用科学计数法" << endl;
}
int main()
{
String_stream_test();
Binary_file_read_write_test();
File_write_test_test();
setw_test();
setfill_test();
setprecision_test();
return 0;
}
实验E14:输入输出流
最新推荐文章于 2023-01-07 15:25:20 发布