Code:
#include<stdio.h>
#include<string.h>
#include<windows.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<limits.h> //INT_MAX equal to +2147483647 <<complier dependant
int File_IPOP(int selection, int in)
//1=Write data to file >> return 0;
//2=erase data from file >> return 0;
//3=read size of file >> return int filesize;
//4=read LAST integer from file >> return int last_prime;
//5=read FIRST integer from file >> return int first_prime;
//6=read NEXT integer from file >> return int prime;
//"In" = Prime integer to add to end of file.
//"/cygdrive/c/data/PP.txt"<<FILE
{
int out,count,filesize,c,last_prime,line;
char filename;
FILE *fp;
int store,ret;
/*r - open for reading
w - open for writing (file need not exist)
a - open for appending (file need not exist)
r+ - open for reading and writing, start at beginning
w+ - open for reading and writing (overwrite file)
a+ - open for reading and writing (append if file exists)*/
FILE *fw;
fw=fopen("/cygdrive/c/data/PP.txt", "a+");
FILE *fr;
fr=fopen("/cygdrive/c/data/PP.txt", "r+");
FILE *fa;
fa=fopen("/cygdrive/c/data/PP.txt", "r");
switch(selection)
{
case 1:
{
if ((fw=fopen("/cygdrive/c/data/PP.txt", "a+")) == NULL)
{
fprintf(stdout, "Error: Can't open file !\n");
return -1;
}
else
{
fprintf(fw,"%d\n",in); //"In" = Prime integer added to end of file.
fflush(fw);
fclose(fw);
return 0;
}
return 0;
}
break;
case 2:
{
if ((fw=fopen("/cygdrive/c/data/PP.txt", "a+")) == NULL)
{
fprintf(stdout, "Error: Can't open file !\n");
return -1;
}
else
{
fopen("/cygdrive/c/data/PP.txt", "w");
return 0;
}
}
break;
case 3:
{
/* ftell example : getting size of a file */
if ((fa=fopen("/cygdrive/c/data/PP.txt", "r")) == NULL)
{
fprintf(stdout, "Error: Can't open file !\n");
return -1;
}
else
{
while(1)
{
if(feof(fa)) //EOF
{
filesize=ftell (fa); // ftell read bytes from end to beginning
fclose (fa);
if(filesize=!-1)
{
printf ("Size of text file: %ld bytes.\n",filesize);
return filesize;
}
else
{
printf("error occurred when reading file size\n\n");
}
}
else //not EOF yet
{
fscanf(fa,"%d",&last_prime);
}
}
return 0;
}
}
break;
case 4:
{
while(1)
{
if(feof(fa)) //EOF
{
return last_prime;
}
else //not EOF yet
{
fscanf(fa,"%d",&last_prime);
}
}
}
break;
case 5:
{
// Open a file ("test.txt")
if ((fa) == NULL)
{
fprintf(stdout, "Error: Can't open file !\n");
return -1;
}
else
{
ret = fscanf(fa, "%d[^\n]\n", &store);
if(ret != EOF)
{
// Print line
printf("%d\n", store);
return store;
}
}
// Close file
fclose(fa);
return 0;
}
break;
case 6:
{
if ((fa) == NULL)
{
fprintf(stdout, "Error: Can't open file !\n");
return -1;
}
else
{
fscanf(fa, "%d[^\n]\n", &store);
return store;
}
}
break;
default:
printf("No selection was specified");
break;
}
return -1;
}
int main()
{
/*int pp,prevprime;
while(pp%File_IPOP(6,0)==0)//logical check
{
printf("PP not prime") //if yes
PP=PP+2;
File_IPOP(2,0); //set pointer to start
}
else
{
check against all Prevprimes before looping again
}
*/
/*File_IPOP(2,0);
File_IPOP(1,10);
File_IPOP(1,20);
File_IPOP(1,30);
File_IPOP(1,40);
*/
printf("%d\n",File_IPOP(5,0));
printf("%d\n",File_IPOP(6,0));
printf("%d\n",File_IPOP(6,0));
}