| 编程中国 | 业界新闻 | 技术文章 | 视频教程 | 下载频道 | 程序源码 | 个人空间 | 编程论坛
全能ASP/PHP/ASP.NET主机,支持月付专业 MSSQL 数据库空间,支持月付专业 MySQL 数据库空间,支持月付买域名,送MP3、MP4
高端软件开发 = 年薪十万不是梦赛孚耐:软件保护加密专家身份认证令牌USB KEY买空间,免费送域名(厦门中资源)
共有 284 人关注过本帖
标题:C++中有关文件操作的问题,请高手指点,多谢!
收藏  订阅  推荐  打印 
hnbalp
Rank: 1
等级:新手上路
帖子:5
积分:152
注册:2007-5-22
C++中有关文件操作的问题,请高手指点,多谢!

#include <string>
#include <iostream>
#include <fstream>
#include <cctype>
using namespace std;
void main(){
fstream inout("copyout",
                  fstream::binary ¦ fstream::ate ¦ fstream::in ¦ fstream::out);
if(!inout){
cerr < <"Unable to open the file" < <endl;
//return //EXIT_FAILURE;
}
fstream::pos_type end_mark=inout.tellg();
inout.seekg(0,fstream::beg);
string line;
while(inout.good()&&inout.tellg()!=end_mark&&getline(inout,line)){
fstream::pos_type mark=inout.tellg();
inout.seekp(0,fstream::end);
for(int index=0;index!=line.size();++index)
line[index]=toupper(line[index]);
inout < <line;
if(mark!=end_mark) inout < <" ";
inout.seekg(mark);





}
inout.clear();
inout.seekp(0,fstream::end);
//cin.get();
//cin.get();
  

}
在VC6.0中编译通过,并且可以执行,但是并没有在原有文件的末尾再追加一行,将原来文件中的小写字符转换为大写字符。 请高手指点,多谢!

[ 本帖最后由 hnbalp 于 2008-7-11 00:07 编辑 ]
搜索更多相关主题的帖子: fstream  include  文件  inout  
2008-7-11 00:05
lindayanglong
Rank: 2
等级:注册会员
帖子:83
积分:1036
注册:2008-3-9

我只能看出 在开启文件没有带后缀,应该是copyout.txt
加上cout <<line; 可以在屏幕上看到是转成大写了,但在文件中没有转,我就不知道了


#include <string>
#include <iostream>
#include <fstream>
#include <cctype>
using namespace std;
void main(){
fstream inout("copyout.txt",fstream::binary | fstream::ate | fstream::in | fstream::out);  
                 
if(!inout)
{

    cerr <<"Unable to open the file" <<endl;
    //return //EXIT_FAILURE;
}
fstream::pos_type end_mark=inout.tellg(); //在输入文件中,获得位置标记目前所在的位置
inout.seekg(0,fstream::beg); //在输入文件中,将位置标记从开头算起,移动0偏移量
string line;
while(inout.good()&&inout.tellg()!=end_mark&&getline(inout,line))
{
    fstream::pos_type mark=inout.tellg();
inout.seekp(0,fstream::end);
for(int index=0;index!=line.size();++index)
line[index]=toupper(line[index]);
inout <<line;
cout <<line;


if(mark!=end_mark) inout <<" ";
inout.seekg(mark);






}
inout.clear();
inout.seekp(0,fstream::end);
//cin.get();
//cin.get();
  

}
2008-7-17 20:41
关于我们 | 广告合作 | 编程中国 | 清除Cookies | Archiver | WAP | TOP

编程中国 版权所有,并保留所有权利。鲁ICP备08000592号
Powered by Discuz, Processed in 0.058700 second(s), 9 queries.
Copyright©2004-2008, BCCN.NET, All Rights Reserved