A<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
		<id>http://www.cppe.ru/index.php?action=history&amp;feed=atom&amp;title=C%2FFile%2FBinary_File</id>
		<title>C/File/Binary File - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://www.cppe.ru/index.php?action=history&amp;feed=atom&amp;title=C%2FFile%2FBinary_File"/>
		<link rel="alternate" type="text/html" href="http://www.cppe.ru/index.php?title=C/File/Binary_File&amp;action=history"/>
		<updated>2026-04-17T18:03:49Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://www.cppe.ru/index.php?title=C/File/Binary_File&amp;diff=84&amp;oldid=prev</id>
		<title> в 14:20, 25 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://www.cppe.ru/index.php?title=C/File/Binary_File&amp;diff=84&amp;oldid=prev"/>
				<updated>2010-05-25T14:20:56Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr style=&quot;vertical-align: top;&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Предыдущая&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Версия 14:20, 25 мая 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; style=&quot;text-align: center;&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
			</entry>

	<entry>
		<id>http://www.cppe.ru/index.php?title=C/File/Binary_File&amp;diff=85&amp;oldid=prev</id>
		<title>Admin: 1 версия:&amp;#32;Импорт контента...</title>
		<link rel="alternate" type="text/html" href="http://www.cppe.ru/index.php?title=C/File/Binary_File&amp;diff=85&amp;oldid=prev"/>
				<updated>2010-05-25T10:22:10Z</updated>
		
		<summary type="html">&lt;p&gt;1 версия: Импорт контента...&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==A prime example using binary files==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
/*&lt;br /&gt;
Beginning C, Third Edition&lt;br /&gt;
 By Ivor Horton&lt;br /&gt;
 ISBN: 1-59059-253-0&lt;br /&gt;
 Published: Apr 2004&lt;br /&gt;
 Publisher: apress&lt;br /&gt;
*/&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;math.h &amp;gt;      /* For square root function sqrt() */&lt;br /&gt;
#define MEM_PRIMES 100  /* Count of number of primes in memory */&lt;br /&gt;
/* Function prototypes */&lt;br /&gt;
int test_prime(unsigned long N);&lt;br /&gt;
void put_primes(void);&lt;br /&gt;
int check(unsigned long buffer[], int count, unsigned long N);&lt;br /&gt;
/* Global variables */&lt;br /&gt;
char myfile[] = &amp;quot;C:\\myfile.bin&amp;quot;;                   /* Physical file name    */&lt;br /&gt;
FILE *pfile = NULL;                                 /* File pointer          */&lt;br /&gt;
unsigned long primes[MEM_PRIMES] = { 2UL,3UL,5UL }; /* Array to store primes */ &lt;br /&gt;
int index = 3;                     /* Index of free location in array primes */&lt;br /&gt;
int nrec = 0;                                      /* Number of file records */&lt;br /&gt;
void main()&lt;br /&gt;
{&lt;br /&gt;
  unsigned long trial = 5UL; /* Prime candidate */&lt;br /&gt;
  long num_primes = 3L;      /* Prime count     */&lt;br /&gt;
  long total = 0L;           /* Total required  */&lt;br /&gt;
  int i = 0;                 /* Loop counter    */&lt;br /&gt;
  printf(&amp;quot;How many primes would you like?  &amp;quot;);&lt;br /&gt;
  scanf(&amp;quot;%d&amp;quot;, &amp;amp;total);      /* Total is how many we need to find */&lt;br /&gt;
  total = total&amp;lt;4 ? 4:total;       /* Make sure it is at least 4 */&lt;br /&gt;
  /* Prime finding and storing loop */&lt;br /&gt;
  while(num_primes &amp;lt; total) /* Loop until we get total required  */&lt;br /&gt;
  {&lt;br /&gt;
    trial += 2;             /* Next value for checking           */&lt;br /&gt;
    if(test_prime(trial))   /* Check if trial is prime           */&lt;br /&gt;
    {                       /* Positive value means prime        */&lt;br /&gt;
       primes[index++] = trial;  /* so store it                  */&lt;br /&gt;
      num_primes++;         /* Increment total number of primes  */&lt;br /&gt;
      if(index == MEM_PRIMES) /* Check if array is full          */&lt;br /&gt;
      {&lt;br /&gt;
        /* File opened OK?   */&lt;br /&gt;
        if((pfile = fopen(myfile, &amp;quot;ab&amp;quot;)) == NULL)  &lt;br /&gt;
        { /* No, so explain and end the program */&lt;br /&gt;
          printf(&amp;quot;\nUnable to open %s to append\n&amp;quot;, myfile);&lt;br /&gt;
          abort();&lt;br /&gt;
        }&lt;br /&gt;
        /* Write the array    */&lt;br /&gt;
        fwrite(primes, sizeof(long), MEM_PRIMES, pfile);  &lt;br /&gt;
        fclose(pfile);                     /* Close the file */&lt;br /&gt;
        index = 0;        /* Reset count of primes in memory */                    &lt;br /&gt;
        nrec++;          /* Increment file record count      */                      &lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  if(total&amp;gt;MEM_PRIMES)   /* If we wrote some to file         */&lt;br /&gt;
    put_primes();        /* Display the contents of the file */&lt;br /&gt;
  if(index)              /* Display any left in memory       */&lt;br /&gt;
    for(i = 0; i&amp;lt;index ; i++)&lt;br /&gt;
    {&lt;br /&gt;
      if(i%5 == 0)&lt;br /&gt;
        printf(&amp;quot;\n&amp;quot;);              /* Newline after five     */&lt;br /&gt;
      printf(&amp;quot;%12lu&amp;quot;, primes[i]);  /* Output a prime         */&lt;br /&gt;
    }&lt;br /&gt;
  if(total&amp;gt;MEM_PRIMES)             /* Did we need a file? */&lt;br /&gt;
    if(remove(myfile))             /* then delete it.     */&lt;br /&gt;
      printf(&amp;quot;\nFailed to delete %s\n&amp;quot;, myfile); /* Delete failed */&lt;br /&gt;
    else&lt;br /&gt;
      printf(&amp;quot;\nFile %s deleted.\n&amp;quot;,myfile);     /* Delete OK     */&lt;br /&gt;
}&lt;br /&gt;
/************&lt;br /&gt;
 * Function to test if a number, N, is prime using primes in       *&lt;br /&gt;
 * memory and on file                                              *&lt;br /&gt;
 * First parameter N ?value to be tested                          *&lt;br /&gt;
 * Return value - a positive value for a prime, zero otherwise     *&lt;br /&gt;
 ************/&lt;br /&gt;
int test_prime(unsigned long N)&lt;br /&gt;
{&lt;br /&gt;
   unsigned long buffer[MEM_PRIMES]; /* local buffer for primes from the file */&lt;br /&gt;
   &lt;br /&gt;
   int i = 0;                        /* Loop counter */                      &lt;br /&gt;
   int k = 0;&lt;br /&gt;
   if(nrec &amp;gt; 0)         /* Have we written records? */&lt;br /&gt;
   {&lt;br /&gt;
     if((pfile = fopen(myfile, &amp;quot;rb&amp;quot;)) == NULL)  /* Then open the file */&lt;br /&gt;
     {&lt;br /&gt;
       printf(&amp;quot;\nUnable to open %s to read\n&amp;quot;, myfile);&lt;br /&gt;
       abort();&lt;br /&gt;
     }&lt;br /&gt;
     for(i = 0; i &amp;lt; nrec ; i++)&lt;br /&gt;
     { /* Check against primes in the file first */&lt;br /&gt;
       /* Read primes */&lt;br /&gt;
       fread(buffer, sizeof(long), MEM_PRIMES, pfile); &lt;br /&gt;
       if((k = check(buffer, MEM_PRIMES, N)) &amp;gt;= 0)   /* Prime or not? */&lt;br /&gt;
       {&lt;br /&gt;
         fclose(pfile);                     /* Yes, so close the file */&lt;br /&gt;
         return k;                          /* 1 for prime, 0 for not */&lt;br /&gt;
       }&lt;br /&gt;
     }&lt;br /&gt;
     fclose(pfile);                         /* Close the file         */&lt;br /&gt;
   }&lt;br /&gt;
   return check(primes, index, N);  /* Check against primes in memory */&lt;br /&gt;
}&lt;br /&gt;
/************&lt;br /&gt;
 * Function to check whether an integer, N, is divisble by any     *&lt;br /&gt;
 * of the elements in the array pbuffer up to the square root of N.*&lt;br /&gt;
 * First parameter buffer ?an array of primes                     *&lt;br /&gt;
 * second parameter count ?number of elements in pbuffer          *&lt;br /&gt;
 * Third parameter N ?the value to be checked                     *&lt;br /&gt;
 * Return value - 1 if N is prime, zero if N is not a prime,       *&lt;br /&gt;
 *               -1 for more checks                                *&lt;br /&gt;
 ************/&lt;br /&gt;
int check(unsigned long buffer[], int count, unsigned long N)&lt;br /&gt;
{&lt;br /&gt;
   int i = 0;                                 /* Loop counter */&lt;br /&gt;
   /* Upper limit */&lt;br /&gt;
   unsigned long root_N = (unsigned long)(1.0 + sqrt((double)N)); &lt;br /&gt;
   for(i = 0 ; i&amp;lt;count ; i++)&lt;br /&gt;
   {&lt;br /&gt;
     if(N % buffer[i] == 0UL ) /* Exact division?              */&lt;br /&gt;
       return 0;               /* Then not a prime             */&lt;br /&gt;
     if(buffer[i] &amp;gt; root_N)    /* Divisor exceeds square root? */&lt;br /&gt;
       return 1;               /* Then must be a prime         */&lt;br /&gt;
   }&lt;br /&gt;
   return  1;                  /* More checks necessary...     */&lt;br /&gt;
}&lt;br /&gt;
/* Function to output primes from the file */&lt;br /&gt;
void put_primes(void)&lt;br /&gt;
{&lt;br /&gt;
   unsigned long buffer[MEM_PRIMES]; /* Buffer for a block of primes */&lt;br /&gt;
   int i = 0;                        /* Loop counter                 */&lt;br /&gt;
   int j = 0;                        /* Loop counter                 */&lt;br /&gt;
 &lt;br /&gt;
   if((pfile = fopen( myfile, &amp;quot;rb&amp;quot;))==NULL) /* Open the file         */&lt;br /&gt;
   {&lt;br /&gt;
     printf(&amp;quot;\nUnable to open %s to read primes for output\n&amp;quot;, myfile);&lt;br /&gt;
     abort();&lt;br /&gt;
   }&lt;br /&gt;
   for (i = 0 ; i&amp;lt; nrec ; i++)&lt;br /&gt;
   {&lt;br /&gt;
     /* Read a block of primes   */&lt;br /&gt;
     fread(buffer, sizeof(long), MEM_PRIMES, pfile);  &lt;br /&gt;
     for(j = 0 ; j&amp;lt;MEM_PRIMES ; j++)  /* Display the primes */&lt;br /&gt;
     {&lt;br /&gt;
       if(j%5 == 0)                   /* Five to a line     */&lt;br /&gt;
         printf(&amp;quot;\n&amp;quot;);&lt;br /&gt;
       printf(&amp;quot;%12lu&amp;quot;, buffer[j]);    /* Output a prime     */&lt;br /&gt;
     }&lt;br /&gt;
   }&lt;br /&gt;
   fclose(pfile);                     /* Close the file     */&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	</feed>