C++文件练习
来源:互联网 发布:淘宝店图片轮播素材 编辑:程序博客网 时间:2024/06/15 16:16
(1)
//读入文件中的数,将其输出,并且找出最大的,再写入到文件中去
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
fstream file;
int x=0,max=0;
file.open("c:\\a.txt",ios::in|ios::out); //以读写方式打开文件,默认也是这种方法
if(file.fail())
{
cout<<"file open fail!"<<endl;
}
else{
cout<<"the file informantion is :";
while(file>>x)
{
cout<<x<<" ";
if(x>max) max=x;
}
/*好像没有效果
cout<<endl;
cout<<file.tellp()<<endl;
file.seekg(ios_base::beg); //重新定位到文件开头
if(file.eof())
{
cout<<"file is end!"<<endl;
}
*/
cout<<endl;
cout<<"the file max is: "<<max<<endl;
}
file.close();
return 0;
}
(2)
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
fstream file;
ifstream ifile;
int count=0,icount=0;
string str,istr;
file.open("c:\\a.txt",ios::in|ios::out); //以读写方式打开文件,默认也是这种方法
if(file.fail())
{
cout<<"file open fail!"<<endl;
}
else{
file.write((char *)&count,sizeof(int));
cout<<"-- 请输入收件人邮箱 --"<<endl;
while(cin>>str&& str!="-1")
{
file<<str<<' ';
count++;
}
long pos=file.tellp();
file.seekp(ios_base::beg);
file.write((char *)&count,sizeof(int));
file.seekp(pos);
cout<<"--- 请输入发件人邮箱 ----"<<endl;
if(cin>>str)
{
file<<str;
}
}
file.close();
ifile.open("c:\\a.txt");
if(ifile.fail())
{
cout<<"file open fail!"<<endl;
}
else{
ifile.read((char *)&icount,sizeof(int));
cout<<"-- 收件人邮箱 --"<<endl;
for(int i=0;i<icount;i++)
{
ifile>>istr;
cout<<istr<<endl;
}
cout<<"--- 发件人邮箱 ----"<<endl;
ifile>>istr;
cout<<istr<<endl;
}
ifile.close();
return 0;
}
- 【c基础练习】文件练习
- 练习C语言文件操作
- C语言练习 文件操作
- c语言之文件操作练习
- C练习
- C练习
- C 练习
- c练习
- C练习
- C练习
- c 练习
- 文件练习
- 文件练习
- c语言文件操作练习--一个简单的字典查找
- C++(笔记)文件操作练习(流类库与输入输出)
- 将c文件注释进行转化的练习小项目
- c语言练习1
- c练习1
- 中兴手机权限开启方法3
- 关联式容器映射map练习
- 四、servlet 分页加载技术
- SPOJ - Horace and his primes(素数+二分查找)
- Error: Error parsing D:\new_android\Android SDK\system-images\android-25\android-wear\armeabi-v7a\de
- C++文件练习
- c#中,类的重写中,new和override的区别
- GIS就业参考系列之方向篇——条条大道通罗马
- ffmpeg 不能发送60帧视频问题分析
- 项目实践---字典
- 在使用hibernate进行数据操作时出现错误
- 深度学习初步了解
- 备忘录模式(设计模式_20)
- C#中Linq查询基本操作使用实例