.


:




:

































 

 

 

 





/

, : ' ' FILE, stdio.h ( /, , ..). ' fopen (), stdio.h FILE. , , FILE, :

FILE *fp;

fp = fopen(< _ >, < __ >);

fopen () : ' . , : , .

:

- r - ;

- w - ( , );

- a - ( , );

- r+ - : , ;

- w+ - : , ;

- a+ - ( , ).

(, r+ ' ), fopen () NULL. :

If ((fp = fopen((< _ >, < __ >) == NULL) { } { }

, , "'" FILE , , . fclose (fp). ' , ' /, /. - 䴻, "", " '". , . .

, , . .

fputc()

:

fputc(<,____>, __);

: __, fp.

/* KTOD: . */

# include <stdlib.h>

# include <stdio.h>

# include <windows.h>

# include <locale.h>

 

int main() {

setlocale(LC_CTYPE, "Russian");

FILE *fp;

char ch;

if((fp=fopen("test", "w"))==NULL) {

printf(" ii .\n");

system ("pause");

exit(1);

}

printf("i i. i ii i i $ [Enter] \n");

do {

ch = getchar();

fputc(ch, fp);

} while (ch!= '$');

fclose(fp);

system ("pause");

return 0;

}

 

fgetc()

:

char c = fgetc(fp);

fp. EOF.

/* DTOS: , . */

# include <stdlib.h>

# include <stdio.h>

# include <windows.h>

# include <locale.h>

 

int main() {

setlocale(LC_CTYPE, "Russian");

FILE *fp;

char ch;

if((fp=fopen("test", "r"))==NULL) {

printf(" ii .\n");

system ("pause");

exit(1);

}

printf(" \n");

ch = fgetc (fp); /* */

while (ch!= EOF) {

putchar (ch); /* */

ch = fgetc (fp);

}

printf("\n");

fclose(fp);

system ("pause");

return 0;

}

 

feof()

:

feof(fp);

, , fgetc () EOF. , fgetc (), , , . -, , . , , , ' , EOF. , , . -, fgetc () EOF , , . fgetc (), , . feof (), , . feof () :

int feof(FILE < >);

, feof () true (), . , :

while(!feof(< >)) ch = fgetc(< >);

, , .

, , feof (). , feof () ߹, .

/* . */

# include <stdlib.h>

# include <stdio.h>

# include <windows.h>

# include <locale.h>

 

int main() {

setlocale(LC_CTYPE, "Russian");

FILE *in, *out;

char ch;

if((in=fopen("testr", "rb"))==NULL) {

printf(" i i .\n");

system ("pause");

exit(1);

}

if((out=fopen("testw", "wb"))==NULL) {

printf(" i i.\n");

system ("pause");

exit(1);

}

/* . */

while (! feof (in)) {

ch = getc (in);

if (!feof (in)) fputc (ch, out);

}

fclose (in);

fclose (out);

system ("pause");

return 0;

}

 

߹ fp: ( ). , , rewind () - "" , . . , , - .

fputs()

:

fputs(< >, <__>);

.

#include <stdlib.h>

#include <stdio.h>

#include <windows.h>

#include <locale.h>

#include <string.h>

 

int main() {

setlocale(LC_CTYPE, "Russian");

char str [80];

FILE *fp;

if ((fp = fopen ("TEST", "w")) == NULL) {

printf (" ii . \ n");

system ("pause");

exit(1);

}

do {

printf ("i (i - ): \n");

gets (str);

strcat (str, "\n"); //

fputs (str, fp);

} while (strcmp("\n", str)); // strcmp() -

fclose (fp);

system ("pause");

return 0;

 

fgets()

:

fgets(s, maxline, fp);

s, s - char ( ' ), maxline - , fp. NULL.

fgets () , , maxline. , .

 

# include <stdlib.h>

# include <stdio.h>

# include <windows.h>

# include <locale.h>

# include <string.h>

 

int main() {

setlocale(LC_CTYPE, "Russian");

char str [80];

int i=0;

FILE *fp;

if ((fp = fopen ("test", "r")) == NULL) {

printf (" ii . \n");

system ("pause");

exit(1);

}

while (! feof (fp)) {

i++;

printf (" i %d- ", i);

fgets (str, 79, fp);

printf (str);

}

printf ("\n");

fclose (fp);

system ("pause");

return 0;

}

 

fread()

:

fread(buf, m, n, fp);

fp n , m . , buf, , char buf [50] char * buf ( ' ). (n * m). , NULL.

fwrite()

:

fwrite(ptr, m, n, fp);

n fp, m . , ptr ( ', , ). (n * m). , - .

, fread () fwrite () - . , double, int long, a . , sizeof ().

/*

. */

# include <stdlib.h>

# include <stdio.h>

# include <windows.h>

# include <locale.h>

# include <ctype.h>

 

int main() {

setlocale(LC_CTYPE, "Russian");

FILE * fp;

float d = 12.23;

int i = 101;

long l = 123023L;

if ((fp = fopen ("test", "wb+")) == NULL) {

printf (" . \n");

system ("pause");

exit (1);

}

fwrite (&d, sizeof (double), 1, fp);

fwrite (&i, sizeof (int), 1, fp);

fwrite (&l, sizeof (long), 1, fp);

rewind (fp);

fread (&d, sizeof (double), 1, fp);

fread (&i, sizeof (int), 1, fp);

fread (&l, sizeof (long), 1, fp);

printf ("%4.2f %d %ld", d, i, l);

printf ("\n");

fclose (fp);

system ("pause");

return 0;

}

, ( ) ', . , fread () fwrite (), . , .

fread () fwrite () , . ,

struct struct_type {

float balance;

char name [80];

} Cust;

cust , fp:

fwrite (& cust, sizeof (struct struct_type), 1, fp);





:


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


:

:

,
==> ...

1550 - | 1507 -


© 2015-2024 lektsii.org - -

: 0.049 .