.


:




:

































 

 

 

 


. . .

:

:

  • : , ( c );
  • : , ( );
  • : , ;
  • : , ( );
  • : , ( );

- , .

/ :

;

/ ( ), (, );

( ).

() .

() :

o ( ).

o , - .

:

o .

o , -1 .

o () () , , .

o (stderr, stdin, stdout).

o , .

, , ( ), . FILE:

FILE *fp;

( !):

.

(/).

.

.

:

.

.

:

FILE *fopen(char *name, char *mode);

name ,

mode :

mode ...
"r" . , fopen . ...
"w" . , . , . ...
"a" . , . ...  
"r+" ( ). . ...
"w+" . , . ...
"a+" . , . ...  

:

mode
t ( , )
b

:

o , \n (Unix) \r, \n (Windows);

o (\n)

:

o .

o / .

:

int fclose(FILE *file_ptr);

(return 0), . ( ).

/ , . fclose() . .

fopen() NULL.

 

errno (<errno.h>).

#include <stdio.h>

#include <stdlib.h>

#include <errno.h>

...

int main()

{

FILE *f = fopen("fil nam .txt", "rt");

if (f == NULL)

{

printf(" %d\n, errno);

exit(1);

}

else

{

//

}

fclose(f);

return 0;

}

, .

1. feof()

int feof(FILE *file_ptr);

 

feof() , . -.

( ) ,

i = 0;

while (!feof(fp))

{

fgets(buf, sizeof(buf), fp);

printf ("Line %4d: %s", i, buf);

i++;

} //

2. , .

#include <stdio.h>

#include <stdlib.h>

#define MYFILE junk1.txt

int main(void)

{

FILE *fp;

char buf[BUFSIZ] = "Garbage";

int i;

if ((fp = fopen(MYFILE, "r")) == NULL)

{

return (EXIT_FAILURE);

}

i = 0;

while (fgets(buf, sizeof(buf), fp)!= NULL)

{

printf ("Line %4d: %s", i, buf);

i++;

}

fclose(fp);

return(0);

}

int ftell (FILE * stream); . -1L. .
int fseek(FILE *stream,int offset,int origin); offset , ORIGIN: SEEK_SET ; SEEK_END ; SEEK_CUR . . "0", .
void rewind (FILE * stream); ( ). , . : fseek(stream, 0L, SEEK_SET);
int fflush(FILE *stream); , .  

.

...

intmain ()

{

FILE * ptrFile = fopen("file.txt","rb");

if (ptrFile == NULL)

exit(1);

else

{

fseek (ptrFile, 0, SEEK _ END); //

long size = ftell(ptrFile); //

fclose (ptrFile); //

}

return 0;

}

 

 



<== | ==>
. . . | -. .
:


: 2018-11-11; !; : 361 |


:

:

, .
==> ...

1809 - | 1688 -


© 2015-2024 lektsii.org - -

: 0.018 .