标准C++库常见函数
1、
格式化输出:
1) width()函数:可以为接下来的所有显示要素指定默认的宽度。
2) setw()函数:设置数据项实用的宽度
3) fill()函数:当输入宽度非默认宽度时可以设置填充字符的值
4) setiosflags(ios::left)函数:表示输出值的对其方式
5) dec、hex和oct本别表示十进制、十六进制和八进制
6) put:把单个字符写入输入流中
7) get:类似于提取运算符>>,知识空字符被包含到输入中。
#include
<
iostream
>
int main()
{
char line[ 25 ], ch = 0 , * cp;
std::cout << " Type a line terminated by 'x' "
<< std::endl << ' > ' ;
cp = line;
while (ch != ' x ' )
{
std::cin >> ch;
* cp ++ = ch;
}
* cp = ' /0 ' ;
std::cout << ' ' << line;
std::cout << " /n Type another one " << std::endl << ' > ' ;
cp = line;
ch = 0 ;
while (ch != ' x ' )
{
std::cin. get (ch);
* cp ++ = ch;
}
* cp = ' /0 ' ;
std::cout << ' ' << line;
return 0 ;
}
int main()
{
char line[ 25 ], ch = 0 , * cp;
std::cout << " Type a line terminated by 'x' "
<< std::endl << ' > ' ;
cp = line;
while (ch != ' x ' )
{
std::cin >> ch;
* cp ++ = ch;
}
* cp = ' /0 ' ;
std::cout << ' ' << line;
std::cout << " /n Type another one " << std::endl << ' > ' ;
cp = line;
ch = 0 ;
while (ch != ' x ' )
{
std::cin. get (ch);
* cp ++ = ch;
}
* cp = ' /0 ' ;
std::cout << ' ' << line;
return 0 ;
}
2、
1)ofstream类:文件输出
#include
<
fstream
>
#include < iostream >
int main()
{
std::cout << " Opening file " << std::endl;
std::ofstream tfile( " test.dat " , std::ios::app);
std::cout << " Writing to file " << std::endl;
tfile << " , and these are more " ;
return 0 ;
}
#include < iostream >
int main()
{
std::cout << " Opening file " << std::endl;
std::ofstream tfile( " test.dat " , std::ios::app);
std::cout << " Writing to file " << std::endl;
tfile << " , and these are more " ;
return 0 ;
}
标志 | 描述 |
app | 把所有的新数据写到文件尾部 |
ate | 把所有的新数据写到文件尾部,如果程序移动了文件指针,就把数据写到当前位置 |
binary | 以二进制模式而不是文本模式打开文件 |
out | 打开由于输出的文件,删除文件的当前内容。该模式只在没有指定文件打开模式时使用 |
trunc | 打开用于输出的文件,删除文件的当前内容 |
函数 | 描述 |
attach() | 把打开的文件与流关联 |
close() | 在刷新未保存的数据后关闭文件 |
flush() | 刷新流 |
open() | 打开一个文件并把它与流关联 |
put() | 向流中写入一个字符 |
rdbuf() | 返回与流关联的filebuf对象 |
seekp() | 设置流文件指针的位置 |
setmode() | 把流设置成二进制模式或文本模式 |
tellp() | 获取流文件指针的位置 |
write() | 向流中写入一组字节 |
#include
<
iostream
>
#include < string >
#include < fstream >
int main()
{
std::string str("This is a test");
// Create an output stream object.
std::ofstream tfile;
// Associate a file with the stream.
tfile.open("testfile.txt");
// Write a string one character at a time.
for (int x=0; x<14; ++x)
{
std::cout << "File pointer: " << tfile.tellp();
tfile.put(str[x]);
std::cout << " " << str[x] << std::endl;
}
return 0;
}
2)ifstream类:输出类
#include < string >
#include < fstream >
int main()
{
std::string str("This is a test");
// Create an output stream object.
std::ofstream tfile;
// Associate a file with the stream.
tfile.open("testfile.txt");
// Write a string one character at a time.
for (int x=0; x<14; ++x)
{
std::cout << "File pointer: " << tfile.tellp();
tfile.put(str[x]);
std::cout << " " << str[x] << std::endl;
}
return 0;
}
相关函数
eof() | 测试文件是否结束 |
seekg() | 在文件中定位,有beg、cur和end表示起始位置,seekg(5,ios::cur) |
tellg() | 指示在文件中的位置 |
#include
<
fstream
>
#include < iostream >
int main()
{
std::ifstream tfile( " test.dat " );
tfile.seekg( 6 ); // Seek six characters in
while ( ! tfile.eof())
{
char ch;
tfile. get (ch);
if ( ! tfile.eof())
std::cout << ch;
}
return 0 ;
}
3)文件的输入输出
#include < iostream >
int main()
{
std::ifstream tfile( " test.dat " );
tfile.seekg( 6 ); // Seek six characters in
while ( ! tfile.eof())
{
char ch;
tfile. get (ch);
if ( ! tfile.eof())
std::cout << ch;
}
return 0 ;
}
#include
<
fstream
>
#include < cctype >
#include < iostream >
int main()
{
char * fname = " test.dat " ;
// Read the file into an array.
std::ifstream tfile(fname, std::ios:: in |
std::ios:: out | std::ios::binary);
std::ostream ofile(tfile.rdbuf());
char tdata[ 100 ];
int i = 0 ;
while ( ! tfile.eof() && i < sizeof tdata)
tfile. get (tdata[i ++ ]);
// Write the array to the file.
ofile.seekp( 0 , std::ios::end);
ofile << " /r/n " ;
for ( int j = 0 ; j < i - 1 ; j ++ )
ofile.put(static_cast < char > (toupper(tdata[j])));
return 0 ;
}
文件输入
/
输出
#include < cctype >
#include < iostream >
int main()
{
char * fname = " test.dat " ;
// Read the file into an array.
std::ifstream tfile(fname, std::ios:: in |
std::ios:: out | std::ios::binary);
std::ostream ofile(tfile.rdbuf());
char tdata[ 100 ];
int i = 0 ;
while ( ! tfile.eof() && i < sizeof tdata)
tfile. get (tdata[i ++ ]);
// Write the array to the file.
ofile.seekp( 0 , std::ios::end);
ofile << " /r/n " ;
for ( int j = 0 ; j < i - 1 ; j ++ )
ofile.put(static_cast < char > (toupper(tdata[j])));
return 0 ;
}
#include
<
iostream
>
#include < iomanip >
using namespace std;
int main()
{
cout.width( 10 );
cout << setiosflags(ios::left) << oct << 25 << endl;
cout.width( 10 );
cout << setiosflags(ios::right) << hex << 25 << endl;
cout << dec << setfill( ' b ' ) << setw( 10 );
cout << 1024 << " OK " << endl;
cout.put( ' H ' );
return 0 ;
}
#include < iomanip >
using namespace std;
int main()
{
cout.width( 10 );
cout << setiosflags(ios::left) << oct << 25 << endl;
cout.width( 10 );
cout << setiosflags(ios::right) << hex << 25 << endl;
cout << dec << setfill( ' b ' ) << setw( 10 );
cout << 1024 << " OK " << endl;
cout.put( ' H ' );
return 0 ;
}