Simple C

11/04/2012

I noticed the other day that it has been a little while since I have updated this blog. Things have been really busy recently with a coding project and a Pure Data/Gem project on the go. As soon as these projects have been completed then I will be posting all the relevant materials and explanations etc.

Much of my time recently has been spent searching the internet for how to’s, tutorials and bits of source code to help me in my understanding of the C/C++ coding language.

A book I can recommend is ‘C Programming For The Absolute Beginner’, by Michael Vine, which I have found to be incredibly useful and actually pretty fun to work with. There is an example programme to accompany every chapter in the book, demonstrating the areas of the language covered in that chapter. The following is my adaptation of one of those programmes.


/*
 * record_library.c
 *
 * Created on: 22 Mar 2012
 * Author: ollie
 */

/* Programme that allows a user to create a library of/catalogue of records.
 * Library is stored as an 'example.orl' file.
 *
 * (File extension can be anything, providing file is only to be accessed by this programme)
 *
 * Possible bug with the formatting of output when displaying library.
 *
 * Want to add a loop to allow for multiple entries on single execution of programme. -- done
 *
 * Need to be able to include spaces in information entered.
 */

#include <stdio.h>
#include <stdlib.h>

void fPrintFields();//function prototype for getting data from user
void fPrintLibrary();//function prototype for printing record library

char cTitle[20];
char cArtist[20];
char cLabel[20];
char cReleasedate[10];//global variables to be accessed by all functions

FILE *pWrite;
FILE *pRead;//pointers to the location to beginning reading or writing the file

int main()

{

int iSelection = 0;

while (iSelection != 3)
 {

printf ("\nOllies Record Library.\n\nDo you want to;\n\t1) Enter new record?\n\t2) Show library?\n\t3) Exit?\n");
 fflush (stdout);
 scanf ("%d", &iSelection);//display options and get instruction from user.

if (iSelection == 1)
 {
 fPrintFields();//function to get data from user

pWrite = fopen("Record_Library.orl", "a");//get info from user and open .orl file.

if (pWrite != NULL)//if .orl file opened then write data into it.
 {
 fprintf (pWrite, "\n%s\n%s\n%s\n%s\n", cTitle, cArtist, cLabel, cReleasedate);
 fclose (pWrite);
 }
 else
 goto ErrorHandler;
 }

else if (iSelection == 2)
 {
 pRead = fopen ("Record_Library.orl", "r");

if (pRead != NULL)
 {
 fPrintLibrary();//function prints contents of library
 }

printf ("\n");
 fflush (stdout);

}

else if (iSelection == 3)
 {
 printf ("Thank you");
 fflush (stdout);
 exit(EXIT_SUCCESS);
 }

else
 goto ErrorHandler;


 }
 exit(EXIT_SUCCESS);

ErrorHandler:
 perror ("An error occurred");
 exit (EXIT_FAILURE);

}

void fPrintFields()//function definition to get data from user
{
 printf ("\nEnter title:\t");
 fflush (stdout);
 scanf ("%s", cTitle);
 printf ("\nEnter artist:\t");
 fflush (stdout);
 scanf ("%s", cArtist);
 printf ("\nEnter record label:\t");
 fflush (stdout);
 scanf ("%s", cLabel);
 printf ("\nEnter release date:\t");
 fflush (stdout);
 scanf ("%s", cReleasedate);
}


void fPrintLibrary()//function definition for printing library
{
 printf ("\nRecord Library\n");
 fflush (stdout);

while (!feof (pRead))
 {
 fscanf (pRead, "\n%s\n%s\n%s\n%s\n", cTitle, cArtist, cLabel, cReleasedate);

if (!feof (pRead))

printf ("\n\nTitle");
 fflush (stdout);
 printf ("\t%s", cTitle);
 fflush (stdout);
 printf ("\nArtist");
 fflush (stdout);
 printf ("\t%s", cArtist);
 fflush (stdout);
 printf ("\nLabel");
 fflush (stdout);
 printf ("\t%s", cLabel);
 fflush (stdout);
 printf ("\nRelease Date");
 fflush (stdout);
 printf ("\t%s\n\n", cReleasedate);
 fflush (stdout);

}
}


 Unfortunately some of the formatting has been lost whilst attaching the code to the post – I hope that it is still pretty readable. I have also tried to comment the code thoroughly as there is nothing more frustrating for a student learning C/C++ than trying to understand code that is poorly commented.

Over the last two years I have relied heavily on free demonstrations and examples posted online to help me in my studies. I intend to add to the wealth of material already available in the hope that it may be of use to students on similar pathways to my own.

Advertisements

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 )

Google+ photo

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

Twitter picture

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

Facebook photo

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

w

Connecting to %s

%d bloggers like this: