Skip to content

Instantly share code, notes, and snippets.

@keyboardspecialist
Created April 15, 2014 05:47
Show Gist options
  • Save keyboardspecialist/10705393 to your computer and use it in GitHub Desktop.
Save keyboardspecialist/10705393 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_RECORDS 30
#define MAX_NAME_LEN 20
#define ERR_BAD_PTR -1
#define NOERR 0
/*
our struct for holding record data
*/
typedef struct
{
long m_recordId ;
char m_recordName[ MAX_NAME_LEN ] ;
} Record_t ;
//tidiness typedef
typedef Record_t* RecordPtr ;
int
addRecord( RecordPtr record )
{
//if we're NULL, eject and let the caller know
if( !record )
return ERR_BAD_PTR ;
//Buffer for our input
char buf [ MAX_NAME_LEN ] ;
//force initialization to all 0
memset( &buf[0], 0, MAX_NAME_LEN ) ;
//if we are of size 0 or just a newline (user just hit enter) keep asking
while( buf[0] == '\0' || buf[0] == '\n' )
{
printf( "\nEnter record name: " ) ;
//get input from user
fgets ( &buf[0], MAX_NAME_LEN, stdin ) ;
}
//copy our buf into the record
strncpy( &(record->m_recordName[0]), &buf[0], MAX_NAME_LEN ) ;
//reset the buf and reuse it
memset( &buf[0], 0, MAX_NAME_LEN ) ;
//this pointer will tell us if strtol completely failed
char* p = &buf[0] ;
//loop while strtol is unable to parse any numbers
while( p == &buf[0] )
{
printf( "\nEnter the record number: " ) ;
fgets ( &buf[0], MAX_NAME_LEN, stdin ) ;
//strtol will attempt to convert the entered characters in buf to a number.
//it will stop if a non-numeric character is encountered
record->m_recordId = strtol( &buf[0], &p, 10 ) ;
}
//all is good
return NOERR ;
}
int
printRecord( RecordPtr record )
{
if( !record )
return ERR_BAD_PTR ;
printf( "\nRecord ID: %ld \nRecord name: %s \n", record->m_recordId, record->m_recordName ) ;
return NOERR ;
}
int
getRecords( RecordPtr recordList, size_t maxRecords )
{
if( !recordList )
return ERR_BAD_PTR ;
int i = 0 ;
char add = 'y' ;
//quit looping if we reach maximum records or user aborts
for( ; i < maxRecords && (add != 'n' && add != 'N') ; i ++ )
{
int ret = addRecord( &recordList[i] ) ;
//if adding the record went OK, ask for another
if( ret == NOERR )
{
printf( "\nAdd another record (y/n)? " ) ;
add = getchar() ; //in truth we actually only care if they enter n/N
getchar() ; //eat the return, hacky
}
//bad pointer, not really possible in this example
else if( ret == ERR_BAD_PTR )
{
printf( "\nNULL pointer at record %d\n", i ) ;
//return error
return ret ;
}
}
//we succeeded, return number of valid entries
return i ;
}
int
main( int argc, char** argv )
{
//our list of records
Record_t records[ MAX_RECORDS ] ;
int r ;
r = getRecords( &records[0], MAX_RECORDS ) ;
if( r != ERR_BAD_PTR )
{
int i = 0 ;
//r is the number of records we entered, loop through them
for( ; i < r ; i ++ )
{
printf( "Printing record %d of %d \n", i+1, r ) ;
printRecord( &records[ i ] ) ;
}
}
return 0 ;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment