Unix/Linux编程实践教程(三:代码、测试)

时间:2023-11-13 17:11:32

测试logfilec.c的时候,有个sendto(sock,msg,strlen(msg),0,&addr,addrlen),编译时提示:

logfilec.c:30: warning: passing argument 5 of ‘sendto’ from incompatible pointer type

但是书上是这样写的,在*搜了一下,原来是:

Unix/Linux编程实践教程(三:代码、测试)

需要进行一个转换。

另外才注意到C语言中单引号可转义可不转。

需要用curses库的测试hello1.c,发现没有,需要安装。

//hello1.c
#include<stdio.h>
#include<curses.h>
#include<stdlib.h> main()
{
initscr(); clear();
move(,);
addstr("hello,world");
move(LINES-,);
refresh();
getch();
endwin();
}

hello1.c

Unix/Linux编程实践教程(三:代码、测试)

按照yum的安装,安装失败,yum命令无法成功运行。还是不能成功编译hello1.c。

后来网上看才发现CentOS已经默认安装curses,只是编译时需链接库:

gcc hello1.c -o hello1 -lcurses

测试bounce1d.c时,提示:

undefined reference to `set_ticker'

原来set_ticker这个函数并不包含在库里面,需要自己编写。

//bounce1d.c
#include<stdio.h>
#include<curses.h>
#include<sys/time.h>
#include<signal.h>
#include<string.h> #define MESSAGE "hello"
#define BLANK " " int row;
int col;
int dir; int main()
{
int delay;
int ndelay;
int c;
void move_msg(int);
int set_ticker(long);
initscr();
crmode();
noecho();
clear(); row=;
col=;
dir=;
delay=; move(row,col);
addstr(MESSAGE);
signal(SIGALRM,move_msg);
set_ticker(delay); while()
{
ndelay=;
c=getch();
if(c=='q') break;
if(c==' ') dir=-dir;
if(c=='f'&&delay>) ndelay=delay/;
if(c=='s') ndelay=delay*;
if(ndelay>)
set_ticker(delay=ndelay);
} endwin();
return ; } void move_msg(int signum)
{
signal(SIGALRM,move_msg);
move(row,col);
addstr(BLANK);
col+=dir;
move(row,col);
addstr( MESSAGE);
refresh(); if(dir==-&&col<=)
dir=;
else if(dir==&&col+strlen(MESSAGE)>=COLS)
dir=-;
} int set_ticker(long n_msecs)
{
struct itimerval new_timeset;
long n_sec,n_usecs; n_sec=n_msecs/;
n_usecs=(n_msecs%)*1000L; new_timeset.it_interval.tv_sec=n_sec;
new_timeset.it_interval.tv_usec=n_usecs;
new_timeset.it_value.tv_sec=n_sec;
new_timeset.it_value.tv_usec=n_usecs; return setitimer(ITIMER_REAL,&new_timeset,NULL); }

bounce1d.c