首 页 | 新 闻 | Symbian | Android| Windows Mobile | J2ME | 下载中心 | 游戏策划招聘与求职 | 购书指南 | 视频教程
您现在的位置: 开发视界 >> Symbian >> 语言基础 >> 正文
对C++中的文件进行读写操作
作者:孙东风    文章来源:Crazy Wind手机游戏工作室    更新时间:2007-1-9 9:57:57

#include "stdafx.h"

#include<fstream>
#include<iostream>
#include<cmath>

using namespace std;

//从键盘上读取字符的函数
void read_save(){
 char c[80];
 ofstream outfile("f1.dat");       //以输出方工打开文件
 if(!outfile){
  cerr<<"open error!"<<endl;  //注意是用的是cerr
  exit(1);
 }
 cin.getline(c,80);              //从键盘读入一行字符
 for(int i=0;c[i]!=0;i++)        //对字符一个一个的处理,直到遇到'/0'为止
  if(c[i]>=65&&c[i]<=90||c[i]>=97&&c[i]<=122){      //保证输入的字符是字符
   outfile.put(c[i]);                            //将字母字符存入磁盘文件
   cout<<c[i]<<"";
  }
  cout<<endl;
  outfile.close();
}
void creat_data(){
 char ch;
 ifstream infile("f1.dat",ios::in);//以输入的方式打开文件
 if(!infile){
  cerr<<"open error!"<<endl;
  exit(1);
 }
 ofstream outfile("f3.dat");    //定义输出流f3.dat文件
 if(!outfile){
  cerr<<"open error!"<<endl;
  exit(1);
 }
 while(infile.get(ch)){           //当读取字符成功时
  if(ch<=122&&ch>=97)
   ch=ch-32;
  outfile.put(ch);
  cout<<ch;
 }
 cout<<endl;
 infile.close();
 outfile.close();
}

int main(){
 read_save();
 creat_data();
 system("pause");
 return 0;

#include "stdafx.h"

#include<fstream>
#include<iostream>
#include<cmath>

void read_save();
void creat_data();

using namespace std;

int main(){
 read_save();
 creat_data();
 system("pause");
 return 0;
}

//从键盘上读取字符的函数
void read_save(){
 char c[80];
 ofstream outfile("f1.dat");     //以输出方工打开文件
 if(!outfile){
  cerr<<"open error!"<<endl;  //注意是用的是cerr
  exit(1);
 }
 cin.getline(c,80);              //从键盘读入一行字符
 for(int i=0;c[i]!=0;i++)        //对字符一个一个的处理,直到遇到'/0'为止
  if(c[i]>=65&&c[i]<=90||c[i]>=97&&c[i]<=122){      //保证输入的字符是字符
   outfile.put(c[i]);                            //将字母字符存入磁盘文件
   cout<<c[i]<<"";
  }
  cout<<endl;
  outfile.close();
}
void creat_data(){
 char ch;
 ifstream infile("f1.dat",ios::in);//以输入的方式打开文件
 if(!infile){
  cerr<<"open error!"<<endl;
  exit(1);
 }
 ofstream outfile("f3.dat");    //定义输出流f3.dat文件
 if(!outfile){
  cerr<<"open error!"<<endl;
  exit(1);
 }
 while(infile.get(ch)){           //当读取字符成功时
  if(ch<=122&&ch>=97)
   ch=ch-32;
  outfile.put(ch);
  cout<<ch;
 }
 cout<<endl;
 infile.close();
 outfile.close();
}

        C:\Symbian\8.0a\S60_2nd_FP2_SC\Projects\TestArray项目目录下会生成f1.dat,f3.dat文件,并且里面保存有每次程序运行时从console输入的字符.

相关文章:
Symbian中的错误码代号
在Symbian exe 程序中显示图形
Symbian OS程序设计笔记——开发环境的建立
S60 Python 编程指南——对话框和通知信息
在VC里调试标准C语言
C++中的纯虚函数
S60平台: 使用DBMS APIs —DBMS的管理结构和元素(3)
Symbian OS入门指南--错误处理和清理
 

站点地图 | 加入收藏 | 联系站长 | 广告服务 |
QQ:280529124  Tel:0592-8271361 辽ICP备05021703号