注册 登录
编程论坛 C++教室

C++中有关文件操作的问题,请高手指点,多谢!

hnbalp 发布于 2008-07-11 00:05, 725 次点击
#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]]
1 回复
#2
lindayanglong2008-07-17 20:41
我只能看出 在开启文件没有带后缀,应该是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();
  

}
1