| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1209 人关注过本帖
标题:关于向文件尾添加内容,添加后不能close!
只看楼主 加入收藏
lijianbest
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2006-4-6
收藏
 问题点数:0 回复次数:2 
关于向文件尾添加内容,添加后不能close!


//尚未调试通过

//向文件尾添加内容
#include <fstream>
#include <iostream>
using namespace std;

int main()
{
char fileName[80];
char buffer[255];
char ch;

cout << "Please re-enter the file name:";
cin >> fileName;

ifstream fin;
fin.open(fileName);
if(fin)
{
cout << "Current file contents:\n";
while(fin.get(ch))
cout << ch;
cout << "\n****End of file contents***\n";
}
fin.close();

cout << "\nOpening " << fileName << " in append mode...\n";

ofstream fout;
fout.open(fileName,ios::app);
if(!fout)
{
cout << "Unable to open " << fileName << " for appending.\n";
return (1);
}
cout << "\nEnter text for the file: ";
cin.ignore(1,'\n');
cin.getline(buffer,255);
fout << buffer << "\n";
fout.close();

// { //本来不应当加这个大括号的,可是不加不能正常运行,没办法,原因不明。
// int TempIndex=0;
// while (fout && TempIndex<=10)
// {
// fout.close();
// TempIndex++;
// }
// if (TempIndex>=10)
// {
// cout << "Unable to close " << fileName << " after appended!";
// return (1);
// }
//
// }

fin.open(fileName);


// { //本来不应当加这个大括号的,可是不加不能正常运行,没办法,原因不明。
// int TempIndex=0;
// const int MaxLoop=100;
// while (!fin && TempIndex<=MaxLoop)
// {
// fin.open(fileName);
// TempIndex++;
// }
// cout << TempIndex;
// if (TempIndex>=MaxLoop)
// {
// cout << "Unable to open " << fileName << " for reading.\n";
// return (1);
// }
//
// }

if(!fin)
{
cout << "Unable to open " << fileName << " for reading.\n";
return (1);
}
cout << "\nHere's the contents of the file:\n";
while(fin.get(ch))
cout << ch;
cout << "\n***End of file contents***\n";
fin.close();
return 0;
}

搜索更多相关主题的帖子: close 文件 
2006-04-06 15:34
woodhead
Rank: 3Rank: 3
等 级:新手上路
威 望:9
帖 子:1124
专家分:0
注 册:2005-7-18
收藏
得分:0 
以下是引用lijianbest在2006-4-6 15:34:00的发言:


//尚未调试通过

//向文件尾添加内容
#include <fstream>
#include <iostream>
using namespace std;

int main()
{
char fileName[80];
char buffer[255];
char ch;

cout << "Please re-enter the file name:";
cin >> fileName;

ifstream fin;
fin.open(fileName);
if(fin)
{
cout << "Current file contents:\n";
while(fin.get(ch))
cout << ch;
cout << "\n****End of file contents***\n";
}
fin.close();

cout << "\nOpening " << fileName << " in append mode...\n";

ofstream fout;
fout.open(fileName,ios::app);
if(!fout)
{
cout << "Unable to open " << fileName << " for appending.\n";
return (1);
}
cout << "\nEnter text for the file: ";
cin.ignore(1,'\n');
cin.getline(buffer,255);
fout << buffer << "\n";
fout.close();

fin.clear(); //try it

fin.open(fileName);

if(!fin)
{
cout << "Unable to open " << fileName << " for reading.\n";
return (1);
}
cout << "\nHere's the contents of the file:\n";
while(fin.get(ch))
cout << ch;
cout << "\n***End of file contents***\n";
fin.close();
return 0;
}


2006-04-06 17:54
lijianbest
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2006-4-6
收藏
得分:0 

晕,上次fin读到文件末尾时,fail了,谢了!

2006-04-06 18:56
快速回复:关于向文件尾添加内容,添加后不能close!
数据加载中...
 
   



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

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