| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 725 人关注过本帖
标题:C++中有关文件操作的问题,请高手指点,多谢!
只看楼主 加入收藏
hnbalp
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2007-5-22
收藏
 问题点数:0 回复次数:1 
C++中有关文件操作的问题,请高手指点,多谢!
#include <string>
#include <iostream>
#include <fstream>
#include <cctype>
using namespace std;
void main(){
fstream inout("copyout",
                  fstream::binary &brvbar; fstream::ate &brvbar; fstream::in &brvbar; 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中编译通过,并且可以执行,但是并没有在原有文件的末尾再追加一行,将原来文件中的小写字符转换为大写字符。 请高手指点,多谢!

[[it] 本帖最后由 hnbalp 于 2008-7-11 00:07 编辑 [/it]]
搜索更多相关主题的帖子: fstream include 文件 inout 
2008-07-11 00:05
lindayanglong
Rank: 1
等 级:新手上路
帖 子:86
专家分:0
注 册:2008-3-9
收藏
得分:0 
我只能看出 在开启文件没有带后缀,应该是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-07-17 20:41
快速回复:C++中有关文件操作的问题,请高手指点,多谢!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019048 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved