⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 p1-88.cpp

📁 C++应用编程200例--------专为广大学习和使用C++语言的读者设计的
💻 CPP
字号:
#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); 
        }
        else
            break;                //退出循环
    }
 
    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); //关闭文件
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -