东北粘豆包批发地点:C 的输入输出: ios

来源:百度文库 编辑:中财网 时间:2024/04/28 19:43:05
C++ 的输入输出: ios_base、basic_ios类常用的成员函数
2009-02-07 16:00:48|  分类:C/C++ |字号 订阅
/* 范例:11-1 ios_base、basic_ios类常用的成员函数 */
#include
void main(void)
{
int n, p, num = 68;
float flo = 68.3889;
n = cout.width(); // 取得目前输入输出字段宽度设定
p = cout.precision(); // 取得目前浮点数精确位数设定
cout << "目前字段设定是: " << n << endl;
cout << "目前有效位数是: " << p << endl;
cout << num << endl; /* 输出num变量的数值,endl(一行的结束及换行) */
cout.width(6); // 使用成员函数width()设定其宽度为6
cout << num << endl; // 输出num变量的值,宽度为6
cout << num << endl;
cout.width(6);
n = cout.width(); /* 当设定宽度后, 取得目前输入输出字段宽度 */
cout << num << endl << "目前字段设定是: " << n << endl;;
cout << flo << endl;
cout.precision(2); /* 使用成员函数precision(),设定有效位数2位 */
cout << flo << endl;
p = cout.precision(); /* 当设定有效位数后,取得目前浮点数精确位数设定 */
cout << "目前有效位数是: " << p << endl;
getchar();
}
程序执行结果:
目前字段设定是: 0
目前有效位数是: 6
68
68
68
68
目前字段设定是: 6
68.3889
68
目前有效位数是: 2
/* 范例:11-2 标志 */
#include
void main(void)
{
int a = 68, b = 38, c = 6;
cout.setf(ios::showpos); // 当 >=0 显示'+'号
/* 对齐控制 */
cout << "/* 对齐控制: */ " <cout.width(10); // 设定宽度为10
cout.setf(ios::left);
cout << a << "\n";
cout.unsetf(ios::left); // #1.1 清除同一组靠左对齐设定
cout.setf(ios::right); // #1.2 设定同一组靠右对齐设定
cout.width(10);
cout << b << endl;
cout.unsetf(ios::adjustfield); // #2 取消对齐
cout.width(10);
cout << c << "\n\n";
/* 布尔 */
cout << "/* 布尔: */" << endl;
cout << "布尔常数 true = " << true << endl;
cout.setf(ios::boolalpha);
cout << "设定 boolalpha 后,布尔常数 true = " << true << endl;
// -----------------------------------------------------------
float f = 5.680;
int k = 24;
cout.setf(ios::showpoint); // 显示小数点
cout.setf(ios::showpos); // 当 >=0 显示'+'号
cout.precision(3); // 设定浮点数精确度
cout.setf(ios::showbase); // 完全显示几进制(0x,0)
cout.setf(ios::uppercase); // 转换16进位数值时,A~F以大写显示
/* 进位制控制 */
cout << "/* 进位制控制: */" << endl;
cout.setf(ios::hex, ios::basefield); // #3 设定以16进制显示
cout << "十进制 k = 24 十六进制 k = " << k << endl;
cout.setf(ios::oct, ios::basefield); // 设定以8进制显示
cout << "十进制 k = 24 八进位 k = " << k << endl;
cout.setf(ios::dec, ios::basefield); // 设定以10进制显示
/* 浮点数显示控制 */
cout << "/* 浮点数显示控制: */" << endl;
cout.precision(3); // 设定精度有效3位
cout.setf(ios::fixed); // 设定以fixed符号输出
cout << f << endl; // +5.680
cout.setf(ios::scientific,ios::floatfield); // 以科学记号表示
cout << f << endl; // +5.680E+00
/* 使用fill()成员函数 */
cout << "使用fill()成员函数" << endl;
cout.fill('#'); // 设定输出宽度内空白填入字符'#'
cout.width(10); // # 4
cout << 24 << endl;
cout.fill('\0'); // # 5
cout.width(10);
cout << 24 << endl;
getchar();
}
程序执行结果:
/* 对齐控制: */
+68
+38
+6
/* 布尔: */
布尔常数 true = 1
设定 boolalpha 后,布尔常数 true = true
/* 进位制控制: */
十进制 k = 24 十六进制 k = 0X18
十进制 k = 24 八进位 k = 030
/* 浮点数显示控制: */
+5.680
+5.680E+00
使用fill()成员函数
#######+24
+24
/* 范例:11-3格式操纵符(Manipulators) */
#include
#include
void main(void)
{
int a = 68, b = 38, c = 6;
cout << showpos; // 当 >=0 显示'+'号
/* 对齐控制 */
cout << "/* 对齐控制: */ " <cout << left; // #1.1
cout << setw(10); // 设定宽度为10
cout << a << "\n";
cout << right; // #1.2 设定靠右对齐
cout << setw(10) << b << endl;
cout << resetiosflags(ios::adjustfield); // #2 取消对齐
cout << setw(10);
cout << c << "\n\n";
/* 布尔 */
cout << "/* 布尔: */" << endl;
cout << "布尔常数 true = " << true << endl;
cout << boolalpha;
cout << "设定 boolalpha 后,布尔常数 true = " << true << endl;
// -----------------------------------------------------------
float f = 5.680;
int k = 24;
cout << showpoint; // 显示小数点
cout << showpos; // 当 >=0 显示'+'号
cout << setprecision(3); // 设定浮点数精度
cout << showbase; // 完全显示几进制(0x,0)
cout << uppercase; // 转换16进位数值时,A~F以大写显示
/* 进位制控制 */
cout << "/* 进位制控制: */" << endl;
cout << hex; // #3 设定以16进制显示
cout << "十进制 k = 24 十六进制 k = " << k << endl;
cout << oct; // 设定以8进制显示
cout << "十进制 k = 24 八进位 k = " << k << endl;
cout << dec; // 设定以10进制显示
/* 浮点数显示控制 */
cout << "/* 浮点数显示控制: */" << endl;
cout << setprecision(3); // 设定精度有效3位
cout << fixed << f << endl; // +5.680
cout << scientific << f << endl; // +5.680E+00
/* 使用fill()成员函数 */
cout << "使用fill()成员函数" << endl;
cout << setfill('#'); // 设定输出宽度内空白填入字符'#'
cout << setw(10) << 24 << endl;
cout << setfill('\0');
cout << setw(10) << 24 << endl;
getchar();
}
程序执行结果:
/* 对齐控制: */
+68
+38
+6
/* 布尔: */
布尔常数 true = 1
设定 boolalpha 后,布尔常数 true = true
/* 进位制控制: */
十进制 k = 24 十六进制 k = 0X18
十进制 k = 24 八进位 k = 030
/* 浮点数显示控制: */
+5.680
+5.680E+00
使用fill()成员函数
#######+24
+24
/* 范例:11-4 */
#include
#include
void main(void)
{
char *str="steven";
cout.put('s'); // 使用put()成员函数输出's'
cout << 'c' << 'a';
cout <cout.write(str,strlen(str)); /* write()输出长度(strlen(str))的str字符串 */
cout << endl;
getchar();
}
程序执行结果﹕
sca
steven
/* 范例:11-5 */
#include
#include
#include
void cin_buf_clear(void); // 清除输入缓冲区字符
void clear_state(int); // 输出被清除字符
void view_cin_buf(void); // 检测输入缓冲区中的字符
void main()
{
char str1[11];
char ch,ch1,ch2;
int count,int_ch;
/* get(),get(ch) */
cout << "测试cin.get(ch).get(ch1).get(ch2)......\n";
cout << "请输入三字符:";
cin.get(ch).get(ch1).get(ch2); // #1 无法检测
cout << "ch=" << ch << " ch1=" << ch1 << " ch2=" << ch2 << endl;
cin_buf_clear(); // #2 清除输入缓冲区
/* 以下比较get(),getline() */
cout << "\n比较get(str1,5,\'k\'),getline(str1,5,\'k\')===\n";
cout << "=====get(str1,5,\'k\')=======\n";
cout << "请输入字符 abkcdef : ";
cin.get(str1,5,'k');
cout << "str1 = " << str1 << endl;
count = cin.gcount(); // 上次取的字符数
cout << "cin.gcount()=" << count << endl;
int_ch = cin.peek(); // 查看输入缓冲区中的第一个字符
clear_state(int_ch); // #3 清除缓冲区,并输出清除字符
cout << "\n=====getline(str1,5,\'k\')=======\n";
cout << "请输入字符 abkcdef : ";
cin.getline(str1,5,'k');
cout << "str1 = " << str1 << endl;
count = cin.gcount(); // 上次取的字符数
cout << "cin.gcount()=" << count << endl;
int_ch = cin.peek(); // 输入缓冲区中的第一个字符
clear_state(int_ch); // 清除缓冲区,并输出清除字符
cout << "\nPress any key to continue...";
getche();
cin_buf_clear(); // 清除可能由getche()带入的字符
strcpy(str1,"0123456789");
cout << "str1 = " << str1 << endl;
cout << "请输入 abkcdef : ";
cin.read(str1,5); // 读入5字符(不自动加'\0','\n')
cout << "cin.read(str1,5) => str1=" << str1 << endl;
view_cin_buf(); // 检测输入缓冲区中的字符
cout << "\nPress any key to continue...";
getche();
cin_buf_clear();
cout << "\n测试 格式标志 cin.width(5) =================\n";
cout << "请输入 abkcdef : ";
cin.width(5); // #4 cin也可以使用width()
cin >> str1;
cout << "str1=\"" << str1 << "\"" << endl;
view_cin_buf();
cin_buf_clear(); // 清除输入缓冲区字符
cout << "测试 格式操纵符 ws =================\n";
cout << "请输入\" ab c d e fk\"(前置空白,tab...)\n";
cin >> ws; // #5 忽略掉前置空格符
cin.getline(str1,5);
cout << str1 << endl; // 忽略掉前置空白后,取得接下来的4个字符
getche();
}
/*************************************************************
cin_buf_clear():用来清除输入缓冲区中第一个'\n'(含)前的字符
**************************************************************/
void cin_buf_clear()
{
while((cin.peek())!= '\n') // 将输入缓冲区中所有字符忽略
{
cin.get();
}
cin.ignore(); // '\n'
}
/************************************************************
clear_state(char);用来显示被清除的字符
**************************************************************/
void clear_state(int ich)
{
if (ich != '\n')
{
int int_ch;
cout << "cin.peek()=" << ich \
<< ",(即" << (char)ich << ")" << endl;
// 清除输入缓冲区中,接下来字符...
cout << "清除 ";
for(int i=0;i<3;i++)
{
int_ch = cin.get();
cout << (char)int_ch;
}
cout << "...\n";
cin_buf_clear(); // 清除输入缓冲区
}
}
/************************************************************
view_cin_buf();用来显示输入缓冲区中的字符
**************************************************************/
void view_cin_buf()
{
char ch;
char buf[100];
int count=0;
cout << "输入缓冲区中目前字符: ";
while((ch=(char)cin.peek())!='\n')
{
cin.get(ch);
buf[count] = ch;
cout << ch;
count++;
}
// 将取出的字符,按顺序再放回缓冲区中
for(int i=count-1;i>=0;i--)
cin.putback(buf[i]);
cout << endl;
}
程序执行结果︰
测试cin.get(ch),get(ch1),get(ch2)......
请输入三字符:410
ch=4 ch1=1 ch2=0
比较get(str1,5,'k'),getline(str1,5,'k')===
=====get(str1,5,'k')=======
请输入字符 abkcdef : abkcdef
str1 = ab
cin.gcount()=2
cin.peek()=107,(即k)
清除 kcd...
=====getline(str1,5,'k')=======
请输入字符 abkcdef : abkcdef
str1 = ab
cin.gcount()=3
cin.peek()=99,(即c)
清除 cde...
Press any key to continue...
str1 = 0123456789
请输入 abkcdef : abkcdef
cin.read(str1,5) => str1=abkcd56789
输入缓冲区中目前字符: ef
Press any key to continue...
测试 格式标志 cin.width(5) =================
请输入 abkcdef :
abkcdef
str1="abkc"
输入缓冲区中目前字符: def
测试 格式操纵符 ws =================
请输入" ab c d e fk"(前置空白,tab...)
ab c d e fk
ab c
/* 范例:11-6 */
#include
#include
void main ()
{
ofstream out("my_err.txt"); /* 使用out函数,打开一个文件名叫my_err.txt */
clog << "错误输出到屏幕,可以使用clog或cerr\n";
if ( out ) // 如果文件成功打开,执行if以下语句
{
clog.rdbuf(out.rdbuf()); /* 使用clog将错误信息转向输出至所指定的文件
my_err.txt */
clog << "Error 1\n"; // 以clog输出错误信息到"my_err.txt"
clog << "Error 2\n";
clog << "Failure to Access 01\n";
clog << "Failure to Access 02\n";
}
else // 打开文件失败时(例如,将my_err.txt改为只读)
cerr << "Error while opening the file" << endl;
clog.rdbuf(cout.rdbuf());
getchar();
}
程序执行结果﹕(若my_err.txt打开文件成功时,文件内容如下:)
Error 1
Error 2
Failure to Access 01
Failure to Access 02
程序执行结果:(若打开文件失败,譬如,文件只读)
错误输出到屏幕,可以使用clog或cerr
Error while opening the file
/* 范例:11-7 */
#include
void main()
{
ofstream out_file("out.txt"); // # 1 if(变更out.txt属性只读,隐藏)
out_file << "My name is Peipei.\n";
out_file.put('B').put('o').put('y'); // # 2如同cout.put(ch)...
cout << "out_file.is_open()=" << out_file.is_open() << endl;
out_file.close(); // # 3
ifstream in_file("out.txt"); // # 4 if(变更为不存在文件,out1.txt)
cout << "in_file.is_open()=" << in_file.is_open() << endl; // # 5
cout << "in_file.fail()=" << in_file.fail() << endl; // # 6
cout << in_file.rdbuf(); // # 7
in_file.close(); // # 8
getchar();
}
程序执行结果:(依 #1、#4 设定而不同,两者皆成功时,结果如下)
out_file.is_open()=1
in_file.is_open()=1
in_file.fail()=0
My name is Peipei.
Boy
/* 范例:11-8 */
#include
void main()
{
fstream io_file; // #1.1 只定义变量
char ch;
int start_pos=0; // 开始写入字符串位置
int count=0;
io_file.open("out.txt",ios_base::in|ios_base::out); // #1.2
if(io_file.fail()) // #2 打开文件失败时,fail()==true
{
cerr << "文件打开失败!\nPress any key to Exit...";
getchar();
exit(1);
}
if((io_file.peek())==EOF) // #3 无文件的文件(仅EOF字符)
{
cerr << "文件内无文件!\n";
}
else
{
cout << "文件内源文件如下\n====================\n";
cout << io_file.rdbuf(); // 输出 rdbuf()
count = io_file.tellg(); // #4 取得目前位置
cout << "\n====================\n";
cout << "共 " << count << " 字符\n";
cout << "您要从哪个位置插入字符串?(1-" << count+1 << ")";
cin >> start_pos;
start_pos--; // 由0开始,因此输入值减一
if((start_pos <= count) && (start_pos >=0))
io_file.seekp(start_pos); // #5 移到指定位置
else
io_file.seekp(0);
while(cin.get() != '\n') // 清字符(含'\n')
{
}
}
cout << "输入字符串,此字符串会被插入指定位置"
<< "(覆盖,不清除源文件!)\n";
while((ch=cin.get()) != '\n') // 输入非'\n'字符,则写入io_file
{
io_file << ch;
}
io_file.seekg(0); // #6 字符指针指到头(0)
cout << "全部字符串为:\n" << io_file.rdbuf() << "\n";
count = io_file.tellg(); // 取得总字数
cout << "请输入要输出最后几个字符?(1-" << count << ") ";
cin >> start_pos;
// 输入合理值时,倒退指定格数输出
if((start_pos>=0)||(start_pos<=count))
{
io_file.seekg(count-start_pos);
cerr << io_file.rdbuf();
}
io_file.close();
getchar();
}