Tutorial: Storing and loading data in a plain textfile.

A program which contains an array of prime numbers requires functions to facilitate the persistence of data using functions to save an load data.

In this tutorial, we have some functions which can be used to achieve this.

The goal of this tutorial is to understand how we can achieve this by performing the actions in the inline comments of the main() function.

Run the code below, and perform the tasks whilst keeping track of your answers in your notebook:

#include <stdio.h>
#include <stdlib.h>
void setupDummyData();
void saveToFile();
void readFile();
void printPrimesToScreen();



int primes[100];
int result[100];
int count = 0;

int main()
{


   //When answering tutorial questions, be as precise and descriptive as possible

   //TUTORIAL BLOCK 1:
   //a. What does the code accomplish if the following multiline comment is removed?

  /* printPrimesToScreen();
   setupDummyData();
   printPrimesToScreen();*/



   //TUTORIAL BLOCK 2:
   //a. Redo the multiline comment above.
   //b. What does the code accomplish if the following multiline comment is removed?
   /*
   printPrimesToScreen();
   setupDummyData();
   saveToFile();
   printPrimesToScreen();   */



   //TUTORIAL BLOCK 3:
   //a. Redo the multiline comment above.
   //b. What does the code accomplish if the following multiline comment is removed?
   //c. Where did the data in the array come from on the second printPrimesToScreen()?
   /*
   printPrimesToScreen();
   readFile();
   printPrimesToScreen();//2nd printPrimesToScreen()
   */

   //TUTORIAL BLOCK 4:
   //a. Redo the multiline comment above.
   //b. DELETE the file "primes.txt"
   //c. What does the code accomplish if the following multiline comment is removed?
   //d. What data exists in the file "primes.txt"
   /*
   printPrimesToScreen();
   readFile();
   saveToFile();
   readFile();
   printPrimesToScreen();
   */

   //DELETE primes.txt if you need to retry this tutorial.

   printf("Program quitting...\n");
   return 0;
}

//HARDCODE Data into array
void setupDummyData()
{
   count = 8;
    primes[0]= 2;
    primes[1]= 3;
    primes[2]= 5;
    primes[3]= 7;
    primes[4]= 11;
    primes[5]= 13;
    primes[6]= 17;
    primes[7]= 19;
    primes[8]= 23;
    primes[9]= 29;
}

//empty all contents into a file in overwritemode
void saveToFile()
{
   FILE* filePtr = fopen("primes.txt","w");
   if (filePtr==NULL)
      printf("Error creating savefile.....Check that file is not in use\n\n");
   else
   {
      //write the number of records in the array on the first line
      fprintf(filePtr, "%d\n",count);
      //write each prime number on a separate line in the file
      for (int i = 0 ; i<count;i++)
         fprintf(filePtr, "%d\n",primes[i]);
   }
   fclose(filePtr);//close file for use by other processes
   printf("Datafile Saved!\n\n");
}


//read all data into array
void readFile()
{
   FILE* filePtr = fopen("primes.txt","r");
   if (filePtr==NULL)
      printf("Error reading file, does it exist?\n\n");
   else
   {
      //read the number of records in the file from the first line
      fscanf(filePtr, "%d",&count);
      //write each prime number on a separate line in the file
      for (int i = 0 ; i<count;i++)
         fscanf(filePtr, "%d",&primes[i]);
   }
   fclose(filePtr);//close file for use by other processes
}



void printPrimesToScreen()
{
   printf("\n\n****Now Displaying all Primes in the array\n");
   if (count==0)
         printf("\nPrime Array Is empty...\n\n");
   else
   {
      for (int i = 0 ; i<count;i++)
         printf("index: %d , prime: %d \n",i,primes[i]);
      printf("\n");

   }

}




© 2022  Vedesh Kungebeharry. All rights reserved. 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s