C++常用I/O流技术

本文介绍了C++中如何使用格式控制进行数据输出,包括左对齐、右对齐、精度控制等,并展示了如何读写文件,包括单个字符和结构体对象的文件操作。
//格式控制

#include
<iostream>
//format manipulator.
#include<iomanip>
//
#include<cstdlib>
using namespace std;
int main()
{   
//Function Object.
 cout << setw(20<< "hello" << endl;
 cout 
<< left << setw(10<< "hello" << endl;
 cout 
<< setprecision(4<< 2008.852 << endl;
 cout 
<< right << setw(20<< setfill("*"<< "oh,mygod" << endl;
 
 cout 
<< scientific << uppercase << 300.89 << endl;
 cout 
<< scientific << nouppercase << 300.89 << endl;
 cout 
<< fixed << 300.89 << endl;
 
 cout 
<< showbase << oct << 9 << endl;
 cout 
<< showbase << dec << showpos << 100 << endl;
 cout 
<< noshowpos << 100 << endl;
 cout 
<< hex << 100 << endl;
 
 cout 
<< boolalpha << true << endl;
 cout 
<< noboolalpha << true << endl;
 
 
//show weight
 
//cout << hex << 100 << endl;
 cout << showbase << 458 << endl;
 
//解除设置的格式 
 cout.unsetf(ios::hex);
 cout 
<< showbase << 458 << endl;
 
 
//show point
 cout << showpoint << 152 << endl;
 
//
 system("pause");
 
return 0;
}
 

//多个字符的读写

#include
<cstdlib>
#include
<iostream>
#include
<fstream>
using namespace std;
//
typedef struct {
 
char name[10];
 
int age;
}Student; 
//
int main()
{
 Student s;
 strcpy(s.name, 
"rushgk");
 s.age 
= 23;
 
//
 ofstream out("c:/Student.txt",ios::out);
 
//write
 out.write(reinterpret_cast<char*>(&s), sizeof(Student));
 
//
 out.close();
 
//read
 ifstream in("c:/Student.txt", ios::in);
 
//
 in.read(reinterpret_cast<char*>(&s), sizeof(Student));
 
//
 cout << s.name << " " << s.age << endl;
 system(
"pause");
 
return 0;
}
 
//单个字符的读写

//读出get() 写入 put()
#include<cstdlib>
#include
<iostream>
#include
<fstream>
using namespace std;

int main()
{
 ifstream testFile;
 testFile.open(
"c:/wFileTest.txt", ios::in | ios::binary);
 
if(!testFile)
 {
  cout 
<< "打开文件失败!" << endl; 
  system(
"pause");
  
return 1;
 }
 
char c;
 
//read out by character
 while(!testFile.eof())
 {
  c 
= testFile.get();
  cout 
<< c;
 }
 testFile.close();
 system(
"pause");
 
return 0;
}
//写入到文件

#include
<iostream>
#include
<fstream>
using namespace std;
int main()
{
 
 ofstream wFile;
 
//ios::app (append from...)
 wFile.open("c:/wFileTest.txt", ios::out  | ios::app);
 
//ios::trunc (cover original before writing data...)
// wFile.open("c:/wFileTest.txt", ios::out | ios::trunc);
 if(!wFile)
 {
  cout 
<< "file open failure " << endl;
  
return 1;
 }
 wFile 
<< "apple" << " " <<"苹果" << endl;
 wFile 
<< "water" << " " <<"" << endl;
 wFile 
<< "fuck" << " " << "****" << endl;
 wFile.close(); 
 
 
return 0;
}
//读出到文件

#include
<iostream>
#include
<fstream>
#include
<cstdlib>
using namespace std;

int main()
{
 ifstream rFile;
 rFile.open(
"c:/wFileTest.txt", ios::in);
 
if(!rFile)
 {
  cout 
<< " open file failure " << endl;
  system(
"pause");
  
return 1;
 }
 
char one[10], two[10];
 
//Read fILE
 /** Key words: whiteSpace
  *          overloaded >> and <<
  *
*/  
 rFile 
>> one >> two;
 cout 
<< one << " " << two << endl;
 
//Auto
 rFile >> one >> two;
 cout 
<< one << " " << two << endl;
 
//Auto
 rFile >> one >> two; 
 cout 
<< one << " " << two << endl;
 rFile.close();
 system(
"pause");
 
return 0;
}


//还有两个函数seekg() & seekp() 用于任意制定文件指针的位置然后读写,google下




 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值