• 对于表达式比较长的 for 语句和 if 语句


    对于表达式比较长的 for 语句和 if 语句,为了紧凑起见可以适当地去 掉一些空格,如 for (i=0; i<10; i++)和 if ((a<=b) && (c<=d))。

     1 #include <iostream>
     2 #include<stdio.h>
     3 #include<string.h>
     4 #include<process.h>
     5 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
     6 using namespace std;
     7 int main(int argc, char** argv) {
     8      //声明变量
     9     int i=0;
    10     char p[100];               // 声明输入缓冲区 
    11     FILE *fp1;                 // 声明文件指针变量
    12 
    13     //以写入方式打开d.dat文件
    14     if ((fp1=fopen("d.dat","w"))==NULL)
    15     {
    16        cout<<"
    Could not open the file."<<endl;
    17        cout<<"Exiting program."<<endl;
    18        exit(1);   //结束程序执行
    19     }
    20 
    21     // 写文件操作 
    22     for (i=1;;i++)  {            //无条件循环
    23         cout<<i<<" string:";
    24         cin>>p;                   //从键盘上输入数据
    25         if (stricmp(p,"end")) {      //如果输入的字符串为end,则结束循环
    26             fputs(p,fp1);         //写入文件操作
    27             fputs("
    ",fp1); 
    28         }
    29         else
    30             break;                //退出循环
    31     }
    32  
    33     fclose(fp1);                   //关闭文件
    34 
    35     // 以读方式打开d.dat文件
    36     if ((fp1=fopen("d.dat","r"))==NULL) 
    37     {
    38        cout<<"
    Could not open the file."<<endl;
    39        cout<<"Exiting program."<<endl;
    40        exit(1);   //结束程序执行
    41     }
    42 
    43     // 循环从文件读取字符,并显示
    44     while (fgets(p,100,fp1)!=NULL)  
    45           cout<<p;        
    46      fclose(fp1); //关闭文件
    47     return 0;
    48 }
  • 相关阅读:
    学习:大文件统计与排序
    共享库SidebySide之Windows Shared Assembly
    Bundle是个好东西
    所谓的代码段、数据段
    [design decision] common case vs. rare case
    如何给C++设计一个GC
    玩一把tesseract
    [design decision]让工具依赖于naming convention是个拙劣的做法
    监控域名可用性并自动发信
    调试lua代码
  • 原文地址:https://www.cnblogs.com/borter/p/9413217.html
Copyright © 2020-2023  润新知