.
; . .
, const (, int const int int int &).
, :
- , ;
- , ;
- ( ).
++ , , , , . #include , . .
: /, , , ..
/
/ ++ , , ++. , sync_with_stdio ().
/ <stdio.h> <cstdio>. / . , , .
. / . :
FILE *fopen(const *filename, cnst char *mode);
FILE , − NULL. − -, − :
"r" − ;
"w" − ( , );
"a" − ;
"r+" − ( );
"w+" − ;
"a+" − .
"t" ( ) "b" ( ).
|
|
FILE *f = fopen("d:\\users\\data.dat", "rb+");
f . / .
/
/ : , . .
/ , , . ( ) /. ftell fgetpos fseek fsetpos:
int fgetpos (FILE *f, fpos_t *pos);
f pos. fpos_t.
int ftell (FILE *f);
f .
int fseek (FILE *f, long off, int org);
f off, org, :
SEEK_CUR − ;
SEEK_END − ;
SEEK_SET − .
int fsetpos (FILE *f, const fpos_t *pos);
f *pos, fgetpos.
fread fwrite:
size_t fread (void *buffer, size_t size, size_t count, FILE *stream);
, count, .
size_t fwrite (void *p, size_t size, size_t n, FILE *f);
n size , p f. .
getc fgetc; stdin − getchar:
int getc (FILE *f);
int f. , EOF ( ).
int fgetc (FILE *f);
, .
int getchar (void);
int . , EOF.
− putc, fputc; stdout − putchar.
int putc (int ch, FILE *f);
ch f. EOF, − .
int fputc (int ch, FILE *f);
.
int putchar (int ch);
ch ; , ch, EOF.
− fgets; stdin − gets:
char * gets (char *s);
s. s.
char *fgets (char *s, int n, FILE *f);
n-1 f s, . , . ('\0'). NULL, − s.
|
|
− fputs; stdout − puts.
int fputs (const char *s, FILE *f);
s f. . EOF, − .
int puts (const char *s);
s , . EOF − .
− fscanf; stdin − scanf, − sscanf.
int fscanf (FILE *f, const char *fmt [, par1, par2,...]);
par1, par2,... , fmt f. , .
int scanf (const char *fmt [, par1, par2,...]);
par1, par2,... , fmt . , .
int sscanf (const char *buf, char *format [, par1, par2,...]);
, ; format − , .
− fprintf; stdout − printf; − sprintf.
int fprintf (FILE *f, const char *fmt,...);
f , , fmt. .
int printf (const char *fmt,...);
, , , fmt.
int sprintf (char *buffer, const char *format [, argument,...]);
buffer format.
, fclose:
int fclose (FILE *);
feof ferror:
int feof (FILE *) − , ; .
int ferror(FILE *) − , /; .
. , mon . . (20 ), ( 5 ), 40 :
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
# include <string.h>
int main () {
FILE *fi, *fo;
if ((fi=fopen("d:\\users\\file.txt", "r")) = =0) {
cout << " "; return 1;}
if ((fo=fopen("d:\\users\\binfile.out", "w+b")) = =0) {
cout << " "; return 1;}
const int dl=80;
char s[dl];
struct {
char type[20];
int opt, rozn;
char comm[40];
} mon;
int kol =0; //
while (fgets(s, dl, fi)) {
// s mon
strncpy (mon.type, s, 19); // 20 s mon.type
mon.type[19]='\0';
mon.opt=atoi(&s[20]); //
mon.rozn=atoi(&s[25]);
strncpy(mon.comm, &s[30], 40); //
//
fwrite(&mon, sizeof mon,1, fo);
kol++;}
fclose (fi);
fclose (fo);
rerurn 0;
}
. :
|
|
- ;
- ;
- ;
- ;
- ;