基本输入读取

#if 0
/*读写文件cin.getline*/
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<process.h>
using namespace std;
main()  {//声明变量FILE *fp1;char str[80];//从键盘上任意输入一个字符串cout<<"Inupt a string:";cin.getline(str,80);//以写入方式打开d.dat文件if ((fp1=fopen("d.dat","w"))==NULL){cout<<"\nCould not open the file."<<endl;cout<<"Exiting program."<<endl;exit(1);   //结束程序执行}// 写"流"文件 fputs(str,fp1); fputs("\n",fp1);fclose(fp1);              //关闭文件// 以读方式打开d.dat文件if ((fp1=fopen("d.dat","r"))==NULL) {cout<<"\nCould not open the file."<<endl;cout<<"Exiting program."<<endl;exit(1);   //结束程序执行}// 循环从"流"文件读取字符,并显示char ch;while ((ch=fgetc(fp1))!=EOF)  cout<<ch;      cout<<endl;fclose(fp1); //关闭文件
}
#endif

cin>>写入

#if 0
/*cin>>*/
#include<iostream.h>
#include <process.h>
#include<stdio.h>
#include<conio.h>
void main(void)  {//变量声明char ch;FILE *fp1;//以写入方式打开d.dat文件 if ((fp1=fopen("d.dat","w"))==NULL)    {cout<<"\nCould not open the file."<<endl;cout<<"Exiting program."<<endl;exit(1);   //结束程序执行}//循环从键盘上读取字符,写入"流"文件cout<<"char:"<<endl;cin>>ch; while (ch!='*') {fputc(ch,fp1);   //将字符写到fp1指向的"流"文件中cin>>ch; }fclose(fp1);  //关闭文件// 以读方式打开d.dat文件if ((fp1=fopen("d.dat","r"))==NULL) {cout<<"\nCould not open the file."<<endl;cout<<"Exiting program."<<endl;exit(1);   //结束程序执行}// 循环从"流"文件读取字符,并显示while ((ch=fgetc(fp1))!=EOF)  cout<<ch<<"  ";       cout<<endl;fclose(fp1); //关闭文件
}
#endif

stricmp()

#if 0
/*stricmp或strcmpi*/
#include<iostream.h>
#include<stdio.h>
#include<string.h>
#include<process.h>
main()  {//声明变量int i=0;char p[100];            // 声明输入缓冲区 FILE *fp1;               // 声明文件指针变量//以写入方式打开d.dat文件if ((fp1=fopen("d.dat","w"))==NULL){cout<<"\nCould not open the file."<<endl;cout<<"Exiting program."<<endl;exit(1);   //结束程序执行}// 写文件操作 for (i=1;;i++)  {            //无条件循环cout<<i<<" string:";cin>>p;                 //从键盘上输入数据if (stricmp(p,"end")) {     //如果输入的字符串为end,则结束循环fputs(p,fp1);         //写入文件操作fputs("\n",fp1); }elsebreak;                //退出循环}fclose(fp1);                //关闭文件// 以读方式打开d.dat文件if ((fp1=fopen("d.dat","r"))==NULL) {cout<<"\nCould not open the file."<<endl;cout<<"Exiting program."<<endl;exit(1);   //结束程序执行}// 循环从文件读取字符,并显示while (fgets(p,100,fp1)!=NULL)  cout<<p;       fclose(fp1); //关闭文件
}
#endif

rand()-putw()

#if 0
/*rand与putw*/
#include<iostream.h>
#include<stdio.h>
#include<string.h>
#include<process.h>
#include<stdlib.h>
#define MAX    10
main()  {//声明变量int i,n;FILE *fp1;               // 声明文件指针变量//以写入方式打开d.dat文件if ((fp1=fopen("d.dat","w"))==NULL){cout<<"\nCould not open the file."<<endl;cout<<"Exiting program."<<endl;exit(1);   //结束程序执行}// 写文件操作 for (i=1;i<=MAX;i++)  {     n=rand();        //产生1个整数随机数putw(n,fp1);//将整型n写入fp1指向的文件(d.dat)cout<<n<<" ";}cout<<endl<<"--------------------"<<endl;fclose(fp1);                 //关闭文件// 以读方式打开d.dat文件if ((fp1=fopen("d.dat","r"))==NULL) {cout<<"\nCould not open the file."<<endl;cout<<"Exiting program."<<endl;exit(1);   //结束程序执行}// 循环从"流"文件读取字符,并显示while ((n=getw(fp1))!=EOF)  cout<<n<<" ";    cout<<endl;fclose(fp1); //关闭文件
}
#endif

结构体、数组-文件

#if 0
/*fprintf-pscanf*/
#include<iostream.h>
#include<stdio.h>
#include<string.h>
#include<process.h>
#include<stdlib.h>
#define MAX    3
main()  {//定义结构类型struct student {int num;char name[10];float grade;};//声明数组和变量student st[3];int i;FILE *fp1;                    // 声明文件指针变量//以写入方式打开d.dat文件if ((fp1=fopen("d.dat","w"))==NULL){cout<<"\nCould not open the file."<<endl;cout<<"Exiting program."<<endl;exit(1);   //结束程序执行}//从键盘上读数据,写入文件cout<<"    num    name    grade"<<endl;for (i=0;i<MAX;i++)  {     cout<<i+1<<" ";cin>>st[i].num;cin>>st[i].name;cin>>st[i].grade;fprintf(fp1,"%d %s %f\n",st[i].num,st[i].name,st[i].grade);}fclose(fp1);                //关闭文件// 以读方式打开d.dat文件if ((fp1=fopen("d.dat","r"))==NULL) {cout<<"\nCould not open the file."<<endl;cout<<"Exiting program."<<endl;exit(1);   //结束程序执行}// 循环从"流"文件读取字符,并显示student t;while ((fscanf(fp1, "%d %s %f",&t.num,t.name,&t.grade))!=EOF)  {cout<<t.num<<" ";     cout<<t.name<<" ";        cout<<t.grade<<endl;        }fclose(fp1); //关闭文件
}
#endif

二进制文件

#if 0
/*二进制文件*/
#include<iostream.h>
#include <process.h>
#include <stdlib.h>
#include <stdio.h>
int main(void)
{ FILE *fpd,*fpw;    // 声明FILE结构指针变量 unsigned char dw;int i=0;//以二进制读方式打开Calc.exe文件if((fpd=fopen("C:\WINDOWS\Calc.exe", "rb"))==NULL) { cout<<"\nCould not open the file."<<endl;cout<<"Exiting program."<<endl;exit(1);   //结束程序执行} // 以二进制写方式打开xc.exe文件 if((fpw=fopen("xc.exe", "wb+"))==NULL){cout<<"\nCould not open the file."<<endl;cout<<"Exiting program."<<endl;exit(1);   //结束程序执行}// 二进制文件读写操作,每次指定读写1个字节while(!feof(fpd)) {   //使用feof()判断文件尾 fread(&dw, 1, 1, fpd);fwrite(&dw, 1, 1, fpw);}// 关闭文件fclose(fpd);fclose(fpw);//执行Calc.exe和Calc.exe文件cout<<"1 Run C:\WINDOWS\Calc.exe"<<endl;system("C:\WINDOWS\Calc.exe");cout<<"-------------------"<<endl;cout<<"2 Run xc.exe!"<<endl;system("xc.exe");
}
#endif

文件定位、偏移量

#if 0
/*文件定位、偏移量*/
#include<iostream.h>
#include <process.h>
#include<stdio.h>
#include<conio.h>
void main(void)
{//声明变量int i;char ch;FILE *fp1;//以写入方式打开d.dat文件 if ((fp1=fopen("d.dat","w"))==NULL)    {cout<<"\nCould not open the file."<<endl;cout<<"Exiting program."<<endl;exit(1);   //结束程序执行}//循环从键盘上读取字符,写入文件cout<<"char:";cin>>ch; while (ch!='*') {fputc(ch,fp1);   //将字符写到fp1指向的"流"文件中cin>>ch; }cout<<"--------------------"<<endl;fclose(fp1);  //关闭文件//以读方式打开d.dat文件if ((fp1=fopen("d.dat","r"))==NULL) {cout<<"\nCould not open the file."<<endl;cout<<"Exiting program."<<endl;exit(1);   //结束程序执行}//循环从文件读取字符,并显示while ((ch=fgetc(fp1))!=EOF)  cout<<ch;       cout<<endl<<"--------------------"<<endl;//以下按倒序方式读取文件中的字符,并显示for (i=-1;;i--) {fseek(fp1,i,2);              //设置文件指针,偏移量为i,相对文件尾if ((ch=fgetc(fp1))!=EOF)cout<<ch;elsebreak;}cout<<endl<<"--------------------"<<endl;//以下读取"流"文件中偶数位置上的字符,并打印long position;for (i=0;;i=i+2) {fseek(fp1,i,0);            //设置文件指针,偏移量为i,相对文件头position=ftell(fp1);//相对指针偏移量if ((ch=fgetc(fp1))==EOF)    //遇到文件尾,则退出,否则打印读取的字符break;     else {cout<<position<<" :"<<ch<<endl;}}cout<<endl;fclose(fp1); //关闭文件
}
#endif

数组写入文件与读取

#if 0
/*数组写入文件与读取*/
#include<iostream.h>
#include<stdio.h>
#include<process.h>
#include<stdlib.h>
#define len 5//显示数组的数据
void show_array(double x[],int size) {for(int i=0;i<size;i++)cout<<x[i]<<" ";cout<<endl;
}//main函数测试数组数据的文件读写
int main(void)
{ //声明变量FILE *fp;    // 声明FILE结构指针变量 int i;double a[len]={1.0,1.2,1.4,1.6,1.8};//显示数组a的数据cout<<"a:";show_array(a,len);//打开d.dat文件if ((fp=fopen("d.dat","wb+"))==NULL){cout<<"\nCould not open the file."<<endl;cout<<"Exiting program."<<endl;exit(1);   //结束程序执行}//以单个元素对数组进行文件读操作for(i=0;i<len;i++) { fwrite(&a[i], sizeof(double), 1, fp);}rewind(fp);   //恢复读写指针的位置//以单个元素对数组进行文件读操作double b[len];for(i=0;i<len;i++) { if (!feof(fp))    //使用feof()判断文件尾 fread(&b[i], sizeof(double), 1, fp);elsebreak;}cout<<"b:";show_array(b,len);//显示数组b的数据fclose(fp); // 关闭文件//打开d1.dat文件if ((fp=fopen("d1.dat","wb+"))==NULL){cout<<"\nCould not open the file."<<endl;cout<<"Exiting program."<<endl;exit(1);   //结束程序执行}//将数组当成数据块写入文件fwrite(&a, sizeof(double), len, fp);rewind(fp);   //恢复读写指针的位置//将数组当成数据块从文件中读取double c[len];if (!feof(fp))    //使用feof()判断文件尾 fread(&c, sizeof(double),len,fp);cout<<"c:";show_array(c,len);  //显示数组c的数据fclose(fp); // 关闭文件
}
#endif

struct写入与读取

#if 0
/*struct写入文件与读取*/
#include<iostream.h>
#include<stdio.h>
#include<process.h>
#include<stdlib.h>
#define len 5
//定义结构类型
struct student {int  num;char name[20];float grade;
};//显示student结构数据
void show_str(student a,char *name) {cout<<name<<":"<<endl;cout<<a.num<<" "<<a.name<<" "<<a.grade;cout<<endl;
}//main函数测试结构数据的文件读写
int main(void)
{ //声明变量FILE *fp;    //声明FILE结构指针变量 student st={1001,"TianRui",85.5};//显示st结构数据show_str(st,"st");//打开d.dat文件if ((fp=fopen("d.dat","wb+"))==NULL){cout<<"\nCould not open the file."<<endl;cout<<"Exiting program."<<endl;exit(1);   //结束程序执行}//用fprintf()函数写结构数据到文件fprintf(fp,"%d %s %f",st.num,st.name,st.grade);rewind(fp);   //恢复读写指针的位置//用fscanf()函数读文件中的数据赋值给结构并显示student temp;fscanf(fp, "%d %s %f",&temp.num,temp.name,&temp.grade);show_str(temp,"temp");cout<<"-----------------------"<<endl;fclose(fp); // 关闭文件//将结构数据当成数据块进行读写if ((fp=fopen("dx.dat","wb+"))==NULL)  //打开dx.dat文件{cout<<"\nCould not open the file."<<endl;cout<<"Exiting program."<<endl;exit(1);   //结束程序执行}//声明结构数组并初始化int i;student starr[3]={{101,"XieCh",92},{102,"Li",85},{103,"TangXu",97}};//显示结构数组for(i=0;i<3;i++) show_str(starr[i],"starr");//将结构数组当成数据块写入文件fwrite(starr, sizeof(student), 3, fp);rewind(fp);   //恢复读写指针的位置//按数据块从文件中读取数据赋值给结构数组student temp_arr[3];if (!feof(fp))    //使用feof()判断文件尾 fread(temp_arr, sizeof(student),3,fp);for(i=0;i<3;i++) show_str(temp_arr[i],"temp_arr");fclose(fp); // 关闭文件
}
#endif

stdin-stdout

#if 0
/*stdin-stdout*/
#include<stdio.h>
#include<stdlib.h>
#include<iostream.h>
int main(void)
{ //声明变量char ch;char str[20];int n;float x;//用stdin从键盘上输入数据fprintf(stdout,"ch str\n");fscanf(stdin,"%c %s",&ch,str);fprintf(stdout,"n    x \n");fscanf(stdin,"%d  %f",&n,&x);cout<<"----------------"<<endl;//输出显示fprintf(stdout,"ch=%c str=%s",ch,str);fprintf(stdout,"\nn=%d x=%f",n,x);cout<<endl;
}
#endif

ferror-perror-clearerr

#if 0
/*ferror-perror-clearerr*/
#include <stdio.h>
void main( void )
{int c;/* Create an error by writing to standard input. */putc( 'A', stdin );if( ferror( stdin ) ){perror( "Write error" );clearerr( stdin );}/* See if read causes an error. */printf( "Will input cause an error? " );c = getc( stdin );if( ferror( stdin ) ){perror( "Read error" );clearerr( stdin );}
}
#endif

数学三角函数

#if 0
/*三角函数*/
#include<iostream.h>
#include<math.h>   //此预处理指令不可少(数学运算)
const double angle=3.1415926/180;
main() {cout<<"x\tsin(x)"<<endl;for (int i=0;i<=180;i=i+30) cout<<i<<"\t"<<sin(i*angle)<<endl;
}
#endif

define

#if 0
/*define*/
#include<iostream.h>
//宏替换预处理指令
#define  YES        1
#define  PI         3.1415926
#define  DEG        PI/180
#define  MESG       "This is a string."//以下是主程序
main()  {//以下各语句使用了宏替换 cout<<"YES="<<YES<<endl;if (YES) cout<<"PI="<<PI<<endl;cout<<"DEG="<<DEG<<endl;cout<<MESG<<endl;
}
#endif

带参数define

#if 0
/*带参数define*/
#include<iostream.h>
//带参数宏替换的预处理指令
#define  PRINT(k)   cout<<(k)<<endl;
#define  MAX(a,b)   ((a)>(b) ? (a):(b))
main()
{int i=3,j=2;//MAX(a,b)宏替换的使用 cout<<"MAX(10,12)="<<MAX(10,12)<<endl;cout<<"MAX(i,j)="<<MAX(i,j)<<endl;cout<<"MAX(2*i,j+3)="<<MAX(2*i,j+3)<<endl;//PRINT(k)宏替换的使用PRINT(5);PRINT(MAX(7,i*j));
}
#endif

#if--#endif

#if 0
/*#if-#endif,在实例(1)已大致描述*/
#include<iostream.h>
#define   PI   3.1416
main()
{int i=100;
#if 1cout<<"i="<<i<<endl;
#endif#ifdef PIcout<<"1  PI="<<PI<<endl;
#endif#ifndef PIcout<<"2  PI="<<PI<<endl;   //此语句不被编译执行,类似于注释
#endif
}
#endif

C++基础实例-文件Io等(5)相关推荐

  1. Linux基础(6)--文件IO操作

    文件IO操作 1. open打开操作 2. close关闭操作 3. creat创建操作 4. write写操作 5. read读操作 Linux下一切皆文件,所以文件IO是很重要的也是很基础的操作. ...

  2. Python基础知识点拾遗---文件IO(os、filecmp、shutil库)、sys库、序列化(pickle)、类

    Python基础知识点拾遗 文件IO 文本 目录 os 获取文件的创建.修改及最近访问时间 获取当前文件的大小 获取当前的登录用户名称 获取当前的cpu核数 调用操作系统底层的random生成器 sh ...

  3. (理论篇)从基础文件IO说起虚拟内存,内存文件映射,零拷贝

    为了快速构建项目,使用高性能框架是我的职责,但若不去深究底层的细节会让我失去对技术的热爱. 探究的过程是痛苦并激动的,痛苦在于完全理解甚至要十天半月甚至没有机会去应用,激动在于技术的相同性,新的框架不 ...

  4. 零基础学Python(第十八章 文件IO流操作)

    本套学习内容共计[22]个章节,每个章节都会有对应的从0-1的学习过程详细讲解,希望可以给更多的人提供帮助. 开发环境:[Win10] 开发工具:[Visual Studio 2019] 本章内容为: ...

  5. (代码篇)从基础文件IO说起虚拟内存,内存文件映射,零拷贝

    上一篇讲解了基础文件IO的理论发展,这里结合java看看各项理论的具体实现. 传统IO-intsmaze 传统文件IO操作的基础代码如下: FileInputStream in = new FileI ...

  6. Linux Ubuntu下的文件IO介绍及实例应用(C语言)

           大家好,我是练习编程时长两年半的个人练习生昆工第一ikun,昨天咋们说了标准IO,今天咋们来分享文件IO,以及一个很有趣的实例,给图片加密,使其无法打开.话不多说,上代码. 一.文件IO ...

  7. java io教学文件_通过代码实例跟我学Java语言程序设计及应用技术——Java文件IO技术及应用相关的教学示例(第3部分)...

    1.1Java文件IO技术及应用相关的教学示例(第3部分) 1.1.1文本字符流IO操作 1.字符流Reader/Writer类 提供的对字符流处理的类,它们为抽象类.其子类 (1)InputStre ...

  8. Python基础语法全体系 | 文件IO与常用文件操作模块

    <Python基础语法全体系>系列博文第六篇,本篇博文将讲解Python的文件IO操作,包括文件的打开.读取和写入.本文整理自疯狂python编程. 文章目录 使用pathlib操作目录 ...

  9. linux 文件io实例代码,linux 文件IO(示例代码)

    1.文件描述符 (1)文件描述符的本质是一个数字,这个数字本质上是进程表中文件描述符表的一个表项,进程通过文件描述符作为index去索引查表得到文件表指针,再间接访问得到这个文件对应的文件表. (2) ...

最新文章

  1. python 字符串前面加u,r,b,f的含义
  2. 奢侈品级别的广告位,到底要不要继续砸钱?
  3. 剑指offer(19)顺时针打印矩阵
  4. linux获取cpu核数(线程数)
  5. 2020 前端开源领域技术展望
  6. (转载)关于My97 datepicker与Angular ng-model绑定问题解决。
  7. python 零代码快速开发平台_现在低代码开发平台和零代码平台区别是什么?
  8. 打开多个界面_如何创建用户界面
  9. JSP和Servlet互相传输数据的过程中产生的乱码问题及解决方案(没有使用AJAX的情况)...
  10. 换行与回车(\r \n)的起源以及在编制语言中的使用
  11. HTML - 元素/标签和属性基础
  12. Smith Builder ERP代码生成器(开源,提供源码下载)
  13. 空字符串(“”)和null的区别
  14. 关于Asp.Net中的返回的操作
  15. MATLAB画图线性,颜色和数据点
  16. zone在linux中的含义,linux-日常运维-firewalld的9个zone
  17. 台式计算机如何上无线网络,台式电脑如何实现无线上网
  18. 《信息系统布线技术》实验报告之交叉线制作
  19. Pale Transformer:新视觉ViT主干
  20. 学校心理管理/预测系统

热门文章

  1. 【变量创建】CFPS应用及C刊变量复盘STATA实战1
  2. Linux下配置C语言编程环境
  3. 北斗导航卫星公开服务信号频率
  4. 【Node.js】 基础语法
  5. 适用于火车头7.6的翻译插件-亲测10000篇文章稳定不报错
  6. AS608 指纹模块驱动代码
  7. 关于程序员如何创造财富的35条建议
  8. JavaEE - Tomcat和HTTP协议
  9. 陈力:传智播客古代 珍宝币 泡泡龙游戏开发第35讲:PHP 抽象类与接口
  10. Python程序设计快速入门01