代码行最大长度宜控制在 70 至 80 个字符以内

代码行最大长度宜控制在 70 至 80 个字符以内。代码行不要过长,否 则眼睛看不过来,也不便于打印。

 1 #include <iostream>
 2 #include <process.h>
 3 #include<stdio.h>
 4 #include<conio.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        //声明变量
10     int i;
11     char ch;
12     FILE *fp1;
13 
14     //以写入方式打开d.dat文件 
15     if ((fp1=fopen("d.dat","w"))==NULL)    {
16        cout<<"
Could not open the file."<<endl;
17        cout<<"Exiting program."<<endl;
18        exit(1);   //结束程序执行
19     }
20 
21     //循环从键盘上读取字符,写入文件
22     cout<<"char:";
23     cin>>ch; 
24     while (ch!='*') {
25         fputc(ch,fp1);   //将字符写到fp1指向的"流"文件中
26         cin>>ch; 
27     }
28     cout<<"--------------------"<<endl;
29     fclose(fp1);  //关闭文件
30 
31     //以读方式打开d.dat文件
32     if ((fp1=fopen("d.dat","r"))==NULL) 
33     {
34        cout<<"
Could not open the file."<<endl;
35        cout<<"Exiting program."<<endl;
36        exit(1);   //结束程序执行
37     }
38 
39     //循环从文件读取字符,并显示
40     while ((ch=fgetc(fp1))!=EOF)  
41           cout<<ch;        
42     cout<<endl<<"--------------------"<<endl;
43 
44     //以下按倒序方式读取文件中的字符,并显示
45     for (i=-1;;i--) {
46         fseek(fp1,i,2);               //设置文件指针,偏移量为i,相对文件尾
47         if ((ch=fgetc(fp1))!=EOF)
48             cout<<ch;
49          else
50             break;
51     }
52     cout<<endl<<"--------------------"<<endl;
53 
54     //以下读取"流"文件中偶数位置上的字符,并打印
55     long position;
56     for (i=0;;i=i+2) {
57         fseek(fp1,i,0);            //设置文件指针,偏移量为i,相对文件头
58         position=ftell(fp1);
59         if ((ch=fgetc(fp1))==EOF)     //遇到文件尾,则退出,否则打印读取的字符
60             break;     
61         else {
62            cout<<position<<" :"<<ch<<endl;
63         }
64     }
65     cout<<endl;
66 
67     fclose(fp1); //关闭文件
68     return 0;
69 }
原文地址:https://www.cnblogs.com/borter/p/9413302.html