[̲] [] [] [ ]

 

7.9.

 

, , : , , . , , - ' , . . , , , , . ( ) . - .

t

 

type tf = file of t;

 

t - ( t - ), , , :

 

var F: tf

var F: file of t.

 

:

 

const D_Name = 80; D_Num = 7;

type StrF = string[D_Name]; StrN = string[D_Num];

Phone = record

Num: StrN;

Name: StrF

end;

var FL: file of Phone.

 

, , , - . 0.

F - t, Str - , - t, n - . :

 

assign(F, Str) F , ' Str. , ' ' Rez.dat, assign(,'Rez.dat');

 

rewrite(F) - . , assign. ', . 0;

 

reset(F) - F. ;

 

read(F, ) - F , , p. ϳ ;

 

write(F, ) - . F . , p . ϳ ;

 

seek(F, n) - F n ( 0);

 

close(F) - F. , ;

 

:

 

eof(F) , ;

 

filesize(F) - . , F. filesize(F)=0, , ;

 

filepos(F) , 0.

 

, seek (F, filesize(F)).

 

, :

 

- assign;

- rewrite;

- write;

- close;

 

:

 

- assign;

- reset;

- read seek ( );

- ;

- close;

 

, , :

 

- assign;

- reset;

- ;

- write;

- close.

 

7P. . , , ,

 

program Ref_Book;

const D_Name = 80; D_Num = 20;

type StrF = string[D_Name]; StrN = string[D_Num];

Phone = record

Num : StrN;

Name: StrF

end;

FOP = file of Phone;

var FL: FOP; R: byte; FName: string;

{ ***** ***** }

procedure WriteToFile (var F: FOP);

var A: Phone; Yes: char;

begin

repeat

write(' : ?'); readln(A.Num);

writeln(' :');

readln(A.Name); write(F,A);

write(' [Y/N]: ?'); readln(Yes)

until (Yes = 'N') or (Yes = 'n');

writeln; close(F)

end;

{ ***** ***** }

procedure FillFile (var F: FOP);

begin

rewrite(F); WriteToFile(F)

end;

{ ***** ***** }

procedure AppFile (var F: FOP);

begin

reset(F); seek(F,filesize(F)); WriteToFile(F)

end;

{ ***** ***** }

procedure SearchPhone (var F: FOP);

var P: boolean; N: StrF; A: Phone;

begin

reset(F); P:= False;

write(' : ');

readln(N); writeln;

while not eof(F) and not P do begin

read(F,A);

P:= A.Name=N

end;

if P then writeln('*** *** ',A.Num)

else writeln(' ',N,' !!!');

close(F);

end;

{ ***** ***** }

procedure ReplacePhone (var F: FOP);

var P: boolean; N: StrF; T: StrN;

A: Phone;

i,m: longint;

begin

reset(F);

write(' : ');

readln(N);

write(' : ');

readln(T);

writeln;

m:=filesize(F); i:=0; P:= False;

while (i<m) and not P do begin

i:=i+1;

read(F,A);

P:= A.Name=N

end;

if P then begin

seek(F,i-1); A.Num:=T; write(F,A);

writeln('*** *** ')

end

else writeln(' ',N,' !!!');

close(F);

end;

{ ***** ***** }

begin

write(''' : ?'); readln(FName);

assign(FL,FName);

repeat

writeln(' :');

writeln(' 1 : ');

writeln(' 2 : ');

writeln(' 3 : ');

writeln(' 4 : ');

writeln(' 5 : ');

write(>); readln(R);

case R of

1: FillFile(FL);

2: AppFile(FL);

3: SearchPhone(FL);

4: ReplacePhone(FL);

end

until R = 5;

end.

 

, - .

 

:

 

var F: text;

 

:

assign;

reset;

rewrite;

close;

eof.

. .

:

 

read(F, x1, x2, ..., xn) F x1, x2, ..., xn;

 

readln(F, x1, x2, ..., xn) F x1, x2, ..., xn ( , );

 

write(F, e1, e2, ..., en) F e1, e2, ..., en;

 

writeln(F, e1, e2, ..., en) F e1, e2, ..., en ( );

 

read, readln, write, writeln , , , #, # . г , (>1), , ( ). . , . . , :

 

var X: real; N: integer;

C: char; B: boolean;

S: string[3];

 

, F :

 

^2.67ÿÿ#ÿÿÿ-356ÿabcÿ#ÿ12

 

ÿ , ^ - , # - .

 

read(F,X,N,C,S)

 

: X=2.67, N=-356, C= , S=abc. :

 

2.67ÿÿ#ÿÿÿ-356ÿabc^ÿ#ÿ12

 

 

readln(F,X,N,C,S)

 

,

 

2.67ÿÿ#ÿÿÿ-356ÿabcÿ#^ÿ12

 

, , . . .

. : X=2.67, N=-356, C= , S=abc, B=true.

 

write(F,X,N,C,S,B)

 

:

 

2.6700000000E+00-356ÿabcTRUE^

 

 

writeln(F,X,N,C,S,B)

 

:

 

2.6700000000E+00-356ÿabcTRUE#^

 

, write writeln, ,

 

writeln(F,X, ,N)

 

:

 

append(F) ;

 

eoln(F) , .

 

: input output. input , output . assign, reset, rewrite, close. , read, readln, write, writeln , input output. ,

 

readln(input,x); writeln(output,y)

 

 

readln(x); writeln(y)

 

, , , input output.

 

8P. c A. ( 7.18)

 

Program ReplaceChars;

procedure ReplaceText (c: char; A: string; var F,G: text);

function Replace(S, A: string; c: char): string;

var S1: string; i: word;

begin

S1 := ;

for i:=1 to length(S) do

if S[i] = c then S1 := S1+A

else S1 := S1+S[i];

Replace := S1

end;

var S, S1: string;

begin

reset(F); rewrite(G);

while not eof(F) do begin

readln(F,S);

S1 := Replace(S,A,c);

writeln(G,S1)

end;

close(F); close(G)

end;

 

var F, G: text;

FN, GN, A: string;

c: char;

begin

write(쒒 : );

readln(FN); assign(F,FN);

write(쒒 : );

readln(GN); assign(G,GN);

write( :); readln(c);

write( :); readln(A);

ReplaceText(c,A,F,G)

end.

 

ѳ , . FILE, :

 

FILE *fp;

 

ѳ, . 0.

fp - t, str - , - t, n - . :

 

fp = fopen(str,mode) fp , ' str, , mode. mode : rb, wb, ab ; r+b, w+b, a+b . r ; w ; a . + , . ,

 

fp = fopen(rez.dat, wb);

fp = fopen(mod.dat, r+b);

 

fread(&, sizeof(p), 1, fp); - fp , , p. ϳ ;

 

fwrite(&, sizeof(p), 1, fp); - . fp . , p . ϳ ;

 

fseek(fp, n*sizeof(t), SEEK_SET) - fp n ( 0). SEEK_SET, , SEEK_CUR SEEK_END ;

 

fclose(fp) - fp. , ;

 

feof(fp) , . , , , fread;

 

ftell(fp) , 0. ftell , , filepos . , , : ftell(fp)/sizeof(t).

 

, :

 

- fopen(str, wb);

- fwrite;

- fclose;

 

:

 

- fopen(str, rb) ( fopen(str, r+b));

- fread fseek ( );

- ;

- fclose;

 

, , :

 

- fopen(str, ab);

- fwrite;

- fclose.

 

7. . , , ,

 

#include <stdio.h>

#include <string.h>

 

/* RefBook */

 

#define FALSE 0

 

typedef struct {

char name[30];

char tel[20];

} phone;

char fname[81];

 

fillfile()

{

phone p;

int i,n;

FILE *fp;

 

fp=fopen(fname,"wb");

printf("\n n:");

scanf("%d",&n); getchar();

for (i=1;i<=n;i++)

{

printf(" %d:",i);

gets(p.name);

printf(" %d:",i);

gets(p.tel);

fwrite(&p,sizeof(p),1,fp);

}

fclose(fp);

}

 

appfile()

{

phone p;

FILE *fp;

 

fp=fopen(fname,"ab");

printf(" :");

gets(p.name);

printf(" :");

gets(p.tel);

fwrite(&p,sizeof(p),1,fp);

fclose(fp);

}

 

findtel(char s[], char t[])

{

phone p;

FILE *fp;

int b;

 

fp=fopen(fname,"rb");

b=FALSE; strcpy(t,"");

do

{

fread(&p,sizeof(p),1,fp);

if (!feof(fp) && (b=strcmp(p.name,s)==0)) strcpy(t,p.tel);

}

while (!feof(fp) && !b);

fclose(fp);

}

 

changetel(char s[], char t[])

{

phone p;

FILE *fp;

char t1[20];

int b;

long pos;

 

fp=fopen(fname,"r+b");

b=FALSE;

do

{

fread(&p,sizeof(p),1,fp);

if (!feof(fp)) b=strcmp(p.name,s)==0;

}

while (!feof(fp) && !b);

if (!b) printf(" , ");

else

{

pos=ftell(fp);

fseek(fp,pos-sizeof(p),SEEK_SET);

strcpy(p.tel,t);

fwrite(&p,sizeof(p),1,fp);

}

fclose(fp);

}

 

main()

{

char s[30],t[20],c;

int k;

 

printf(" : "); gets(fname);

do

{

printf("\n (1-5):");

scanf("%d",&k); getchar();

switch (k)

{

case 1: fillfile(); break;

case 2: appfile(); break;

case 3: printf("\n :");

gets(s);

findtel(s,t);

if (strlen(t)>0) printf("\n: %s",t);

else printf ("\n ");

break;

case 4: printf("\n :");

gets(s);

printf("\n :");

gets(t);

changetel(s,t);

break;

}

}

while (k != 5);

}

 

, , - .

 

ѳ , . , , fopen:

 

fp = fopen(str,mode)

 

mode : r, w a. r ; w ; a . ,

 

fp = fopen(rez.txt, w);

fp = fopen(mod.txt, r);

 

:

fclose;

feof.

 

:

 

fscanf(fp, format, &x1, &x2, ..., &xn) fp x1, x2, ..., xn. format , scanf;

 

c=fgetc(fp) fp c;

 

fgets(str, n, fp) fp (n-1) str. (n-1) , ;

 

fprintf(fp, format, e1, e2, ..., en) F e1, e2, ..., en. format , printf;

 

fputc(c,fp) fp c;

 

fputs(str,fp) fp str.

 

ϳ fscanf, fprintf read, write . (\n) fprintf str fputs.

ѳ, , : . , . fopen fclose. : scanf, getchar, gets, : printf, putchar, puts.

 

8. c A. ( 7.18)

 

# include <stdio.h>

# include <string.h>

 

/* ReplaceChars */

 

#define MAXLEN 256

#define MAXREPL 10

 

char fn[81], gn[81];

 

replace(char s[], char a[], char c, char r[])

{

unsigned i;

 

strcpy(r, );

for (i=0; i<strlen(s); i++)

if (s[i] == c) strcat(r,a);

else

{

r[strlen(r)+1] = \0;

r[strlen(r)] = s[i];

}

}

 

replace_text (char c, char a[])

{

FILE *fp, *gp;

char s[MAXLEN], s1[MAXLEN*MAXREPL];

 

fp = fopen(fn, r);

gp = fopen(gn, w);

while(1)

{

fgets(s,MAXLEN,fp);

if (feof(fp)) break;

replace(s,a,c,s1);

fputs(s1,gp);

}

fclose(fp); fclose(gp);

}

 

main()

{

char c, a[MAXREPL];

 

printf( : ); gets(fn);

printf( : ); gets(gn);

printf( :); gets(a);

printf( : ); c=getchar();

printf(\n);

replace_text(c,a);

}

 

[̲] [] [] [ ]