' . ' () MS Windows. , -, , [1].
. ' , , . : Borland C++, Visual C++ .
MS Windows Visual C++ 6.0 ( ) , Windows, , , UNIX:
- ;
- , , ;
- , ' ;
- ;
- ;
- - .
, , . .
:
;
;
, , .
ѳ.
1.
, , .
. spawnl - spawnvpe ѳ , , .
.
"" , . , , . , . , , , , .
Visual C++ , , , . ( signal()) process.h 䳿:
|
|
1. (getpid).
2. (abort,exit,_exit).
3. (signal).
4. ( exec- spawn-, system-).
:
spawnl - .
spawnle - .
spawnlp - , PATH
spawnlpe - , PATH, .
spawnv -
spawnve - .
spawnvp - , PATH .
spawnvpe - , PATH , .
spawn-, main() .
main :
main(int argc, char *argv[],char *envp[]).
argc , main() . argv main . envp , (), .
argv[0] ' , , argc , . , , , . , . , , ѳ- :
:\>cprog Working program
cprog , argc argv :
arg=4
argv[0]="a:\cprog.exe"
argv[1]="Working"
argv[2]="C"
argv[3]="program"
argv[4]=""=NULL='\0'.
() , . :
" NAME = value "
NAME - ' , value . , value . NULL. :
PROMPT -
PATH - , O ,
COMSPEC - ' , .
:
envp[0]="PROMPT=$g$s"
envp[1]="path=a:\;c:\"
envp[2]="comspec=c:\command.com"
envp[3]=""=NULL'\0'.
Spawn - , . spawn ( exec -) . spawn - , ', , ' . spawn -.
|
|
#include <stdio.h>
int spawnl(modeflag,pathname,arg0,arg1...,argn,NULL);
int spawnle(modeflag,pathname,arg0,arg1...,argn, NULL,envp);
int spawnlp(modeflag,pathname,arg0,arg1...,argn,NULL);
int spawnlpe(modeflag,pathname,arg0,arg1...,argn, NULL,envp);
int spawnv(modeflag,pathname,argv);
int spawnve(modeflag,pathname,argv,envp);
int spawnvp(modeflag,pathname,argv);
int spawnvpe(modeflag,pathname,argv,envp);
int modeflag;
char *pathname;
char *arg0,*arg1...,*argn;
char *argv[];
char *envp[];
modeflag 䳿 . process.h modeflag :
P_WAIT - , , . ;
P_OVERLAY - ' , .
pathname , , . Pathname ( ), ( ) ' . pathname , "COM" . , "EXE" . pathname , . pathname , pathname . Spawnlp, spawnlpe, spawnvp spawnvpe , PATH, .
, , ( char). , , 128 . ('\\0') , , .
spawnl, spawnle, spawnlp, spawnlpe arg0, arg1... argn, , spawnv, spawnve, spawnvp spawnvpe argv.
̳ , arg0 argv[0], , . , ﳺ pathname.
spawnl, spawnle, spawnlp, spawnlpe , . argn NULL, .
spawnv, spawnvp, spawnve, spawnvpe , . argv. argv[0] ' pathname, argv[1] argv[n] , . argv[n+1] NULL , , .
spawnl, spawnlp, spawnv, spawnvp . spawnle spawnlpe, spawnve spawnvpe , envp . envp , , . envp NULL. envp NULL, .
1.1 spawn -. ' . : PATH , . . : .
|
|
1.1
PATH | |||
spawnl | PATH | ||
spawnle | PATH | ||
spawnlp | PATH | ||
spawnlpe | PATH | ||
spawnv | PATH | ||
spawnve | PATH | ||
spawnvp | PATH | ||
spawnvpe | PATH |
Spawn - . 0, . exit . . spawn - -1, errno :
E2BIG - ;
EINVAL - modeflag;
ENOENT - ' ;
ENOEXEC - ;
ENOMEM - '.
³, , spawn -, , . , , setmode (). ', , -, .
1. . .
2. prog . Settings Project, Debug, Program Arguments. prog.exe.
3. , , spawn (8 ), prog.exe (8 ). , , , , . PATH. spawnl-spawnvpe 1 8 , , spawn. ' spawn- .
1. ?
2. Visual C++?
3. main?
4. ?
5. ?
6. spawn- PATH , ?
7. ?
8. spawn-?
9. ?
10. spawn- ?
2.
|
|
. .
. .
.
. . - . . . , , , . ', , , -, , , , , - . .
, . .
< 0 1 2 0 3 1 2 >. , , , - . - 0, < 1 2 0 3 1 2 > .
.
1. . 6 < 6 >.
2. , 2 < 6 7 3 7 1 >.
3. . 6.
4. . , 2 , < 2 6 7 3 7 1 >.
5. . , 2 , < 2 6 7 3 7 1 2 >.
6. . , < 6 7 3 7 1 2 >.
7. . ϳ < 6 7 3 7 1 >.
8. , . 7, < 6 3 7 1>.
9. () .
10. , , < 1 2 3 4 > < 9 8 7 5 >
< 1 2 3 4 9 8 7 5 >.
11. ﳿ .
4 6, 5 7, . - 5 6. , 4, 5, 6 7, .
. . , , :
struct list {struct name_age data;
struct list *next;};
data - ,
struct name_age {char *name; /* */
int age;}; /*³*/
next - .
. - struct list*;
, ' . next.
struct list{int val; /* */
struct list *next;};.
typedef struct list{int val;
struct list *next;} LIST;
LIST, LIST*.
#define END (LIST*)0.
, 1-11.
LIST* crtlst(int); /**/
int exist(LIST* h,int i); /* i h*/
int head(LIST* h); /* h*/
void uptoh(LIST** ph, int i);/* i (*ph)*/
void uptot(LIST* h,int i); /* i h*/
int delfromh(LIST** ph); /* (*ph)*/
int delfromt(LIST** ph); /* (*ph)*/
int del(LIST** ph, int i); /* i (*ph)*/
void print(LIST* h); /* h*/
LIST* concat(LIST* f,LIST* s);/*ᒺ f s */
|
|
LIST *dbl(LIST* s); /* */
LIST**. ³ , , .
head
int head(LIST* h);
{return h->val;}
, . ' . ѳ malloc free.
void* malloc(unsigned size)
' size . ' NULL. ', malloc,
void free(void *p)
- ', .
, '
LIST *j;
j=(LIST*)malloc(sizeof(LIST));
free(j).
.
LIST* crtlst(int l) /**/
{LIST* h=(LIST*)malloc(sizeof(LIST));
h->val=l; h->next=END;
return h;}
l. (END) next.
#define END (LIST*)0
void uptoh(LIST** h, int v) /* v (*h)*/
{LIST *oldh=*h;
*h=(LIST*)malloc(sizeof(LIST));
(*h)->val=v;
(*h)->next=oldh; }
, (*h).
int delfromt(LIST** h) /* (*h)*/
{ LIST *i,*j;
int v;
if((*h)->next==END)
{v=(*h)->val; free(*h); (*h)=END;return v;}
for(j=(*h),i=j->next;i->next;j=i,i=j->next);
v= i->val;free(i);
j->next=END;
return v;}
, . , .
void print(LIST* h) /**/
{LIST *i;
printf("\n ");
if(h==END) printf(" ");
for(i=h;i;i=i->next)
printf(" %d ",i->val); }
. , .
LIST*concat(LIST* f, LIST* s) /*ᒺ f s */
{LIST* i;
for(i=f;i->next;i=i->next);
i->next=s; return f;}
s f. ', f s. .
LIST *dbl(LIST* s) /* */
{LIST *p,*t,*i;
t=(LIST*)malloc(sizeof(LIST));
for(p=s,i=t;p->next;p=p->next)
{i->val=p->val;
i=i->next=(LIST*)malloc(sizeof(LIST));
}
i->next=END;
i->val=p->val;
return t;}
s t. ' ﳿ malloc. END.
.
1. . .
2. crtlst, delfromt, uptoh print.
3. , :
)
int exist(LIST* h, int i); /* i h*/.
, print. 1 0 .
) i
void uptot(LIST* h,int i); /* i h*/
, ' , , next . ', next 0 (END).
) .
int delfromh(LIST** ph); /* (*ph)*/.
. (*h) ' - .
) i
int del(LIST** ph, int i); /* i (*ph)*/
, delfromh, , delfromt. , , next. '. 1 -1 i.
3. :
#include <stdio.h>
typedef struct list{int val;
struct list *next;} LIST;
#define END (LIST*)0
LIST* crtlst(int);
int delfromh(LIST**);
void uptot(LIST*,int);
void uptoh(LIST**,int);
int delfromt(LIST**);
int del(LIST**,int);
int exist(LIST*,int);
void print(LIST*);
main()
{int i; LIST* h;
printf(" \n ? ");
scanf("%d",&i));
h=crtlst(i);
print(h);
while(1)
{printf(" \nֳ ? ");
scanf("%d",&i));
if(h==(LIST*)0){printf("\n \n");exit(1);}
print(h);
if (exist(h,i))
{printf(" \n %d \n",i);
printf(" \n %d (/n)?",i);
if(getche()=='y')
{del(&h,i);
if(h==END){printf("\n \n");exit(1);}
print(h); }
}
printf(" \n %d (/n)? ",i);
if(getche()=='y') {uptot(h,i);print(h);}
printf(" \n %d (/n)h? ",i);
if(getche()=='y') {uptoh(&h,i); print(h);}
printf(" \n (/n)?");
if(getche()=='y')
if(h==END){printf("\n \n");exit(1);}
else
{printf(" \n %d\n"
delfromh(&h));
print(h); }
printf(" \n (/n)?");
if(getche()=='y')
if(h==END) {printf("\n \n");exit(1);}
else
{printf(" \n %d\n"
delfromt(&h));
print(h);
}}}
1. ? .
2. ?
3. 11 . ³ .
4. ? ' ?
5. LIST**?
6. ? .
7. ' ? .
8. ѳ?
9. delfromt ?
3.
.
. , ' .
ѳ . 3 -:
- - ;
- - ;
- - .
(). - () , .
-. - , , , .
, , , . . , .
. ֳ stdio.h. EOF ( ) NULL ( ), FILE, . .
, . ' FILE, . ³ fopen. FILE, . :
FILE *fopen(char *pathname,char *type),
pathname - ' , , type - , :
"r" - ( );
"w" - , , ;
"a" - (); , ;
"r+" - ( );
"w+" - , ;
"a+" - ; , .
"w" "w+", .
(, , ). :
"t" - ( _ _ (CRLF) (LF), LF CR-LF; Ctrl-Z );
"b" - ( Ctrl-Z ).
, "rb" . "t".
fopen NULL.
, ѳ . :
stdin - ( , );
stdout ( , );
stderr - ( , );
stdprn - ( , ).
"abc.def" :
#include <stdio.h>
main()
{
FILE *stream;
if ((stream = fopen("abc.def","r")) == NULL) {
printf (" abc.def\n");
exit(1); /* 1*/}
}
, . , , , , .
int fclose(FILE *stream),
stream - , .
0 EOF .
, ' , , FILE .
#include <stdio.h>
main()
{ FILE *stream;
stream = fopen("abc.def","r");
if(EOF==fclose(stream))
printf(" abc.def.\n");
}
, - . . , , 1 , . , . . , ' ( ), .
fget fput.
int fgetc(FILE *stream) stream, , . , EOF. feof ferror.
# include <stdio.h>
main()
{FILE *stream;
char buffer[81];
int i, ch;
/* stream */
for (i = 0; i < 80 && ((ch = fgetc(stream)!= EOF) && (ch!= '\n'); i++)
buffer[i]= ch;
buffer[i]= '\0'; }
int fputc(int , FILE *stream) stream. EOF, . , feof ferror.
.
#include <stdio.h>
main()
{ FILE *stream;
char buffer[81];
int i, ch;
/* buffer stream*/
for (i = 0; i < 81 && ((ch = fputc(buffer[i], stream))! = EOF); i++);
}
fseek, - .
int fseek (FILE *stream, long offset, int origin).
, ' stream, , offset , origin. origin , stdio.h:
SEEK_SET - ;
SEEK_CUR - ;
SEEK_END - .
.
, ( a+, r+ w+) , . "a" "a+" , . , fseek rewind , . , .
, - . . . . 0.
.
#include <stdio.h>
main()
{FILE *stream; int result;
stream = fopen("data","r");
...
/* */
result = fseek(stream,0L,SEEK_SET);
}
- stream,
long ftell(FILE *stream),
-1L .
.
#include <stdio.h>
main()
{ FILE *stream;
long sition;
stream = fopen("data","rb");
...
sition = ftell(stream); }
ѳ , , . .
void rror (char string),
stderr. string, , , . errno, . sys_errlist, . error , errno, sys-errlist. rror , . errno .
.
,,,
{ FILE *p;
if(NULL==(p=fopen("file.dat","r")))
rror(" FILE.DAT"); }
FILE.DAT , :
FILE.DAT: No such file or directory.
, , FILE . ferror feof. void clearerr (FILE *stream), stream.
ferror
int ferror(FILE *stream)
, stream .
.
#include <stdio.h>
main()
{int i;
i=fputc ('*', stdin);
if (ferror(stdin)) {
rror(" ");
clearerr();}
}
stdin , : : Error 0.
, ' stream, int feof(stream). . , clearerr. fseek .
.
#include <stdio.h>
main()
{char st[100];
FILE *stream;
/*
, */
while (! feof (stream))
{
(fscanf(stream,"%s",st))
/* */}
ferror feof clearerr.
.
#include <stdio.h>
#include <stdlib.h>
main()
{FILE *stream;
int c;
/* : . */
if ((c=getc(stream)) == EOF) {
if (ferror(stream){
rror(" \n");
clearerr(stream);}
}
int cess(char *pathname, int mode),
: pathname mode. mode :
06 - ;
04 - ;
02 ;
00 - .
0, -1, . errno :
EACCES - ;
ENOENT - ..
.
#include<io.h>
main()
{ FILE *fn;
/* */
if ((cess("data",2))== -1) {
rror(" ");
exit(1); }
else
fn = fopen("data", "a");}
.
1. .
2.
FILE *rndfopen(char* path),
path . - . , , ( , ). , . FILE, , NULL . fopen cess.
3. rndfopen, , . ѳ.
. , " l" :
if(fseek(p,l, SEEK_SET))
rror("\n H !");
/* */
#include <stdio.h>
FILE *rndfopen(char*); /* */
char s[10], n[15];
main()
{long l, ll; int ,ii;
FILE *p;
printf("\n ?");
gets(n); /* */
if(NULL==(p=rndfopen(n))) /* */
{rror("\n .");
exit(1);} /* 1 */
while(1)
{ printf("\n?");
gets(s); /* - */
l=atol(s); -
l*/
< l.>
< ll.>
< ii.>
printf("\n %ld %c (%d).",ll,ii,ii);
printf("\n (/n)?");
if(getche()=='y')
{printf("\n ?");
c=getche(); /* . */
< l. >
< c ,>
printf("\n (/n)?");
if(getche()=='n')break;
}
< .>
}
FILE *rndfopen(char *n)
{ <ҳ .> }
4. ϳ :
) ra.dat .
' ? ra.dat
? 0
0 (-1)
(/n)? n
(/n)?
? 3
3 (-1)
(/n)?
? 4
(/n)
? 3
3 4 (52)
(/n)? n
(/n)
? 0
0 ²__0
(/n)?
? 1
(/n)
? 0
0 1 (49)
(/n)? n
(/n) n
ra.dat 4- :
'1' ²__1 ²__2 '4'
) . ra.dat. .
' ? ra.dat
? 0
0 1 (49)
(/n)? n
(/n)?
? 3
3 4 (52)
(/n)?
? 0
(/n)
? 5
5 (-1)
(/n)?
? 6
(/n)
? 4
4 ²__4
(/n)? n
(/n)
? 1
1 ²__1
(/n)?
? 2
(/n) n
ram.dat 6- :
'1' '2' ²__2 '0' ²__4 '6'.
1. -? ?
2. ?
5. , ?
6. ?
7. fopen?
8. ?
9. / ?
10. ?
11. ?
12. fgetc fputc ?
13. ?
15. ѳ- ?
17. ?
18. cess?
19. ra.dat ' ?
4.