world:
:%-20s: :hello, world :
:%20.10s: : hello, wor:
:%-20.10s: :hello, wor :
:%.10s: :hello, wor:
: printf
.
,
.
13.4. - scanf
scanf
printf
.
scanf(control, arg1, arg2, ...)
,
, control,
.
; ,
, ,
.
-
, -
.
-55-
:
- , ("
"), ;
- ( %), -
"
" ;
- , %,
*,
,
.
. -
, .
, , * -
,
.
, " -
";
, ,
. ,
, scanf , -
.
, -
("scanf(3)").
; ,
scanf . -
:
d - ;
.
o - (
);
.
x - ( -
0 ); -
.
h - short;
short.
c - ;
;
-56-
.
-
; , -
, -
%1s.
s - ;
,
,
\0.
f - ; -
float.
e - e f.
float -
, , -
, -
e, ,
.
d, o x
l, ,
long, int.
, l -
e f, ,
double,
float.
,
int 1;
float ;
char name[50];
scanf("%d %f %s", &i, &, name);
25 54.32e-1 thompson
i 25, - 5.432
name - "thompson",
\0. -
, ,
.
int i;
float ;
char name[50];
scanf("%2d %f %*d %2s", &i, &, name);
-57-
56789 0123 4572
i 56, - 789.0, 0123
name "45". -
a. -
name , ,
&.
-
, :
#include <stdio.h>
main() /* */
{
double sum, v;
sum =0;
while (scanf("%lf", &v) !=EOF)
printf("\t%.2fFI\n", sum += v);
}
scanf ,
,
-
.
.
. EOF; -
, 0, , -
. scanf
-
.
: scanf
. ,
scanf("%d", n);
scanf("%d", &n);
13.5.
scanf printf sscanf
sprintf, ,
, . -
:
-58-
sprintf(string, control, arg1, arg2, ...)
sscanf(string, control, arg1, arg2, ...)
, sprintf
arg1, arg2 .. ,
control, string,
. , string ,
. , name - -
, n - ,
sprintf(name, "temp%d", n);
name "tempnnn", nnn - n.
sscanf -
string
control -
arg1, arg2 .. -
.
sscanf(name, "temp%d", &n);
n ,
temp name.
13.6.
, -
.
,
, /
.
-
,
open . fopen
( .c "temp002")
,
.
, " ", -
,
, , -
,
.
, ,
stdio.h, .
: FILE *fp;
-59-
, fp FILE.
, file ,
int, ;
"#define".
fopen :
fp=fopen(name,mode);
fopen ,
"name".
mode () , -
, .
: (r), (w) (a),
+ (, r+), -
, , .
, ,
, (
).
. -
. -
(,
, ). -
NULL
( stdio.h).
, . -
, getc putc .
getc ; -
, , .
: c=getc(fp)
c , -
fp, EOF, .
putc: putc(c,fp)
c fp c.
getchar putchar, getc putc ,
.
, .
,
;
stdin, stdout stderr.
, stdin stdout -
.
getchar putchar -
getc, putc, stdin stdout :
#define getchar() getc(stdin)
#define putchar(c) putc(c, stdout)
-60-
fscanf fprintf. -
scanf printf, , -
, ,
; -
.
stdin stdout
-
; ,
FILE *. , , -
, , .
fclose fopen;
,
fopen,
. -
,
. fclose ,
, putc
(
fclose
).
13.7. - stderr exit
,
, -
.
,
, stdin stdout,
, stderr.
, , stderr, -
,
(
,
).
exit
, -
. exit
, -
,
. , 0
,
, -
.
exit fclose -
,
, _exit.
_exit
-61-
- ; ,
.
13.8.
fgets.
fgets(line, maxline, fp)
( ) -
fp line;
maxline-1 .
\0. fgets
line; NULL.
fputs
( )
:
fputs(line, fp)
gets puts
fgets fputs,
; gets -
, puts
:
gets(line)
puts(line)
13.9. ungetc
,
.
ungetc(c,fp)
c fp.
.
13.10.
-
,
.
13.10.1.
calloc . -
-62-
calloc(n, sizeof(objct))
,
n , NULL,
. -
. malloc
, :
malloc(size)
,
,
char *calloc();
int *ip;
ip=(int*) calloc(n,sizeof(int));
free(p) ,
p, p
calloc.
, -
, calloc mal-
loc, .
13.10.2.
,
, .
, 4 ( ), -
(man(3)). -
, -
:
- (string);
- sscanf sprintf" (atoi,
itoa, atof, ftoa);
- (sin, exp, ...);
- (ctype);
- .
* 14.
-63-
14.1.
, ,
-77, .
cc.
:
cc 1.c 2.c ...
1.c, 2.c, ... - , -
(
.c).
. -
, a.out,
, :
a.out
( ).
: -
, ,
(
cc). :
cc 1.c 2.c ...
cc N.c N1.c ...
cc 1.o 2.o ... N.o ...
,
, a.out,
cdeb, ..
(cc(1), ld(1)).
shell cshell :
_ __
, , .
main.
, -
.
14.2.
-
. -
main , -
. ( int, argc)
,
-64-
; (argv)
,
, .
-
echo,
,
. ,
echo hello, world
:
hello, world
argv[0] ,
, argc 1.
argc 3, argv[0], argv[1]
argv[2] echo, hello, world.
argv[1], -
argv[argc-1]. argc 1,
. -
echo:
main(argc, argv)
int argc;
char *argv[];
{
int i;
for (i = 1; i < argc; i++)
printf("%s%c", argv[i],
(i<argc-1) ? ' ' : '\n');
}
argv ,
,
, -
. :
main(argc, argv)
int argc;
char **argv;
{
while (--argc > 0)
printf("%s%c",*++argv,
(argc > 1) ? ' ' : '\n');
}
,
, , -
.
-65-
( ). , TERM
, . -
getenv:
char *getenv();
par = getenv("_")
- -
, NULL, .
* 15.
.
-
. :
/, , , . -
,
"", "", "".
-
-
( 2 3). , -
popen(3),
, 4,
, 3;
, man 3 popen.
15.1. /
/ -
,
. -
-, . -
/
, .
15.1.1.
-
,
, -
, .
, -
.
-
; "" .
,
( ? -
?), ,
,
-66-
. ,
,
, (
read (5,...) write (6,...) ).
;
.
-
. (shell) -
, , -
, , -
0, 1 2 -
.
,
0 1 2,
,
.
, <
>:
prog < infile > outfile
0 1 .
2 ,
.
,
. ,
shell
( cshell), . ,
0 1 2 ,
,
.
15.1.2. /.
/
- , -
;
. -
: read write. -
. -
,
. -
. :
n_read=read(fd,buf,n);
n_written=write(fd,buf,n);
-67-
, -
. -
,
.
, "-1" - .
-
; , -
,
.
, ,
.
"1", -
(.. ), "512",
-
.
,
.
. .
,
.
#define BUFSIZE 512
main() /*copy input to output*/
{
char buf[BUFSIZE];
int n;
while((n=read(0,buf,BUFSIZE))>0)
write(1,buf,n);
}
BUFSIZE,
read ,
write;
read .
15.1.3. , ,
, -
, ,
,
. : open
creat.
open fopen, -
, ,
,
int.
int fd;
fd=open(name,rwmode);
-68-
fopen, name
, . -
, , : rwmode : 0 -
, 1 - , 2 - .
- , open "-1";
.
, ,
. creat
. :
fd=creat(name,pmode);
, -
name, "-1" . -
, , :
creat .
, creat
,
pmode.
,
, ,
. -
,
. , 0755
,
-
.
( 15 - 25)
, -
. ,
, -
. close
-
. exit
.
unlink(filename)
filename (, filename,
, -
).
15.1.4. - lseek
: read
write , -
.
-69-
.
lseek , -
.
lseek(fd,offset,origin);
fd
offset (), ,
origin ( ).
.
offset long; fd origin int.
origin 0, 1 2, -
, offset -
,
. , ,
:
lseek(fd,0l,2);
, :
lseek(fd,0l,0);
0l;
(long) 0.
lseek
, , -
.
. , , -
.
/* n pos buf */
get(fd,pos,buf,n)
int fd, n;
long pos;
char *buf;
{
lseek(fd,pos,0); /*get to pos */
return(read(fd,buf,n));
}
15.2.
.
,
, .
-70-
15.2.1. system
- -
system:
system(" ")
- , -
, , -
shell .
-
(0, ).
/ >, <, ..
, , -
, system
, fflush.
15.2.2. - execl
, -
execl - -
, .
:
execl(,0,1,...,N,NULL);
"" - , -
. , pr
/bin/pr.
, 0
, -
.
execl -
,
execl (, -
).
, -
(execl(2), execv(2),
execvp(2), ...) ,
.
15.2.3. - fork
, ,
-
. "fork" (-
):
-71-
proc_id = fork()
, -
.
- "" - fork
0, - "" - ,
"".
fork execl -
:
if( fork() == 0)
{ /* - */
... ...
execl(... );
/* execl */
perror(" ");
exit(1);
}
... ...
fork , -
("" "") , -
, . -
"",
wait:
int status;
...
if( fork() == 0)
{ ... execl(...); exit(1);
}
wait(&status));
wait - "",
status .
- 8 -
;
exit"
0. 8 ,
exit,
exit; 0 ,
, .
fork, execl ,
fork
,