C++基础实例-文件Io等(5)
程序员文章站
2022-04-15 11:07:08
基本输入读取
#if 0
/*读写文件cin.getline*/
#include
#include
#include
#include
using namespace std;...
基本输入读取
#if 0 /*读写文件cin.getline*/ #include #include #include #include;>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."<cin>>写入 #if 0 /*cin>>*/ #include #include;>;>#include #include void main(void) { //变量声明 char ch; file *fp1; //以写入方式打开d.dat文件 if ((fp1=fopen("d.dat","w"))==null) { cout<<"\ncould not open the file."<>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."<stricmp() #if 0 /*stricmp或strcmpi*/ #include #include #include #include;>;>main() { //声明变量 int i=0; char p[100]; // 声明输入缓冲区 file *fp1; // 声明文件指针变量 //以写入方式打开d.dat文件 if ((fp1=fopen("d.dat","w"))==null) { cout<<"\ncould not open the file."<>p; //从键盘上输入数据 if (stricmp(p,"end")) { //如果输入的字符串为end,则结束循环 fputs(p,fp1); //写入文件操作 fputs("\n",fp1); } else break; //退出循环 } fclose(fp1); //关闭文件 // 以读方式打开d.dat文件 if ((fp1=fopen("d.dat","r"))==null) { cout<<"\ncould not open the file."<rand()-putw() #if 0 /*rand与putw*/ #include #include #include #include;>#include #define max 10 main() { //声明变量 int i,n; file *fp1; // 声明文件指针变量 //以写入方式打开d.dat文件 if ((fp1=fopen("d.dat","w"))==null) { cout<<"\ncould not open the file."<结构体、数组-文件 #if 0 /*fprintf-pscanf*/ #include #include #include #include;>;>#include #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."<>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."<二进制文件 #if 0 /*二进制文件*/ #include #include;>#include #include 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."<文件定位、偏移量 #if 0 /*文件定位、偏移量*/ #include #include;>;>#include #include void main(void) { //声明变量 int i; char ch; file *fp1; //以写入方式打开d.dat文件 if ((fp1=fopen("d.dat","w"))==null) { cout<<"\ncould not open the file."<>ch; while (ch!='*') { fputc(ch,fp1); //将字符写到fp1指向的"流"文件中 cin>>ch; } cout<<"--------------------"<数组写入文件与读取 #if 0 /*数组写入文件与读取*/ #include #include #include;i++)>#include #define len 5 //显示数组的数据 void show_array(double x[],int size) { for(int i=0;istruct写入与读取 #if 0 /*struct写入文件与读取*/ #include #include #include#include #define len 5 //定义结构类型 struct student { int num; char name[20]; float grade; }; //显示student结构数据 void show_str(student a,char *name) { cout<stdin-stdout #if 0 /*stdin-stdout*/ #include #include #include 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<<"----------------"<ferror-perror-clearerr;>#if 0 /*ferror-perror-clearerr*/ #include 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 #include //此预处理指令不可少(数学运算) const double angle=3.1415926/180; main() { cout<<"x\tsin(x)"<define;>#if 0 /*define*/ #include //宏替换预处理指令 #define yes 1 #define pi 3.1415926 #define deg pi/180 #define mesg "this is a string." //以下是主程序 main() { //以下各语句使用了宏替换 cout<<"yes="<带参数define ;>;>#if 0 /*带参数define*/ #include //带参数宏替换的预处理指令 #define print(k) cout<<(k)<(b) ? (a):(b)) main() { int i=3,j=2; //max(a,b)宏替换的使用 cout<<"max(10,12)="<#if--#endif#if 0 /*#if-#endif,在实例(1)已大致描述*/ #include #define pi 3.1416 main() { int i=100; #if 1 cout<<"i="<;> ;>;> ;>;>