A prime example using binary files : Binary File « File « C / ANSI-C

C / ANSI-C
1. assert.h
2. Console
3. ctype.h
4. Data Structure Algorithm
5. Data Type
6. Development
7. File
8. Function
9. Language Basics
10. Macro Preprocessor
11. Math
12. math.h
13. Memory
14. Pointer
15. setjmp.h
16. signal.h
17. Small Application
18. stdio.h
19. stdlib.h
20. String
21. string.h
22. Structure
23. time.h
24. wctype.h
Microsoft Office Word 2007 Tutorial
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
C / ANSI-C » File » Binary FileScreenshots 
A prime example using binary files

/*
Beginning C, Third Edition
 By Ivor Horton
 ISBN: 1-59059-253-0
 Published: Apr 2004
 Publisher: apress

*/

#include <stdio.h>
#include <stdlib.h>
#include <math.h >      /* For square root function sqrt() */

#define MEM_PRIMES 100  /* Count of number of primes in memory */

/* Function prototypes */
int test_prime(unsigned long N);
void put_primes(void);
int check(unsigned long buffer[]int count, unsigned long N);

/* Global variables */
char myfile[] "C:\\myfile.bin";                   /* Physical file name    */
FILE *pfile = NULL;                                 /* File pointer          */
unsigned long primes[MEM_PRIMES2UL,3UL,5UL }/* Array to store primes */ 
int index = 3;                     /* Index of free location in array primes */
int nrec = 0;                                      /* Number of file records */

void main()
{
  unsigned long trial = 5UL; /* Prime candidate */
  long num_primes = 3L;      /* Prime count     */
  long total = 0L;           /* Total required  */
  int i = 0;                 /* Loop counter    */

  printf("How many primes would you like?  ");
  scanf("%d", &total);      /* Total is how many we need to find */
  total = total<4:total;       /* Make sure it is at least 4 */

  /* Prime finding and storing loop */
  while(num_primes < total/* Loop until we get total required  */
  {
    trial += 2;             /* Next value for checking           */

    if(test_prime(trial))   /* Check if trial is prime           */
    {                       /* Positive value means prime        */
       primes[index++= trial;  /* so store it                  */
      num_primes++;         /* Increment total number of primes  */

      if(index == MEM_PRIMES/* Check if array is full          */
      {
        /* File opened OK?   */
        if((pfile = fopen(myfile, "ab")) == NULL)  
        /* No, so explain and end the program */
          printf("\nUnable to open %s to append\n", myfile);
          abort();
        }
        /* Write the array    */
        fwrite(primes, sizeof(long), MEM_PRIMES, pfile);  

        fclose(pfile);                     /* Close the file */
        index = 0;        /* Reset count of primes in memory */                    
        nrec++;          /* Increment file record count      */                      
      }
    }
  }

  if(total>MEM_PRIMES)   /* If we wrote some to file         */
    put_primes();        /* Display the contents of the file */
  if(index)              /* Display any left in memory       */
    for(i = 0; i<index ; i++)
    {
      if(i%== 0)
        printf("\n");              /* Newline after five     */
      printf("%12lu", primes[i]);  /* Output a prime         */
    }

  if(total>MEM_PRIMES)             /* Did we need a file? */
    if(remove(myfile))             /* then delete it.     */
      printf("\nFailed to delete %s\n", myfile)/* Delete failed */
    else
      printf("\nFile %s deleted.\n",myfile);     /* Delete OK     */
}

/************
 * Function to test if a number, N, is prime using primes in       *
 * memory and on file                                              *
 * First parameter N ?value to be tested                          *
 * Return value - a positive value for a prime, zero otherwise     *
 ************/
int test_prime(unsigned long N)
{
   unsigned long buffer[MEM_PRIMES]/* local buffer for primes from the file */
   
   int i = 0;                        /* Loop counter */                      
   int k = 0;

   if(nrec > 0)         /* Have we written records? */
   {
     if((pfile = fopen(myfile, "rb")) == NULL)  /* Then open the file */
     {
       printf("\nUnable to open %s to read\n", myfile);
       abort();
     }

     for(i = 0; i < nrec ; i++)
     /* Check against primes in the file first */
       /* Read primes */
       fread(buffer, sizeof(long), MEM_PRIMES, pfile)
       if((k = check(buffer, MEM_PRIMES, N)) >= 0)   /* Prime or not? */
       {
         fclose(pfile);                     /* Yes, so close the file */
         return k;                          /* 1 for prime, 0 for not */
       }
     }
     fclose(pfile);                         /* Close the file         */
   }

   return check(primes, index, N);  /* Check against primes in memory */
}

/************
 * Function to check whether an integer, N, is divisble by any     *
 * of the elements in the array pbuffer up to the square root of N.*
 * First parameter buffer ?an array of primes                     *
 * second parameter count ?number of elements in pbuffer          *
 * Third parameter N ?the value to be checked                     *
 * Return value - 1 if N is prime, zero if N is not a prime,       *
 *               -1 for more checks                                *
 ************/
int check(unsigned long buffer[]int count, unsigned long N)
{
   int i = 0;                                 /* Loop counter */
   /* Upper limit */
   unsigned long root_N = (unsigned long)(1.0 + sqrt((double)N))

   for(i = ; i<count ; i++)
   {
     if(N % buffer[i== 0UL /* Exact division?              */
       return 0;               /* Then not a prime             */

     if(buffer[i> root_N)    /* Divisor exceeds square root? */
       return 1;               /* Then must be a prime         */
   }
   return  1;                  /* More checks necessary...     */
}

/* Function to output primes from the file */
void put_primes(void)
{
   unsigned long buffer[MEM_PRIMES]/* Buffer for a block of primes */
   int i = 0;                        /* Loop counter                 */
   int j = 0;                        /* Loop counter                 */
 
   if((pfile = fopenmyfile, "rb"))==NULL/* Open the file         */
   {
     printf("\nUnable to open %s to read primes for output\n", myfile);
     abort();
   }

   for (i = ; i< nrec ; i++)
   {
     /* Read a block of primes   */
     fread(buffer, sizeof(long), MEM_PRIMES, pfile);  

     for(j = ; j<MEM_PRIMES ; j++)  /* Display the primes */
     {
       if(j%== 0)                   /* Five to a line     */
         printf("\n");
       printf("%12lu", buffer[j]);    /* Output a prime     */
     }
   }
   fclose(pfile);                     /* Close the file     */
}


           
       
Related examples in the same category
w__w___w.__ja__va__2_s._c__om_ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.