福建农林大学计科linux复习.doc_第1页
福建农林大学计科linux复习.doc_第2页
福建农林大学计科linux复习.doc_第3页
福建农林大学计科linux复习.doc_第4页
福建农林大学计科linux复习.doc_第5页
已阅读5页,还剩7页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

2012倾情巨献,走过错过千万不要路过LINUX程序设计复习(内部参考) 一ouch.c p 406 信号处理#include #include #include void ouch() printf(OUCH!n); (void)signal(SIGINT,SIG_DFL);main() (void)signal(SIGINT,ouch); while(1) printf(Hello world!n); sleep(1); 二gmtime.c 书本p 125 #include #include #include main() struct tm *tp; time_t t; (void) time(&t); tp=gmtime(&t); printf(date:%d/%d/%dn,tp-tm_year+1900, tp-tm_mon+1, tp-tm_mday); printf(time:%d:%d:%dn,tp-tm_hour+8,tp-tm_min,tp-tm_sec);三fork.c 书本p 398#include #include #include #include #include main() pid_t pid; char *s; int n,exit_code; printf(fork program startingn); pid=fork(); if (pid=0) printf(parentn); else printf(childn);/*fork函数用于创建一个新进程返回值:负数:如果出错,则fork()返回-1,此时没有创建新的进程。最初的进程仍然运行。零:在子进程中,fork()返回0正数:在父进程中,fork()返回正的子进程的PID*/四cpfile.c 书本p 87 文件复制#include #include #include #include #define MAX 1000main() int fin,fout,count; char bufMAX=0; fin=open(a.dat, O_RDONLY); fout=open(b.dat, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR); do count=read(fin,buf,sizeof(buf); if (count0) write(fout,buf,count); while (count0); close(fin); close(fout);/* 1) open()int open( const char * pathname, int flags);int open( const char * pathname,int flags, mode_t mode);参数pathname 指向欲打开的文件路径字符串Flags:O_RDONLY 以只读方式打开文件 O_WRONLY 以只写方式打开文件 O_RDWR 以可读写方式打开文件。O_CREAT 若欲打开的文件不存在则自动建立该文件。 O_EXCL 如果O_CREAT 也被设置,此指令会去检查文件是否存在。 O_TRUNC 把文件长度设置为0,丢弃已有的内容。 O_APPEND 当读写文件时会从文件尾开始移动,也就是所写入的数据会以附加的方式加入到文件后面。Mode:S_IRUSR , 00400权限,代表该文件所有者具有可读取的权限。 S_IWUSR ,00200 权限,代表该文件所有者具有可写入的权限。 S_IXUSR , 00100 权限,代表该文件所有者具有可执行的权限。 S_IRGRP 00040 权限,代表该文件用户组具有可读的权限。 S_IWGRP 00020权限,代表该文件用户组具有可写入的权限。 S_IXGRP 00010 权限,代表该文件用户组具有可执行的权限。 S_IROTH 00004 权限,代表其他用户具有可读的权限 S_IWOTH 00002权限,代表其他用户具有可写入的权限。 S_IXOTH 00001 权限,代表其他用户具有可执行的权限。2) write()和read()size_t write (int fildes , const void *buf , size_t nbytes);系统调用write的作用是:把缓冲区buf的前nbytes个字节写入与文件描述符fildes关联的文件中。它返回实际写入的字节数。如果函数返回0,表示未写入任何数据;如果返回-1,表示write调用出错。size_t read (int fildes , void *buf , size_t nbytes);系统调用read的作用是:从文件描述符fildes关联的文件里读入nbutes个字节数据,并把他们放到数据区buf中。返回实际读入的字节数。如果函数返回0,表示未写入任何数据,已经到达了文件尾;如果返回-1,表示read调用出错。*/五sum 书本p32 for语句 、p33 while 语句#该程序是直接在linux下运行的脚本程序,对输入的参数N,求1+2+.+N的值。#方法1s=0for i in $(seq $1)do s=$($s+$i)doneecho $s#方法2s=0i=1while $i -le $1 ; do s=$($s+$i) i=$($i+1)doneecho $s六gcd 书本p 29 if语句#该程序是直接在linux下运行的脚本程序,对输入的两个参数求最大公约数if $1 -gt $2 ;then m=$1 n=$2else m=$2 n=$1fiwhile $n -ne 0 ; do t=$n n=$($m % $n) m=$tdoneecho $m七 bdm 书本p237 数据库1create_dbm1.c #include #include #include #include #include #include main() FILE *fr; char s1024, sno10; int m; datum key,dat; DBM *dbp; dbp=dbm_open( /tmp/score1,O_RDWR | O_CREAT, 0666); fr=fopen(score.txt,r); while (!feof(fr) fgets(s,1020,fr); sscanf(s,%s%*s%d,sno,&m); printf(%s: %dn, sno,m); key.dptr=(void *)sno; key.dsize=strlen(sno); dat.dptr=(void *)&m; dat.dsize=sizeof(int); dbm_store(dbp,key,dat,DBM_REPLACE); fclose(fr); dbm_close(dbp);2create_dbm2.c#include #include #include #include #include #include struct sty char name20; int m; main() FILE *fr; char s1024, sno10; struct sty x; datum key,dat; DBM *dbp; dbp=dbm_open( /tmp/score2,O_RDWR | O_CREAT, 0666); fr=fopen(score.txt,r); while (!feof(fr) fgets(s,1020,fr); sscanf(s,%s%s%d,sno,,&x.m); printf(%s %20s: %dn, sno,,x.m); key.dptr=(void *)sno; key.dsize=strlen(sno); dat.dptr=(void *)&x; dat.dsize=sizeof(x); dbm_store(dbp,key,dat,DBM_REPLACE); fclose(fr); dbm_close(dbp);3use_dbm1.c#include #include #include #include #include #include main() char s1024, sno10; int m; datum key,dat; DBM *dbp; dbp=dbm_open( /tmp/score1,O_RDWR , 0666); for(key=dbm_firstkey(dbp); key.dptr; key=dbm_nextkey(dbp) memcpy(sno,key.dptr,key.dsize); snokey.dsize=0; dat=dbm_fetch(dbp,key); memcpy(&m,dat.dptr,dat.dsize); printf(%s: %dn,sno,m); key.dptr=S1013008; key.dsize=8; dat=dbm_fetch(dbp,key); if (dat.dptr) memcpy(&m,dat.dptr,dat.dsize); printf(%dn,m); m=200; dat.dptr=(void *)&m; dat.dsize=sizeof(int); dbm_store(dbp,key,dat,DBM_REPLACE); else printf(No data found for keyn); dbm_close(dbp);4use_dbm2.c#include #include #include #include #include #include struct sty char name20; int m; main() char s1024, sno10; struct sty x; datum key,dat; DBM *dbp; dbp=dbm_open( /tmp/score2,O_RDWR , 0666); for(key=dbm_firstkey(dbp); key.dptr; key=dbm_nextkey(dbp) memcpy(sno,key.dptr,key.dsize); snokey.dsize=0; dat=dbm_fetch(dbp,key); memcpy(&x,dat.dptr,dat.dsize); printf(%s: %20s %dn,sno,,x.m); key.dptr=S1013008; key.dsize=8; dat=dbm_fetch(dbp,key); if (dat.dptr) memcpy(&x,dat.dptr,dat.dsize); printf(%dn,x.m); x.m=200; dat.dptr=(void *)&x; dbm_store(dbp,key,dat,DBM_REPLACE); else printf(No data found for keyn); dbm_close(dbp);八key.c 书本p 183 键盘输入#include #include #include #define ESC 27main() int key; initscr(); crmode(); keypad(stdscr,TRUE); noecho(); clear(); mvprintw(5,5,Key pad demonstration. Press q to quit); refresh(); key=getch(); while (key!=ERR & key!=q) move(7,5); clrtoeol(); switch(key) case ESC: printw(%s,Escape key!); break; case KEY_END: printw(%s,End key!); break; case KEY_BEG: printw(%s,BEG key!); break; case KEY_RIGHT:printw(%s, RIGHT key); break; case KEY_LEFT: printw(%s, LEFT key); break; case KEY_UP: printw(%s, UP key); break; case KEY_DOWN: printw(%s,DOWN key); break; case 8: printw(%s,back); break; case 9: printw(%s,Tab key); break; case 10: case 13: printw(%s,Enter key); break; case 32: printw(%s,VK_SPACE); break; case 262: printw(%s,Home); break; case 263: printw(%s,Backspace); break; case 331: printw(%s,insert);break; case 339: printw(%s,Page Up); break; case 338: printw(%s,Page down); break; case 330: printw(%s,Delete); break; default: printw(Key was: %c,key); refresh(); key=getch(); endwin(); exit(EXIT_SUCCESS); 九menu.c 书本p193 #include #include #include main() char *option=Add,Delete,Modify,Quit,NULL; /WINDOW *pad; int key,srow, scol, crow=0; int i; char *p; initscr(); keypad(stdscr,TRUE); cbreak(); noecho(); srow=5; scol=5; i=0; while (optioni) if (i=crow) attron(A_STANDOUT); mvprintw(srow+i,scol,%s,optioni); if (i=crow) attroff(A_STANDOUT); i+; refresh(); key=0; i=crow; while (key!=27) key=getch(); switch (key) case KEY_DOWN: i+; if (i=4) i=0; break; case KEY_UP: i-; if (i0) i=3; break; if (i!=crow) mvprintw(srow+crow,scol,%s,optioncrow); attron(A_STANDOUT); mvprintw(srow+i,scol,%s,optioni); attroff(A_STANDOUT); refresh(); crow=i; keypad(stdscr,FALSE); nocbreak(); echo(); endwin(); exit(EXIT_SUCCESS); /*attron(mod): 开启属性.attroff(mod): 关闭属性.curses.h 里头定义了一些属性, 如: A_STANDOUT: 反色 A_DIM: A_UNDERLINE: 加底线.A_REVERSE: 反白.A_BLINK: 闪烁.A_BOLD: 高亮度.*/十mwin.c 书本p178 屏幕 #include #include #include #include main() WINDOW *nwp,*pwp; int x,y; char c=a; initscr(); /初始化 move(5,5); /将光标移动到屏幕上坐标(5,5)处 printw(%s,Test multiple windows); sleep(2); /将程序暂停2秒钟 for(y=0;yLINES-1;y+) for(x=0;xz) c=a; refresh(); /刷新屏幕 sleep(2); nwp=newwin(10,10,5,5)

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论