C++ Exercises(九)

<<C++ Primer>>第三版P905的这个程序有很多问题想不明白:
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

None.gif #include  < iostream >
None.gif#include 
< fstream >
None.gif
using   namespace  std;
None.gif
None.gif
int  main()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    fstream inOut( 
"D:\\copy.out", ios_base::in|ios_base::app);
InBlock.gif    
int cnt=0;
InBlock.gif    
char ch;
InBlock.gif    inOut.seekg(
0);
InBlock.gif    
InBlock.gif    
while ( inOut.get( ch ) )
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        cout.put( ch );
InBlock.gif        cnt
++;
InBlock.gif        
if ( ch == '\n' )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            streamoff  mark 
= inOut.tellg();// 标记当前位置
InBlock.gif
            inOut << cnt << ' ';
InBlock.gif            inOut.seekg( mark ); 
// 恢复位置
ExpandedSubBlockEnd.gif
        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    inOut.clear();
InBlock.gif    inOut 
<< cnt << endl;
InBlock.gif    cout 
<< "" << cnt << " ]\n";
InBlock.gif    
return 0;
ExpandedBlockEnd.gif}

None.gif

用下面的数据进行测试:

None.gif abcd
None.gifefg
None.gifhi
None.gifj
None.gif


分别保存为

data.txt copy.out, 运行结果:

None.gif abcd
None.gifefg
None.gifhi
None.gif[ 12 ]


后来我想可能是因为不是读的二进制的缘故,所以我改为:

 

None.gif fstream inOut(  " D:\\data.txt " , ios_base:: in | ios_base::app | ios_base::binary);
None.gif
None.gif

分别进行测试后,结果就更奇怪了。

这是data.txt的:

None.gif abcd
None.gifefg
None.gifhi
None.gif[ 15 ]
None.gif


这是copy.out的:

None.gif abcd
None.gifefg
None.gifj
None.gif[ 18 ]

 

这到底是为什么呢?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
“近年来程序设计领域最好的一本书。”——Larry O’Brien, Software Development Times, July 29, 2015 Using a simple computational task (term frequency) to illustrate different programming styles, Exercises in Programming Style helps readers understand the various ways of writing programs and designing systems. It is designed to be used in conjunction with code provided on an online repository. The book complements and explains the raw code in a way that is accessible to anyone who regularly practices the art of programming. The book can also be used in advanced programming courses in computer science and software engineering programs. The book contains 33 different styles for writing the term frequency task. The styles are grouped into nine categories: historical, basic, function composition, objects and object interactions, reflection and metaprogramming, adversity, data-centric, concurrency, and interactivity. The author verbalizes the constraints in each style and explains the example programs. Each chapter first presents the constraints of the style, next shows an example program, and then gives a detailed explanation of the code. Most chapters also have sections focusing on the use of the style in systems design as well as sections describing the historical context in which the programming style emerged. Cristina Videira Lopes,加州大学欧文分校信息学教授,致力于大规模数据和系统的软件工程研究。她是施乐帕洛阿尔托研究中心的创始成员,还研发并维护着一个搜索引擎,为基于OpenSimulator的虚拟世界提供帮助。
“近年来程序设计领域最好的一本书。”——Larry O’Brien, Software Development Times, July 29, 2015 Using a simple computational task (term frequency) to illustrate different programming styles, Exercises in Programming Style helps readers understand the various ways of writing progr ams and designing systems. It is designed to be used in conjunction with code provided on an online repository. The book complements and explains the raw code in a way that is accessible to anyone who regularly practices the art of programming. The book can also be used in advanced programming courses in computer science and software engineering programs. The book contains 33 different styles for writing the term frequency task. The styles are grouped into nine categories: historical, basic, function composition, objects and object interactions, reflection and metaprogramming, adversity, data-centric, concurrency, and interactivity. The author verbalizes the constraints in each style and explains the example programs. Each chapter first presents the constraints of the style, next shows an example program, and then gives a detailed explanation of the code. Most chapters also have sections focusing on the use of the style in systems design as well as sections describing the historical context in which the programming style emerged. Cristina Videira Lopes,加州大学欧文分校信息学教授,致力于大规模数据和系统的软件工程研究。她是施乐帕洛阿尔托研究中心的创始成员,还研发并维护着一个搜索引擎,为基于OpenSimulator的虚拟世界提供帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值