c++——文件的输入输出

基本知识

在这里插入图片描述

在这里插入图片描述

打开方式

在这里插入图片描述

例子

#include<iostream>
#include<fstream>
using namespace std;
void main()
{
   //声明文件流对象
   ofstream  outFile("outFile.txt",ios::out);
   if (!outFile)
      //使用错误流对象输出错误信息
      cerr<<"Open file or create file error."<<endl;
   else
      //输出数据到与对象outFile 关联的文件中
      outFile << "This is a test file.";
   
}

#include<fstream>
void main()
{
   //声明文件流对象
   ofstream  outFile;
   outFile.open ("outFile.txt",ios::out);
   if (!outFile)
      //使用错误流对象输出错误信息
      cerr<<"Open file or create file error."<<endl;
   else
   {//输出数据到与对象outFile 关联的文件中
      outFile << "This is a test file.";
	  outFile.close ();
	}
   
}

#include<fstream.h>
void main()
{//往文本文件写数据
   ofstream outfile1("myfile1.txt");
   outfile1<<"Hello!...CHINA LIAONING"<<endl;
   outfile1.close ();
//追加数据
   outfile1.open ("myfile1.txt",ios::app);
   int x=1212,y=6868;
   outfile1<<x<<" "<<y<<endl;
   outfile1.close ();
//从文本文件读数据并显示在屏幕上
   char str1[80],str2[80];
   int x2,y2;
   ifstream infile1("myfile1.txt");
   infile1>>str1>>str2;
   infile1>>x2>>y2;
   infile1.close ();
   cout<<"str1="<<str1<<endl;
   cout<<"str2="<<str2<<endl;
   cout<<"x2="<<x2<<endl;
   cout<<"y2="<<y2<<endl;
}

输入一个字符串写入文件中并统计字符的个数
#include<fstream.h>
void main()
{
   char str[80];
   cout<<"Input string :"<<endl;
   cin.getline (str,80,'\n');
   ofstream fout("ft.txt");
   int i=0;
   while(str[i])
	   fout.put (str[i++]);
   cout<<"len="<<i<<endl;
   fout.close ();
  
}

从文件中读出所写字符串并统计字符的个数

void main()
{
   ifstream fin("ft.txt");
   char ch;
   int i=0;
   fin.get (ch);
   while(!fin.eof ())
   {
	   cout<<ch;
	   i++;
	   fin.get (ch);	      
   }
   cout<<endl<<"len="<<i<<endl;
   fin.close ();
  
}

#include<fstream.h>
#include<string.h>
struct stu
{
	char name[20];
	int age;
	double score;
};
void main()
{
   char str[20]="Hello world!";
   stu ss={"zhang san",22,93.5};
   cout<<"WRITE to 'fb.bin'"<<endl;
   ofstream fout("fb.bin",ios::binary);
   int Len=strlen(str);
   fout.write ((char *)(&Len),sizeof(int));
   fout.write (str,Len);
   fout.write ((char *)(&ss),sizeof(ss));
   fout.close ();
   cout<<"-------------------------"<<endl;
   cout<<"READ from 'fb.bin'"<<endl;
   char str2[80];
   ifstream fin("fb.bin",ios::binary);
   fin.read ((char *)(&Len),sizeof(int));
   fin.read (str2,Len);
   str2[Len]='\0';
   fin.read  ((char *)(&ss),sizeof(ss));
   cout<<"Len="<<Len<<endl;
   cout<<"str2="<<str2<<endl;
   cout<<"ss="<<ss.name <<","<<ss.age <<","<<ss.score <<endl;
   fin.close ();
  
}

#include<fstream.h>
#include<string.h>
struct stu
{
	char name[20];
	int age;
	double score;
};
void main()
{
   int n;
   stu ss;
   cout<<"n=?";
   cin>>n;
   ofstream fout("f01.bin",ios::binary);
   for(int i=0;i<n;i++)
   {
	   cout<<"name,age,score=?";
	   cin>>ss.name >>ss.age >>ss.score ;
       fout.write ((char *)(&ss),sizeof(ss));
   }   
   fout.close ();
   cout<<"-------------------------"<<endl;
   ifstream fin("f01.bin",ios::binary);
   double ave=0;
   int n1=0;
   fin.read  ((char *)(&ss),sizeof(ss));
   while(!fin.eof ())
   {
	   ave+=ss.score ;
	   n1++;
	   fin.read  ((char *)(&ss),sizeof(ss));
   }   
   fin.close ();
   cout<<"n1="<<n1<<"  ave="<<ave/n1<<endl;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值