- , , ( ). .
:
int fseek(FILE * stream, long offset, int fromwhere);
: stream FILE, offset , , fromwhere : SEEK_SET , SEEK_CUR , SEEK_END . : 0, , 0 .
:
void rewind(FILE * stream);
stream FILE.
( ):
long ftell(FILE * stream);
stream FILE, .
-
- . , ( ), .
- :
int fflush(FILE * stream);
stream FILE, 0 EOF .
- :
int flushall();
( ), .
. ( , , ) . , , . , .
:
(: , , , , ). n. . .
Microsoft Visual Studio 2013 ( Win32 Console Application) (Empty project). .cpp, .
:
#include <stdio.h>
#include <stdlib.h>
|
|
struct BOOK // -
{
char Author[64];
char Title[128];
char Firm[64];
int year, page;
};
int main(int argc, char* argv[])
{
int n; // ,
BOOK *pBook; //
printf("n="); scanf_s("%d", &n); //
pBook = new BOOK[n]; // ()
for (int i = 0; i<n; i++) //
{
printf("Book N=%d:\n", i + 1);
printf("Author: ");
fflush(stdin); gets_s(pBook[i].Author, 63);
printf("Title: ");
fflush(stdin); gets_s(pBook[i].Title, 127);
printf("Firm: ");
fflush(stdin); gets_s(pBook[i].Firm, 63);
printf("year: "); scanf_s("%d", &pBook[i].year);
printf("page: "); scanf_s("%d", &pBook[i].page);
}
FILE *pF;
fopen_s(&pF, "MyBook.dat", "wb"); //
fwrite(&n, sizeof(int), 1, pF); //
fwrite(pBook, sizeof(BOOK), n, pF); //
fclose(pF); //
system("pause"); // ,
return 0;
}
:
#include <stdio.h>
#include <stdlib.h>
struct BOOK // -
{
char Author[64];
char Title[128];
char Firm[64];
int year, page;
};
int main(int argc, char* argv[])
{
int n; // ,
BOOK *pBook; //
FILE *pF;
fopen_s(&pF, "MyBook.dat", "rb");//
if (pF == 0) // , ,
{
printf("Error, file not found");
return 1;
}
fread(&n, sizeof(int), 1, pF); //
pBook = new BOOK[n]; // ()
fread(pBook, sizeof(BOOK), n, pF); //
fclose(pF); //
for (int i = 0; i<n; i++) //
printf("%s. %s. %s, %d. - %d.\n", pBook[i].Author,
pBook[i].Title, pBook[i].Firm, pBook[i].year,
pBook[i].page);
system("pause"); //
return 0;
}