简单的问题

heartgoon2010 2011-08-15 09:05:16
想读一个文件text.txt,如果不为空就输出最后一行,如果为空就向其中写入数字10,代码如下,但是始终写不进去:
#include<iostream>
#include<fstream>
#include<string>
using namespace std;

int main(){
fstream ftm("c:\\test.txt",fstream::in|fstream::out);
if(!ftm){
cout<<"异常"<<endl;
return -1;
}
string line,lastline;
bool hasLine=false;
while(getline(ftm,line)){
hasLine=true;
lastline=line;
}
if(!hasLine){
ftm<<"hello10"<<endl;
cout<<"最多可使用10次"<<endl;
}else{
cout<<lastline<<endl;
} ftm.close();
return 0;
}
...全文
307 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
heartgoon2010 2011-08-27
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 gykgod 的回复:]
完整的应该这样:

C/C++ code

#include<iostream>
#include<fstream>
#include<string>
using namespace std;

int main(){


fstream ftm;
ftm.open("d:\\test.txt",ios::in|ios::out|ios::app);

……
[/Quote]
问一下,为什么要ftm.open("d:\\test.txt",ios::in|ios::out|ios::app);
而不能ftm.open("d:\\test.txt",ios::in|ios::out);
gykgod 2011-08-17
  • 打赏
  • 举报
回复
完整的应该这样:

#include<iostream>
#include<fstream>
#include<string>
using namespace std;

int main(){


fstream ftm;
ftm.open("d:\\test.txt",ios::in|ios::out|ios::app);

if(!ftm){
cout<<"异常"<<endl;
return -1;
}
string line,lastline;
int cnt=1; ////////////add
while((getline(ftm,line))&&(cnt<=2)){ /////////////add
cnt++; ////////////add
lastline=line;
}
//判断一下是否有效,无效则clear下
if (ftm.eof())
{
ftm.clear();
}
ftm.seekp(ios::end);//让流指向文件最后
ftm<<"hello"<<endl;
ftm.flush();
ftm.close();
return 0;
}



gykgod 2011-08-17
  • 打赏
  • 举报
回复
洒家 看了下 稍作改动即可:


#include<iostream>
#include<fstream>
#include<string>
using namespace std;

int main(){


fstream ftm;
ftm.open("d:\\test.txt",ios::in|ios::out|ios::app);

if(!ftm){
cout<<"异常"<<endl;
return -1;
}
string line,lastline;
int cnt=1; ////////////add
while((getline(ftm,line))&&(cnt<=2)){ /////////////add
cnt++; ////////////add
lastline=line;
}

ftm.seekp(ios::end);//让流指向文件最后
ftm<<"hello"<<endl;
ftm.flush();
ftm.close();
return 0;
}

heartgoon2010 2011-08-17
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 gjlzjb 的回复:]
改成下面代码即可

C/C++ code

#include<iostream>
#include<fstream>
#include<string>
using namespace std;

int main(){
fstream ftm("c:\\test.txt",fstream::in);
if(!ftm){
cout<<"异常"<……
[/Quote]
这个想法很自然,不过我还是很想弄明白:既能读又能写的fstream对象为什么现在就用不了呢
gjlzjb 2011-08-17
  • 打赏
  • 举报
回复
改成下面代码即可

#include<iostream>
#include<fstream>
#include<string>
using namespace std;

int main(){
fstream ftm("c:\\test.txt",fstream::in);
if(!ftm){
cout<<"异常"<<endl;
return -1;
}
string line,lastline;
bool hasLine=false;
while(getline(ftm,line)){
hasLine=true;
lastline=line;
}
if(!hasLine){
ftm.close();
fstream ftm2("c:\\test.txt",fstream::out);
ftm2<<"hello10"<<endl;
cout<<"最多可使用10次"<<endl;
}else{
cout<<lastline<<endl;
} ftm.close();
return 0;
}
heartgoon2010 2011-08-17
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 gykgod 的回复:]
C/C++ code

#include<iostream>
#include<fstream>
#include<string>
using namespace std;

int main(){


fstream ftm;
ftm.open("d:\\test.txt",ios::in|ios::out|ios::app);

if(……
[/Quote]


(1)ios::app表示写的时候在文件末尾写,照你的程序看来,读的时候还是从头开始读的。是吧?
(2)假如我只想在test.txt中读第二行数据,然后再在末尾写数据,我这样修改了程序,为什么写不进去呢?即使在写之前ftm.clear();也不行(尽管在文件行数大于2的话这个语句有点多余)。为什么呢?
说到底:fstream究竟是怎么回事,fstream对象不是既能读也能写的吗?为什么就不行呢?windows下不行linux下也不行,那应该是我的程序有问题,请问问题在哪,希望高手帮忙解答!!!
#include<iostream>
#include<fstream>
#include<string>
using namespace std;

int main(){


fstream ftm;
ftm.open("d:\\test.txt",ios::in|ios::out|ios::app);

if(!ftm){
cout<<"异常"<<endl;
return -1;
}
string line,lastline;
int cnt=1; ////////////add
while((getline(ftm,line))&&(cnt<=2)){ /////////////add
cnt++; ////////////add
lastline=line;
}
//这时候文件流到达eof , clear()让文件流恢复有效状态
//ftm.clear(); ////////////del

ftm<<"hello"<<endl;
ftm.flush();
ftm.close();
return 0;
}
gykgod 2011-08-16
  • 打赏
  • 举报
回复

#include<iostream>
#include<fstream>
#include<string>
using namespace std;

int main(){


fstream ftm;
ftm.open("d:\\test.txt",ios::in|ios::out|ios::app);

if(!ftm){
cout<<"异常"<<endl;
return -1;
}
string line,lastline;

while(getline(ftm,line)){

lastline=line;
}
//这时候文件流到达eof , clear()让文件流恢复有效状态
ftm.clear();

ftm<<"hello"<<endl;
ftm.flush();
ftm.close();
return 0;
}
heartgoon2010 2011-08-16
  • 打赏
  • 举报
回复
顶起!!!
heartgoon2010 2011-08-15
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 babilife 的回复:]
C/C++ code


int main(){
fstream ftm("c:\\test.txt",fstream::in|fstream::out);
ofstream f1("c:\\test.txt"); //++++ 这里
if(!ftm){
cout<<"异常"<<endl;
return -1;
……
[/Quote]
fstream对象不是既可以读也可以写它所关联的文件的吗?
至善者善之敌 2011-08-15
  • 打赏
  • 举报
回复


int main(){
fstream ftm("c:\\test.txt",fstream::in|fstream::out);
ofstream f1("c:\\test.txt"); //++++ 这里
if(!ftm){
cout<<"异常"<<endl;
return -1;
}
string line,lastline;
bool hasLine=false;
while(getline(ftm,line)){
hasLine=true;
lastline=line;
}
if(!hasLine){
f1<<"hello10"<<endl;//++++ 这里
cout<<"最多可使用10次"<<endl;
}else{
cout<<lastline<<endl;
} ftm.close();
return 0;
}

heartgoon2010 2011-08-15
  • 打赏
  • 举报
回复
请问问题出在哪,该如何修改,谢了

64,701

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧