, ' , fread () fwrite (), . . :
struct addr {
char name [30];
char street [40];
char city [20];
char state [3];
unsigned long int zip;
} Addr_list [MAX];
MAX , .
name (NULL). , name , ' ߹ .
save () load (), ( ). , , fread () fwrite ()! , , fread () fwrite (), .
/* . */
void save (void) {
FILE *fp;
register int i;
if ((fp = fopen ("maillist", "wb")) == NULL) {
printf (" ii . \n");
return;
}
for (i = 0; i <MAX; i++)
if (*addr_list[i].name)
if (fwrite (&addr_list[i], sizeof (struct addr), 1, fp)!= 1)
printf (" i . \n");
fclose (fp);
}
/* . */
void load (void) {
FILE *fp;
register int i;
if ((fp = fopen ("maillist", "rb")) == NULL) {
printf (" ii . \ n");
return;
}
init_list ();
for (i = 0; i <MAX; i++)
if (fread (&addr_list[i], sizeof (struct addr), 1, fp)!= 1) {
if (feof (fp)) break;
printf (" i . \n");
}
fclose (fp);
}
, save () load (), ( ) fread () fwrite () , , fread () fwrite (). , load () ߹, . feof (). , fread () , .
, ߹ . , , , .
/* ,
. */
# include <windows.h>
# include <locale.h>
# include <stdio.h>
# include <stdlib.h>
# define MAX 100
struct addr {
char name [30];
char street [40];
char city [20];
char state [20];
unsigned long int zip;
} addr_list [MAX];
void init_list (), enter ();
|
|
void delete1 (), list ();
void load (), save ();
int menu_select (), find_free ();
int main() {
setlocale(LC_CTYPE, "Russian");
char choice;
init_list (); /* */
for (;;) {
choice = menu_select ();
switch (choice)
{
case 1: enter (); break;
case 2: delete1 (); break;
case 3: list (); break;
case 4: save (); break;
case 5: load (); break;
case 6: exit (0);
}
}
system ("pause");
return 0;
}
/* . */
void init_list () {
register int t;
for (t = 0; t <MAX; ++t) addr_list [t].name[0] = '\0';
}
/* , . */
int menu_select () {
char s [80];
int c;
printf ("1. i' \n");
printf ("2. i' \n");
printf ("3. \n");
printf ("4. \n");
printf ("5. \n");
printf ("6. i \n");
do {
printf ("\n i i : ");
gets (s);
c = atoi (s);
} while (c <0 || c> 6);
return c;
}
/* . */
void enter () {
int slot;
char s [80];
slot = find_free ();
if (slot == -1) {
printf ("\n ");
return;
}
printf ("i i': ");
gets (addr_list [slot].name);
printf ("i : ");
gets (addr_list [slot].street);
printf ("i i: ");
gets (addr_list [slot].city);
printf ("i : ");
gets (addr_list [slot].state);
printf ("i i: ");
gets (s);
addr_list [slot].zip = strtoul (s, '\0', 10);
}
/* . */
int find_free () {
register int t;
for (t = 0; addr_list[t].name [0] && t <MAX; ++t);
if (t == MAX) return -1; /* */
return t;
}
/* . */
void delete1 () {
register int slot;
char s [80];
printf ("i : ");
gets (s);
slot = atoi (s);
if (slot >= 0 && slot <MAX)
addr_list [slot].name [0] = '\0';
}
/* . */
void list () {
register int t;
for (t = 0; t <MAX; ++t) {
if (addr_list[t].name [0])
{
printf ("%s \n", addr_list[t].name);
printf ("%s \n", addr_list[t].street);
printf ("%s \n", addr_list[t].city);
printf ("%s \n", addr_list[t].state);
printf ("%lu \n \n", addr_list[t].zip);
}
}
printf ("\n \n");
}
/* . */
void save () {
FILE *fp;
register int i;
if ((fp = fopen ("maillist", "wb")) == NULL) {
printf (" ii . \n");
return;
}
for (i = 0; i <MAX; i++)
if (*addr_list[i].name)
if (fwrite (&addr_list[i], sizeof (struct addr), 1, fp)!= 1)
printf (" i . \n");
fclose (fp);
}
/* . */
void load () {
FILE *fp;
register int i;
if ((fp = fopen ("maillist", "rb")) == NULL) {
printf (" ii . \ n");
return;
}
init_list ();
for (i = 0; i <MAX; i++)
if (fread (&addr_list[i], sizeof (struct addr), 1, fp)!= 1) {
if (feof (fp)) break;
printf (" i . \n");
|
|
}
fclose (fp);
}
fseek()
:
fseek(fp, n, m);
fp , n , ( ) m, : 0, 1, 2 , stdio.h:
- SEEK_SET = 0 ;
- SEEK_CUR = 1 ;
- SEEK_END = 2 .
fseek () / . lseek (). fseek () , . fseek () , . errno :
- EBADF - ;
- EINVAL - ;
- ESPIPE - .
/, / fseek (), . :
int fseek (FILE < >, long int <_>, <_>);
_ - , _, , - :
-
- SEEK_SET
- SEEK_CUR
- SEEK_END
, _ , _ SEEK_SET. , SEEK_CUR, , SEEK_END. fseek () , - .
, fseek (). , . ' , , .
# 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 ("testr", "r")) == NULL) {
printf (" ii . \ n");
system ("pause");
exit (1);
}
fseek (fp, 6, SEEK_SET);
ch=fgetc (fp);
printf (" 6- i i ");
putchar (ch);
printf ("\n");
fclose (fp);
system ("pause");
return 0;
}
fseek () , , . , , , addr ( ). , , :
fseek (fp, 9 * sizeof (struct addr), SEEK_SET);
ftell (). :
long int ftell (FILE < >);
, ' . -1.
. - , , , . , fseek () - , ftell (); SEEK_SET, .
|
|
' : , . , ' , , . , .
ftell()
:
long int i = ftell(fp);
fp ( ) long int. .
fseek ().
, -1L errno .
# 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;
long int l;
FILE *fp;
if ((fp = fopen ("test", "r")) == NULL) {
printf (" ii . \n");
system ("pause");
exit(1);
}
while (! feof (fp)) {
l = ftell(fp);
printf (" i %d ", l);
fgets (str, 79, fp);
printf (str);
}
l = ftell(fp);
printf ("\n i %d i ", l);
printf ("\n");
fclose (fp);
system ("pause");
return 0;
}
fscanf()
:
fscanf(fp, Control, arg1, arg2,..., argn);
fp, , Control, arg1,..., rgn. , scanf () ( scanf ()).
fprintf()
:
fprinf(fp, Control, arg1, arg2,..., argn);
fp, arg1,..., rgn , Control, . , printf ().
/, , / fprintf () fscanf (). , , , , printf () scanf (). fprintf () fscanf () :
int fprintf (FILE * , const char * _,...);
int fscanf (FILE * , const char * _,...);
- , fopen (). / fprintf () fscanf () , .
, , ; ' test1. . , test1. , .
/* fscanf () fprintf () */
# include <stdlib.h>
# include <stdio.h>
# include <windows.h>
# include <locale.h>
int main () {
setlocale(LC_CTYPE, "Russian");
FILE *fp;
char s [80];
|
|
int t;
if ((fp = fopen ("test1", "w")) == NULL) {
printf (" ii . \ n");
system ("pause");
exit (1);
}
printf ("i i : ");
fscanf (stdin, "%s %d", s, &t); /* */
fprintf (fp, "%s %d", s, t); /* */
fclose (fp);
if ((fp = fopen ("test1", "r")) == NULL) {
printf (" ii . \n");
system ("pause");
exit (1);
}
printf("\n");
fscanf (fp, "%s %d", s, &t); /* */
fprintf (stdout, "%s %d", s, t); /* */
printf("\n");
fclose (fp);
system ("pause");
return 0;
}
. , , fprintf () fscanf (), . ASCII , ' ( ), ' . , , , , fread () fwrite ().
rewind()
:
rewind(<__>);
. rewind (<__>) fseek (<__>, 0L, SEEK_SET) , rewind () , fseek () . rewind () , . .
rewind (), , , , . , "" , . , /, , "w+".
# 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() -
/* */
rewind (fp); /* . */
while (! feof (fp)) {
fgets (str, 79, fp);
printf (str);
}
fclose (fp);
system ("pause");
return 0;
}
remove()
:
int remove (< '_>);
߹ ' FILENameString. . FILENameString . , 1 ( ). errno :
- EACCES - ;
- ENOENT - .
. . , , ' -.
/* . */
# include <stdlib.h>
# include <stdio.h>
# include <windows.h>
# include <locale.h>
# include <ctype.h>
int main() {
setlocale(LC_CTYPE, "Russian");
char str [80];
char filename[20]={"test"};
printf (" test? (y/n):");
gets (str);
if (toupper (*str) == 'y')
remove (filename);
if (remove (filename)) {
printf (" . \n");
system ("pause");
exit (1);
}
system ("pause");
return 0;
}
FILElength()
:
long FILElength(fp);
fp . io.h. - 1, errno EBADF - .
|
|
ferror()
:
ferror(fp);
ferror() , . :
int ferror(FILE < _ >);
true (), , false (). - , ferror (), .
ferror (). ߹ , . TAB_SIZE. , ferror () .
/* . */
# include <stdlib.h>
# include <stdio.h>
# include <windows.h>
# include <locale.h>
# define IN 0
# define OUT 1
void err (int e);
int main() {
setlocale(LC_CTYPE, "Russian");
FILE *in, *out;
int i;
char ch;
if ((in = fopen ("testr", "rb")) == NULL) {
printf (" i testr. \n");
system ("pause");
exit(1);
}
if ((out = fopen ("testw", "wb")) == NULL) {
printf (" i testw. \n");
system ("pause");
exit(1);
}
do {
ch = fgetc (in);
if (ferror (in)) err (IN);
/* , */
if (ch == '\t')
{
putc (' ', out);
if (ferror (out)) err (OUT);
}
else
{
putc (ch, out);
if (ferror (out)) err (OUT);
}
} while (!feof (in));
fclose (in);
fclose (out);
system ("pause");
return 0;
}
void err (int e) {
if (e == IN) printf (" i. \n");
if (e == OUT) printf (" i. \n");
system ("pause");
exit (1);
}
freopen()
:
FILE *freopen(const char *FILEname, const char *mode, FILE *stream);
߹ , , . , . , ' / stdin, stdout stderr. fopen (). FILE ( fopen ()), - NULL.
freopen () 1.
1
/* */
if (freopen ("OUTPUT.FIL", "w", stdout) == NULL)
fprintf (stderr, "error redirecting stdout \n");
/* */
printf ("This will go into a FILE.");
/* */
fclose (stdout);
ferror()
:
ferror(FILE *fp);
, (, ). / :
if (ferror(fp)) { / }
, , .
exit()
:
exit(int status);
, . , , , ' , atexit (). atexit ().
atexit () stdlib.h. atexit () exit (). 32- exit (). ': " - ". . , 2.
2
# include <stdio.h>
# include <stdlib.h>
/* exit () */
void exit_fn1 (void) {
printf ("Exit function # 1 called \ n");
}
/* exit () */
void exit_fn2 (void) {
printf ("Exit function # 2 called \ n");
}
/* exit (). exit (), */
int main (void) {
/* */
atexit (exit_fn1);
/* */
atexit (exit_fn2);
/* Exit () , , - . . */
exit (0);
return 0;
}
, exit () . , . exit () .