Вы находитесь на странице: 1из 14

12.

,
.

( ) , .
()
. :
struct _
{
};
. :
struct ANKETA
{ char fio[20];
int gr;
char adr[40];
};

/* */
/* */
/* */

struct BOOK
{ char author[40];
char name [50];
int year;
int pages;
};

/* */
/* */
/* */
/* */

ANKETA

BOOK

- ,

. .
C :
struct

_;

:
1

struct
struct
struct
struct
struct

ANKETA a1;
BOOK b1,b2;
BOOK mb[100]; /* 100 BOOK */
ANKETA *p1;
/* ANKETA */
BOOK
*p2 = &b2; /* BOOK,
b2
*/

C++
struct , :
ANKETA a1;
BOOK
b1,b2;

.
a1

b1

b2

fio
gr
adr

author
name
year
pages

author
name
year
pages

, :
struct BOOK b3 = { .., , 1995, 1650};
.
:
b1=b3;
mb[0]=b3;
. ,
/ .

()

. -> :
_ . _
_ -> _
2

12.1.
/* b2 BOOK*/
gets(b2.author);
gets (b2.name);
scanf(%d%d, &b2.year, &b2.pages);
12.2.
struct ANKETA a2;
strcpy(a2.fio, ..);
2.gr = 1980;
strcpy(a2.adr, ., .10, .5);
12.3.
struct BOOK *p3 = &b3;
printf (%s, %s, %d ., %d .\n, p3->author, p3->name,
p3->year, p3->pages);
:
.., , 1995 ., 1650 .

, .
, :
struct
{ char fio[20];
int gr;
char adr[40];
} a1, a2, *p1;

, , :
struct BOOK *p4;
p4 = (struct BOOK *) malloc (sizeof (struct BOOK));
;
p4, :
gets (p4->name);

12.4.
/* ,
*/
#include <stdio.h>
#include <string.h>
struct MONTH
{ char *name; /* */
int days;
/* */
};
int main()
{
struct MONTH year[12] =
{ {, 31},
{, 28},
{, 31},

{, 31}
};
char m[9]; /* */
int n = 0; /* . */
int i;
/* . - year */
puts (\n );
gets (m);
for (i=0; i<12 && strcmp (m, year[i].name)); i++)
n += year[i].days;
if (i==12) puts ( );
else printf ( : %d, n );
return 0;
}


, -
, , , , .
().
,
. MS DOS
4

.
. :
( 13) ( 10).
.
exe-,
.
, , (
, int).

fopen().
FILE,
. FILE
stdio.h.

FILE ,
fopen().
:
fscanf() ,
fgets() ,
fgetc() ,
fread() ().
:
fprintf() ,
fputs() ,
fputc() ,
fwrite() ().
:
, ,
.

. ()
5

. ,
()

fseek().
.

fclose().

1. fopen .

:
FILE * fopen (char * fname, char * mode);
fname ,
. mode
:
r ,
w ( , ),
a ,
r+ .
,
.
.
t
( ) b ( ). , rb+ ,
.
.


6

( ).
.
.
fopen() FILE
.

NULL.
, .
,
/.
, .
.
.

.

.

12.5.
FILE *fout, *fmod; /* */
char fname[13];
/* . */
fout = fopen (f1.txt, w);
puts( );
gets (fname);
if ((fmod = fopen(fname, r+) ==NULL)
{ puts ( );
exit (1);
}
2. fclose .

.
7

12.6.
fclose (fout);
fclose (fmod);

. .
3. fgets .

:
char * fgets (char *s, int n, FILE *f);
,
f, s. n
:

(n-1) .

(\0). gets()
( ).
s NULL
.
12.7.
char str[81];
FILE *f;

while (fgets(str,81,f))
{ /* */
.
.
.

}
4.

fputs .
:

int fputs (char *s, FILE *f);


s f (
-).
8

.
12.8. ,

.
#include <stdio.h>
/* */
char * ZamenaBukv (char *str)
/* str */
{
int i; /* */
for ( i = 0; str[i] ; i++ )
if (str[i] >= A && str[i] <= Z) str[i] += 32;
return str;
}
/* */
main()
{ FILE *f1, *f2; /* */
char fname1[13], fname2[13]; /* */
char str[81]; /* */
puts ("\n ");
gets (fname1);
if ((f1= fopen (fname1, "r")) == NULL)
{ puts (" ");
getch();
return 1;
}
puts (" ");
gets (fname2);
f2 = fopen(fname2, "w");
while (fgets(str, 81, f1))
{ fputs(ZamenaBukv(str),f2);
}
fclose (f1);
fclose (f2);
return 0;
}

12.9. st.txt
.
(15 ) (5
) .
.
/*-------------------------------------------------------*/
/* */
/*-------------------------------------------------------*/
#include <stdio.h>
#include <conio.h>
struct STUDENT
{ char fio[15]; /* .. */
char oc[7]; /* 5 + '\n' + '\0' */
};
void main()
{ FILE *f;
/* */
struct STUDENT tz; /* */
int i,
s;
/* */
if ((f= fopen("st.txt","r")) == NULL)
{ puts (" st.txt ");
return;
}
puts ("\n .. .");
puts ("------------------------------");
while (fgets((char *)&tz, sizeof(struct STUDENT), f) != NULL)
{ for (i=0,s=0; i<5; i++)
s+=tz.oc[i]-'0';
tz.fio[14]='\0';
printf("%s %.1f\n", tz.fio, (float)s/5);
}
fclose(f);
getch();
}
5.

fscanf .
10

scanf

. ,

int, long, float .
12.10. , 20 ,
, .
, int float.
char a[21];
int b;
float c;
FILE *f;

fgets (a, 21, f);


fscanf (f, %d %f, &b, &c);
6. fprintf .
,
printf.
7. feof - .
:
int feof (FILE *f);
( 0),
, (0) .
12.11.

f1.txt

, .
.
#include <stdio.h>
void main()
11

{ FILE *f;
int c;
/* */
long s=0; /* */
if (f=fopen(f1.txt, r))
{ while (!feof(f))
{ fscanf(f, %d, &c);
s+=c;
}
printf(=%ld\n, s);
fclose (f);
}
else puts ( f1.txt );
}

1.


struct A
{ int a,b;
};
struct A s = {10, 20};
printf (%d, s.a + s.b);

2. :
struct
{ char a[10]; int b;
} s = {, 155};
printf (%d, %s, s.b, s.a);
3. :
struct A
{ int x;
float y;
} s, *p = &s;
x, y
10 5.5?

4. :
struct A
{ int x; char y[10];
} s, *t = &s;
t >x = 2006;
strcpy (t >y, );
12

printf (%s %d, s.y, s.x);


5. 4- :
- , ( 1 0);
- 100 ;
- ;
- .
.
6. 30 ().
4- , :
- , ;
- ;
- (, 1.85);
- .
7. :
int F (int a, int b)
{ if (a<b) return a+b;
return a-b;
}
void main()
{ struct
{ int n1, n2;
} s = {30, 20};
printf (%d, F(s.n1, s.n2));

}
8. MS DOS?
9. fopen()?
10.

fclose()?

11. f1.txt, 80
. n . n
.
13

12. f1.txt, 80
.
n . n .

1.

12.9.

st.txt .

1

16

st.txt:

20

------------------------------ .. 54435
..
55445
.. 45343
. . .
2.

st.txt , 20

, 4.

). .
3.

,
) 4;
) 4 ;
) ;
) ;
) ;
) .
.

14