C++常用I/O流技术
Posted 0x16
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++常用I/O流技术相关的知识,希望对你有一定的参考价值。
//
格式控制
#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下
#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下
以上是关于C++常用I/O流技术的主要内容,如果未能解决你的问题,请参考以下文章