标准C++库常见函数

标准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 ;
}

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 ;
}
标志描述
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类:输出类
      相关函数
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  < 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  < 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 ;

}

原帖地址:http://www.cppblog.com/The-Moment/archive/2009/06/22/88250.html

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值