1. 程式人生 > >對於表達式比較長的 for 語句和 if 語句

對於表達式比較長的 for 語句和 if 語句

input you ogr 寫文件 clas exit roc 數據 緩沖區

對於表達式比較長的 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<<"\nCould 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("\n",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<<"\nCould 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 }

對於表達式比較長的 for 語句和 if 語句