.


:




:

































 

 

 

 





 

. .

 

() . ', . .

', , . ' - . ' , . , , , . . .

, . ' , . .

- . : . . / , .

: '\r'( ) '\n' ( ), 13 10, . , . . . 128, ' (16- 00 80), '1','2' '8'. , , .

, '\r' '\n'.

. , . , /. , . , , , : , , , г . , , '., 128 '\x00' '\x80'. , . .

, . . ̳ , /, /, . ϳ . , . . - .

/ , fscanf fprint. . : close ( ), lseek ( ), open ( ), read ( ), write ( ). io.h.

. / .

open. , . open , , .

, , fcntl.h. types.h stat.h .

open

 

int open (char *pathname, int oflag[, int pmode]),

 

pathname - ' , , flag - . oflag , , fcntl.h ( , (|)):

O_APPEND - ;

O_CREAT - , , ;

O_EXCL - -1, pathname ; O_CREAT;

O_RDONLY - ; , O_RDWR, O_WRONLY ;

O_RDWR - ; , O_RDONLY, O_WRONLY ;

O_TRUN - ; . ;

O_WRONLY - ; , O_RDONLY, O_RDWR ;

O_TEXT - ( -/- (CR/LF) (LF) ; LF CR/LF . Ctrl-Z .);

O_BINARY - . ( . Ctrl-Z .).

pmode , O_CREAT. , pmode . pmode , . Pmode , S_IWRITE S_IREAD, stat.h. , (|). mode :

S-IWRITE - ;

S-IREAD - ;

S-IREAD | S-IWRITE - .

,

-1 . errno :

EACCES - ' , , ;

EEXIST - O_CREAT O_EXCL , ;

EMFILE - handle ( );

ENOENT - ' .

.

 

#include <fcntl.h>

#include <types.h>

#include <stat.h>

#include <io.h>

#include <stdlib.h>

main()

{ int fn1, fn2;

fn1 = open("data1", O_RDONLY);

if (fn1 == -1) rror(" ");

fn2 = open("data2, O_WRONLY|O_TRUNC|O_CREAT, S_IREAD|S_IWRITE);

if (fn2 == -1) rror(" ");

}

rror, stderr , ' errno.

³ , . int close(int handle), handle - , .

0, , -1, handle . errno EBADF - , .

.

 

#include <io.h>

#include <fcntl.h>

main()

{ int fh;

fh = open("data",O_RDONLY);

close(fh);}

 

lseek / , .

 

long lseek(int handle, long offset, int origin)

 

handle - , origin offset :

SEEK_SET - ;

SEEK_CUR - ;

SEEK_END .

, .

. -1L, errno :

EBADF- (. )

EINVAL - origin, , offset, , .

.

 

#include <io.h>

#include <fcntl.h>

#include <stdlib.h>

main()

{ int fh; long sition;

fh = open("data", O_RDONLY);

sition = lseek (fh, 0L, SEEK_SET);

if (sition == -1L) rror(" ");

/* */

sition = lseek (fh, 0L, SEEK_CUR);

if (sition == -1L) rror (" ");

sition = lseek(fh, 0L,SEEK_END);

if (sition == -1L) rror(" ");}

 

read write. ֳ 䳿 , . write, :

 

int write(int handle, char *buffer, unsigned count),

 

handle - , count buffer.

ϳ . (APPEND), .

. , , , count. -1 , error :

EACCES - ;

EBADF - ;

ENOSPC - '.

.

 

#include <io.h>

#include <stdio.h>

#include <fcntl.h>

char buffer[30000];

main()

{ int fh; unsigned nbytes = 30000;

int byteswritten;

if ((fh = open(":/data/cont.dat",O_WRONLY)) == -1) {

rror(" ");

exit(1); }

if ((byteswritten = write(fh, buffer, nbytes)) == -1){

rror(" ");

else printf(" %d \n", byteswritten);

}

read :

 

int read (int handle, char * buffer, unsigned count);

 

handle - , count buffer. . ϳ .

, count, . , , , . , -/_ (LF) (LF), , .

0. -1 errno : EBADF (. )

 

#include <io.h>

#include <stdio.h>

#include <fcntl.h>

char buffer [60000];

main ()

{int fh;

unsigned int nbytes = 60000, bytesread;

if ((fh = open ("conf.dat", O_RDONLY)) == -1) {

rror (" ");

exit (1); }

if ((bytesread = read (fh, buffer, nbytes)) == -1) rror (" ");

else printf (" %u \n", bytesread);

}

 

int chsize(int handle, long size),

 

, ' handle , size. . , .

0, -1 errno :

EACCES - .;

EBADF - (. ).

ENOSPC - .

.

 

#include <io.h>

#include <fcntl.h>

#include <types.h>

#include <stat.h>

#define SIZE 32768L

main()

{int fn, result;

fn = open("data", O_RDWR|O_CREAT, S_IREAD|S_IWRITE);

result = chsize (fn, SIZE); }

 

. - . . , , , .

, , 512 . ᒺ . .

, : - 14 - int -2 . rcrdsz =16 .

512 . 0- 0-, 1-... 31- . 1- 32-, 33-... 63- . , 512 .

i- . ³ bl=i*rcrdsz/512 ( ) n=i*rcrdsz%512. buf , bl - , k=buf+n i- buf. k buf char*.

. :

struct _rec {char name[14]; /* */

int gr;}; /* г */

 

_rec . 16-, , , . ³ , , ᒺ union. :

 

union _bufrec {char bufrec[16];

struct _rec rec;} rbuf;

 

rbuf,bufres - 16- , rbuf.rec, rbuf.rec.name () rbuf.rec.gr ( ). : rbuf.rec.name rbuf.rec.gr rbuf.rec 16- rbuf.bufrec.

, , . AFILE.

 

typedef struct afile {

char *buf; /* */

int bz; /* , , -1*/

int w; /* buf , w=1 w=0*/

int rcrdsz; /* */

int numrec; /* ʳ */

int handler; /* */}

AFILE;

 

, , AFILE* .

i- bl=i*rcrdsz/512 ( ) n=i*rcrdsz%512. (bl==ap->bz). , (ap->w==1). , ap->bz - , ap->buf ap->bz*512 - . ( bl!=ap->bz) ap->buf bl - , 512 , 512* bl - , ap->bz=bl ap->w=0. ϳ , n - .

: .

 

.

 

1. . .

2. pp :

2.1.

 

int reate (char* name, int rcrdsz, int numrec),

 

name - ' , rcrdsz - , numrec - .

.

open ' name. , reate -1.

open, name .

2 write, 4 .

4+rcrdsz*numrec ( chsize).

( close).

2.2.

 

AFILE *aopen(char *name),

 

name - ' .

, .

' AFILE

 

AFILE *ap=(AFILE*)malloc(sizeof(AFILE)).

 

, :

- 512 ap->buf=(char*)malloc(512);

- open name ap->handler;

- read , ap->rcrdsz ap->numrec;

- ap->w 0;

- ap->bz -1.

AFILE.

2.3.

 

int areadwrite(AFILE *ap,int nr, char* rec, int mode),

 

- , , , nr - , , rec , - , mode - (0 - , 1 - ).

䳿.

bl=nr*rcrdsz/512, nr n= nr *rcrdsz%512.

nr (bl==ap->bz). , (ap->w==1). , ap->bz - , , lseek write, ap->buf 4+ap->bz* 512- . ( bl!=ap->bz) :

- ->buf bl- , lseek read, 512 , 4+512*bl - ;

- ap->bz=bl ;

- ap->w =0. nr ap->buf, n - mode;

- (mode=0), :

 

for(i=0;i<rcrdsz;i++)rec[i]=(ap->buf+n)[i].

 

- (mode=1) :

for(i=0;i<rcrdsz;i++)(ap->buf+n)[i]=rec[i]

 

ap->w=1.

 

2.4.

 

int lose(AFILE *ap),

 

- AFILE, , .

.

ap->buf (ap->w==1), - (4+ap->bz*512) . ap->handler.

free ', ap->buf, ap->handler ' AFILE, .

3. main

 

typedef struct afile {

char *buf;

int bz;

int w;

int rcrdsz;

int numrec;

int handler;} AFILE;

#include <stdio.h>

#include <io.h>

#include <fcntl.h>

#include <stat.h>

#define R 0

#define W 1

struct _rec {char name[14]; int gr;};

int reate (char*, int, int); /* */

AFILE *aopen(char *) *ap;

int areadwrite(AFILE*, int, char*, int);

int lose(AFILE *ap); /* */

main()

{ int i, nr;

char s[13],c,c1;

union _bufrec {char bufrec[16]; /**/

struct _rec rec;} rbuf; /* r */

printf("\n ' \

(100 16 ): ");

gets(s);

if(-1==acreate(s,16,100))

printf("\n %s!",s);

ap=aopen(s); /* ³ */

do { /* */

for(i=0;i<16;i++)rbuf.bufrec[i]='\0'; /* */

puts("\nHp ?");

scanf("%d",&nr);

areadwrite(, nr, rbuf.bufrec, R); /* */

/* */

printf("\n #%d. :\n =\" %s\". \

г = %d",nr,rbuf.rec.name,rbuf.rec.gr);

puts(" \n / (/n)?");

c=getche();

if(c=='y')

{puts("\n?"); /* */

scanf("%s",rbuf.rec.name);

puts("\nг ?");

scanf("%d",&rbuf.rec.gr);

areadwrite(, nr, rbuf.bufrec, W); /* */

}

puts("\n (/n)?");

c1=getche();

}

while(c1=='y');

lose(); /* */

}

rbuf.

 

.

 

1. ?

2. ?

3. ?

4. .

5. ?

6. UNIX MS DOS?

7. /?

8. ?

9. / ?

10. () () ()?

11. ?

12. ?

13. .

14. .

15. .

16. ?

17. AFILE ?

18. ?

6.





:


: 2016-09-06; !; : 356 |


:

:

- , - .
==> ...

784 - | 762 -


© 2015-2024 lektsii.org - -

: 0.179 .