• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            posts - 200, comments - 8, trackbacks - 0, articles - 0

            一.IO與文件映射
             1.IO的共享與效率
              read與write其中數(shù)據(jù)緩沖的大小
              讀取數(shù)據(jù)的緩沖大?。篻etpagesize。 
             2.定位與定位讀取(隨機(jī)讀取)
              read與write在操作的時(shí)候,自動(dòng)移動(dòng)讀取位置.
              lseek改變讀取位置.

              pread/pwrite在指定位置讀寫(xiě)。
              2.1.lseek的函數(shù)說(shuō)明: 

             off_t   lseek(
                                int fd,//定位文件描述符號(hào)
                                off_t off,//定位位置
                                int whence//定位參照點(diǎn):文件開(kāi)始位置/文件結(jié)束位置/文件當(dāng)前位置
                                                    
            //SEEK_SET    SEEK_END  SEEK_CUR
                                );
             返回:
                   返回當(dāng)前讀取位置在文件中的絕對(duì)位置.
              2.2.lseek的作用:定位文件的位置
                問(wèn)題:lseek的定位的位置超出文件的大小范圍?
                lseek移動(dòng)位置只要合法,都是有效
              2.3.lseek+write=pwrite
                  lseek+read =pread

            #include <stdio.h>
            #include <fcntl.h>
            #include <stdlib.h>
            main()
            {
                int fd;
                float score;
                int r;
                int i=0;
                fd=open("stu.dat",O_RDWR);
                if(fd==-1) printf("open error:%m\n"),exit(-1);
                    
                //定位
                /*
                for(i=0;i<2;i++)
                {
                    r=lseek(fd,i*28,SEEK_SET);    
                    r=lseek(fd,24,SEEK_CUR); 
                    //r=lseek(fd,i*28+24,SEEK_SET);
                    //讀取
                    r=read(fd,&score,sizeof(float));
                    //打印 輸出
                    printf("%.2f\n",score);
                }
            */
                /*
                r=lseek(fd,-100,SEEK_SET);
                printf("%d\n",r);
                //write(fd,"Hello",5);
                
            */
                for(i=0;i<2;i++)
                {
                    pread(fd,&score,sizeof(float),i*28+24);
                    printf("%.2f\n",score);
                    read(fd,&score,sizeof(float));
                    printf("%.2f\n",score);
                }
                
                close(fd);
            }
            2.4.案例:
                 讀取一個(gè)特殊的文件:
                   /proc/${pid}/mem文件程序的虛擬內(nèi)存文件

            #include <stdio.h>
            #include <stdlib.h> 
            #include <fcntl.h>
            int a=9999;
            main()
            {
                char filename[100];
                int fd;
                int data=8888;
                
                //得到文件名
                sprintf(filename,"/proc/%d/mem",getpid());
                //打開(kāi)文件
                fd=open(filename,O_RDWR);
                if(fd==-1) printf("open error:%m\n"),exit(-1);
                //讀取a地址這個(gè)位置的數(shù)據(jù)
                
            //pread(fd,&data,4,(int)&a);
                
            //lseek(fd,(int)&a,SEEK_SET);
                
            //read(fd,&data,4);
                
            //write(fd,&data,4);
                pwrite(fd,&data,4,(int)&a);
                printf("%d\n",a);
                
                close(fd);
            }
            3.文件的其他操作
                fstat獲取文件狀態(tài)
                ftruncate改變文件大小

            #include <fcntl.h>
            #include <stdio.h>
            #include <stdlib.h>
            #include <unistd.h>
            #include <sys/stat.h>
            main()
            {
                int fd;
                struct stat st;
                
                fd=open("stu.dat",O_RDONLY);
                if(fd==-1) printf("err:%m\n"),exit(-1);
                
                fstat(fd,&st);
                printf("%d,%o\n",st.st_size,st.st_mode);
                
                close(fd);
            }
            4.文件映射:
                虛擬地址映射到內(nèi)存。
                虛擬地址可以映射到文件:可以用內(nèi)存方式訪問(wèn)文件.
                mmap/munmap
              案例:
                1.使用內(nèi)存方式寫(xiě)入數(shù)據(jù) 

            #include <stdio.h>
            #include <stdlib.h>
            #include <unistd.h>
            #include <fcntl.h>
            #include <sys/stat.h>
            #include <string.h>
            #include <sys/mman.h>
            struct stu
            {
                char name[20];
                int  age;
                float score;
            };
            main()
            {
                int fd;
                struct stu *s;//文件在虛擬內(nèi)存的映射首地址
                struct stat st;
                int size;//文件大小
                int count;//記錄條數(shù)
                int i;
                //1.打開(kāi)文件
                fd=open("newstu.dat",O_RDWR|O_CREAT|O_EXCL,0666);
                if(fd==-1)
                {
                    fd=open("newstu.dat",O_RDWR);
                    if(fd==-1) printf("::%m\n"),exit(-1);
                }
                //2.得到文件大小,文件記錄條數(shù)
                fstat(fd,&st);
                size=st.st_size;
                count=size/sizeof(struct stu);
                //3.文件大小改變只要在munmap之前調(diào)用都有效    
                
            //ftruncate(fd,size+sizeof(struct stu));
                
            //4.映射到一個(gè)虛擬的地址
                s=mmap(0,size+sizeof(struct stu),
                            PROT_READ|PROT_WRITE,
                            MAP_SHARED,fd,0);
                //5.把數(shù)據(jù)寫(xiě)入虛擬地址
                /*
                printf("輸入姓名:");
                scanf("%s",s[count].name);
                printf("輸入年齡:");
                scanf("%d",&(s[count].age));
                printf("輸入成績(jī):");
                scanf("%f",&(s[count].score));
                ftruncate(fd,size+sizeof(struct stu));
                
            */
                for(i=0;i<count;i++)
                {
                    printf("%s,\t,%d,\t%.2f\n",
                        s[i].name,s[i].age,s[i].score);
                }    
                //6.卸載虛擬地址
                munmap(s,sizeof(struct stu)+size);
                //7.關(guān)閉文件
                close(fd);
            }

            作業(yè):
              -+
            2.使用內(nèi)存方式讀取數(shù)據(jù)
             
            二.文件描述符號(hào)的操作(IO鎖)
              文件描述符號(hào)是整數(shù).文件描述符號(hào)對(duì)應(yīng)內(nèi)核的上下文環(huán)境. 
              1.dup  dup2拷貝文件描述符號(hào)
               dup拷貝文件符號(hào),返回系統(tǒng)指定的整數(shù)
               dup2拷貝文件描述符號(hào),返回用戶指定的整數(shù)
              2.fcntl對(duì)文件描述的屬性的修改
                2.1.拷貝文件描述符號(hào)
                2.2.修改判定文件的描述標(biāo)記
                2.3.修改判定文件的狀態(tài)標(biāo)記
                     O_RDONLY O_WRONLY _ORDWR  O_CREAT O_EXCL
                     
                     O_APPEND O_ASYN
                2.4.設(shè)置強(qiáng)制鎖(重新編譯內(nèi)核)
                2.5.設(shè)置建議鎖(默認(rèn))
                2.6.設(shè)置的信號(hào) 

            三.IO與Curses(介紹)
              Curses:CUI
              
              UI:User Interface.
               CUI:字符界面
               GUI:圖形界面
             
             使用一套封裝庫(kù)  libcurses.so
             /usr/lib目錄下
             
             編譯只需要指定-lcurses
             老版本:libcurses.so
             新的版本:libncurses.so
             如果頭文件curses.h不存在,請(qǐng)嘗試使用ncurses.h
             如果庫(kù)curses不存在,嘗試使用ncurses
             
             printf /scanf標(biāo)準(zhǔn)IO
             大部分標(biāo)準(zhǔn)IO重定向到終端./dev/tty  /dev/pts/1
             
             curses就是終端輸出.
             -lcurses   -ncurses 
             
             為了防止printf重定向到終端破壞UI,禁止在curses中使用標(biāo)準(zhǔn)IO.
             
             
             1.編程模型
               初始化終端initscr
               操作終端(輸入/輸出/定位/刷新....)
               釋放終端endwin
             2.顯示
               2.1.圖形輸出
                border    
                box    
                hline    
                vline

            #include <curses.h>
            int main()
            {
                initscr();//初始化終端
                
            //border(0,0,0,0,0,0,0,0);
                box(stdscr,0,0);
                mvhline(2,10,'=',20);
                mvvline(2,10,'|',10);
                refresh();
                //wrefrsh(stdscr);
                getch();//等待一個(gè)字符輸入        
                endwin();//釋放終端
                return 0;
            }
            屬性字符:字節(jié)=屬性字節(jié)+字符字節(jié)
               注意:
                 box需要窗體.
                 initscr返回被初始化的窗體:標(biāo)準(zhǔn)屏幕WINDOW*
                 實(shí)際上curses定義一個(gè)全局變量stdscr就是標(biāo)準(zhǔn)屏幕 
               函數(shù)命名規(guī)則:
                  ****   標(biāo)準(zhǔn)屏幕stdscr
                  w****    指定窗體
                  mv****  指定位置
                  mvw****  指定窗體的指定位置
               2.2.刷屏
                 void refresh()   
                 void wrefresh(WINDOW*);
                 
                 從里到外刷屏
               2.3.字符輸出
                 addch
                 普通字符:''
                 屬性字符: '' | 屬性 //位與,屬性可以man attron查看可選屬性    
                 
                 特殊的屬性字符(特殊形狀):比如ACS_PI

               2.4.字符串輸出
                 int addstr(const char *);
               2.5.格式字符串輸出
                 int printw(const char*,....);  

            #include <curses.h> 
            main()
            {
                char name[9]={0};
                int r;
                initscr();
                //繪制UI
                mvaddstr(4,10,"用戶:[        ]");
                //輸入
                r=mvgetnstr(4,16,name,8);
                //name[r]=0;
                
            //打印輸入
                mvprintw(7,10,"你輸入的是:%s",name);
                refresh();
                //輸入字符    
                getch();
                endwin();
            }
            3.字符屬性與顏色
                顏色屬性
                3.1.判定終端是否支持顏色
                  bool has_colors();//都支持顏色,建議不判定
                3.2.初始化顏色:
                  int start_color();
                3.3.定義顏色對(duì)
                  int init_pair(short pair,short fore,short back);
                3.4.使用顏色對(duì)
                  COLOR_PAIR(short pair)      
                3.5.設(shè)置屬性
                  attron()開(kāi)啟屬性
                  attroff()關(guān)閉屬性
                  括號(hào)里傳入所需要開(kāi)啟或者關(guān)閉的屬性,      
                  如:attron(COLOR_PARE(1));//開(kāi)啟顏色對(duì)1
                這組函數(shù)一定要在initscr后調(diào)用
                
                背景函數(shù):
                  bkgd();
            #include <curses.h>
            #include <time.h>
            #include <unistd.h>
            void init();
            void drawui();
            void business();
            void destroy();
            main()
            {
                init();
                drawui();
                business();
                destroy();
            }
            void business()
            {
                time_t tt;
                struct tm *t;
                while(1)
                {
                    //取時(shí)間
                    tt=time(0);
                    t=localtime(&tt);
                    //顯示時(shí)間
                    mvprintw(LINES/2,(COLS-8)/2,
                        "%02d:%02d:%02d",
                        t->tm_hour,t->tm_min,t->tm_sec);
                    //刷新屏幕
                    refresh();
                    sleep(1);
                }
            }
            void drawui()
            {
                box(stdscr,0,0);
            }
            void destroy()
            {
                endwin();
            }
            void init()
            {
                initscr();
            }
             2.登錄界面
                 1.初始化
                 2.繪制界面
                    頭
                    繪制用戶名輸入?yún)^(qū)
                    繪制密碼輸入?yún)^(qū)
                    
                 3.等待輸入
                 4.結(jié)束
            #include <curses.h>
            #include <unistd.h>
            #include <stdlib.h>
            #include <string.h>
            void init();
            void drawLogin();
            void destroy();
            main()
            {
                init();
                drawLogin();
                destroy();
            }
            void drawLogin()
            {
                char *heads="聯(lián)通BSS業(yè)務(wù)支撐系統(tǒng)";
                char *user="用戶[           ]";
                char *pass="口令[           ]";
                
                box(stdscr,0,0);
                attron(A_BOLD);
                mvaddstr(3,(COLS-strlen(heads))/2,heads);
                mvhline(4,(COLS-strlen(heads))/2,0,strlen(heads));
                attroff(A_BOLD);
                mvaddstr(8,(COLS-strlen(user))/2,user);
                mvaddstr(10,(COLS-strlen(pass))/2,pass);
                refresh();
            }
            void destroy()
            {    
                getch();
                endwin();
                
            }
            void init()
            {
                initscr();
            }
            4.輸入
               1.字符輸入
                int getch();
                返回的是字符
                禁止回顯noecho() //回顯只是輸入以后不會(huì)出現(xiàn)在屏幕上,輸入密碼時(shí)采用這種方式
                使功能鍵有效,使用keypad(WINDOW*,bool) 

            #include <curses.h>
            main()
            {
                int ch;
                //初始化
                initscr();
                noecho();
                //循環(huán)輸入
                while(1)
                {
                    ch=mvgetch(5,10);
                    //循環(huán)顯示輸入
                    mvprintw(8,10,"你輸入的是:%c(%d)",ch,ch);
                }    
                //釋放
                endwin();
            }
            案例:
               使用鍵盤(pán)控制字母在屏幕上的移動(dòng)
                
               補(bǔ)充:
                 curses屏幕清除:man 3 clear
                   clear
                   erase 
                 光標(biāo)控制:
                   得到光標(biāo)位置  getsyx
                   設(shè)置光標(biāo)的位置 setsyx
                   控制光標(biāo)是否可見(jiàn):curs_set();
                   
               2.字符串輸入
                  int addstr
               
               3.格式數(shù)據(jù)輸入
                 scanw
            #include <curses.h>
            main()
            {
                int ch;
                int x=5,y=5;
                initscr();
                keypad(stdscr,TRUE);
                curs_set(0);
                noecho();
                mvaddch(y,x,'A');
                while(1)
                {
                    ch=getch();
                    //mvaddch(y,x,' ');
                    
            //clrtoeol();
                    erase();
                    //clear();
                    switch(ch)
                    {
                    case KEY_UP:
                        y--;
                        break;
                    case KEY_DOWN:
                        y++;
                        break;
                    case KEY_LEFT:
                        x--;
                        break;
                    case KEY_RIGHT:
                        x++;
                        break;
                    }
                    mvaddch(y,x,'A');
                    refresh();
                }        
                endwin();
            }
            5.窗口 
               subwin()//創(chuàng)建子窗體(坐標(biāo)采用標(biāo)準(zhǔn)屏幕坐標(biāo))
               derwin()//創(chuàng)建子窗體(坐標(biāo)采用父窗體坐標(biāo))

            #include <curses.h>
            main()
            {
                WINDOW *w;
                initscr();
                box(stdscr,0,0);
                
                w=derwin(stdscr,4,20,5,3);
                box(w,0,0);
                
                refresh();
                wrefresh(w);
                getch();
                endwin();
            }

            #include <curses.h>
            void init();
            void drawUi();
            void dealInput();
            void destroy();
            main()
            {
                init();
                drawUi();
                dealInput(); 
                destroy();
            }
            void dealInput()
            {
                int a,b;
                while(1)
                {
                    mvaddstr(2,3,"     ");
                    mvscanw(2,3,"%d",&a);
                    mvaddstr(2,11,"     ");
                    mvscanw(2,11,"%d",&b);
                    mvaddstr(2,19,"      ");
                    mvprintw(2,19,"%d",a+b);
                    refresh();
                }
            }
            void drawUi()
            {
                mvaddstr(2,2,"[     ]+[     ]=[      ]");
                refresh();
            }
            void destroy()
            {
                endwin();
            }
            void init()
            {
                initscr();
            }
            1.在vi設(shè)置編碼:
              :set encoding=編碼    gb2312  ios-8859-1  utf-8 
            2.在編譯器指定源文件的編碼 -finput-charset=gb2312
            3.在終端指定編碼:
            4.系統(tǒng)默認(rèn)編碼
              /etc/sysconfig/i18n配置編碼
              
                  
            作業(yè):(使用文件映射) 
             1.使用內(nèi)存方式讀取數(shù)據(jù)
             2.使用curses+io完成:圖書(shū)信息的錄入
             3.使用curses+io顯示圖書(shū)信息:
                每次顯示一條:
                使用up down鍵翻滾記錄數(shù)據(jù)
             4.讀取文件文件,使用curses 顯示.
                實(shí)現(xiàn)如下功能:
                   上下翻頁(yè)功能
                   輸入q,結(jié)束功能 

             

            亚洲午夜久久久久久噜噜噜| 精品人妻伦九区久久AAA片69| 亚洲一区精品伊人久久伊人| 18禁黄久久久AAA片| 97久久精品无码一区二区| 久久99精品久久久久久噜噜| 久久这里有精品| 色综合色天天久久婷婷基地| 综合久久一区二区三区| 久久国产高清字幕中文| 无码人妻少妇久久中文字幕| AAA级久久久精品无码片| 人人狠狠综合久久亚洲高清| 久久精品人人做人人爽97| 亚洲精品无码久久毛片| 久久99精品国产99久久6男男| 欧美精品国产综合久久| 精品无码久久久久久久久久| 久久香综合精品久久伊人| 亚洲综合久久久| 精品久久久久久无码人妻热| 国产精品久久网| 欧美大香线蕉线伊人久久| 久久亚洲国产精品成人AV秋霞| 99久久成人18免费网站| 国产亚洲精品美女久久久| 国内精品人妻无码久久久影院导航 | 99久久国产免费福利| 久久久久久亚洲Av无码精品专口| 亚洲午夜福利精品久久| 久久e热在这里只有国产中文精品99| 国内精品人妻无码久久久影院| 尹人香蕉久久99天天拍| 四虎国产精品成人免费久久| 久久久久国产精品麻豆AR影院 | 久久中文字幕一区二区| 久久不见久久见免费视频7| 国产成人精品免费久久久久| 人妻久久久一区二区三区| 亚洲精品国产字幕久久不卡| 无码人妻精品一区二区三区久久|