Heya, i'm learning c at the moment and have a project i need to complete. I'd like to start of by saying that the course i'm on is pretty poorly taught. They taught us scanf printf basic arrays basic structures and fflush which doesn't even work on my compiler. We appear to be using sun systems that are well in excess of 10 years old and the compilers havn't been updated either. I''ve decided to do mine on my home pc with a copy of ubuntu i've installed instead and i've managed to get some of the poor teachng out of my head and good stuff in but i'm having some problems with some parts of my project.

PROBLEM NUMBER 1

Anyway, the problems i need help with are firstly to do with date. I'm making a basic video library system. I'm been reading through the documentation for <time.h> and i think i'm pretty sure i understand how to use strftime to get the current date and save it in integers and that shouldn't take me long to understand.

int current_day
int current_month
int current_year

the problem comes when i want to create a due date. Everytime a video is rented the due date will be 2 weeks later. I want to store these dates in the structure containing video information as integers like so

int current_day
int current_month
int current_year

so really i need to add 14 days to the current time and then format it with strftime and save it in those fields but i'm not sure how to go about it.

PROBLEM 2

I am using two structures in my database, customers and films, and they are linked by a borrowed by field which corresponds to the customer id in the customer field. the two structures look like this.

/* defining structure to hold customer details*/
typedef struct
{
    int  customer_id;
        char surname [25];
    char forename [15];
        char phone_number[20];
    int strikes;
    int rentals;
 
        } CUSTOMERS;
/* This is a variable that stores customer details */
 
CUSTOMERS customer_details [500];

/* defining structure to hold film details*/
typedef struct
{
    int  film_id;
        char film name [50];
    int genre;
        int type;
    int due_day;
    int due_month;
    int due_year;
 
        } FILMS;
         
/* This is a variable that stores customer details */
 
FILMS film_details [1000];

I'm sure this is an aweful implementation but i've just been trying to get along with what i have learned. I don't really have time to redo everything a cleverer way so thats the data will be stored. My problems arise when i bring files into the equation. I need to be able to save each structure into a file on program exit(or when new entries are made) and then retrieve them from the file when i open the program again. I have absolutely no idea where to start with this. How could i go about implementing this in my project?

Recommended Answers

All 5 Replies

Sorry, i meant to say that i want to save an array of structures to a file. Though you probably guessed by the 500 and 1000 respectively for each structure.

Problem 1:

Add 14 to days.  
If days > days-per-month
    set days to  days - days-per-month
    increment month

This is the idea, which isn't complete. You can flesh it out.

Problem 2:
First output the number of elements in the structure (active people/books). 1 integer.
Then set up a loop to output each element of the structure.
Reverse it for the read.

Other option is to simply output the entire structure in one output command.

Problem 1:

int dueDate(int current_year, int current_month, int current_day, 
            int days,
            int* pyear, int* pmon, int* pday)
{
    int res = 0;
    struct tm t;
    t.tm_year = current_year - 1900;
    t.tm_mon  = current_month - 1;
    t.tm_mday = current_day + days;
    t.tm_hour = t.tm_min = t.tm_sec = 0;
    t.tm_isdst = 0; /* daylight saving time */
    if (mktime(&t) != (time_t)-1) {
        if (pyear) *pyear = t.tm_year + 1900;
        if (pmon)  *pmon  = t.tm_mon  + 1;
        if (pday)  *pday  = t.tm_mday;
        res = 1;
    }
    return res;
}

Problem 2.
Is it a problem? Didn't you know regular fopen/fwrite/fread/fclose library functions from stdio.h header?

okay, i'm making headway with a few things. I've implemented a function that adds new records to the file.
using the following code i get an output of. Just three test files i put in.

01454315364, joshua, baxter ,1, 0 ,0,1
07896463747, charlotte, jones ,2, 0 ,0,1
07896463747, JOSHUA, BAXTER ,3, 0 ,0,1

{
FILE *filepointer;
int y;


	filepointer = fopen("customer", "r");
fread ( &customer_details, sizeof ( CUSTOMERS),1,filepointer);

while (!(feof (filepointer)))
{
printf("%s, %s, %s ,%d, %d ,%d,%d\n",customer_details.phone_number, customer_details.forename, customer_details.surname, customer_details.customer_id, customer_details.strikes, customer_details.rentals, customer_details.active);

fread ( &customer_details, sizeof ( CUSTOMERS),1,filepointer);
}

scanf ("%d" , &y );
fclose (filepointer);
}

now i'm trying to write a function that finds a person by there id and then edits a field in the string its found in but i'm having some trouble. find the relevant file is easy but i want to rewrite the field active to 0.

void f_removecust ()
{
FILE *filepointer;
int found;
int id;
char choice;
fpos_t pos;

 puts("What is the ID number of the customer you would like to delete");
 scanf("%d",&id);
__fpurge(stdin);



	filepointer = fopen("customer", "r+");
fread ( &customer_details, sizeof ( CUSTOMERS),1,filepointer);

while ( (found==0) || (!(feof (filepointer))) )
{
if ( customer_details.customer_id == id)
	{ 
		system("clear");
		printf("The customer with this id is %s %s with phone number %s. Do you definately want to delete this customer?\n", customer_details.forename, customer_details.surname, customer_details.phone_number);
			
			do
			{
			scanf( "%c" , &choice);
			__fpurge(stdin);
			if ( (choice != 'y') && (choice != 'Y') && (choice != 'n') && (choice != 'N'))
				{
				system ("clear");
				puts("Invalid Input. Must answer y or n.\n");			
				}
			} while ((choice != 'y') && (choice != 'Y') && (choice != 'n') && (choice != 'N'));

			if ( (choice == 'N') || (choice == 'n'))
			{
			fclose (filepointer);
			return;
			}
//*** the previous part all works fine, its the following i'm having trouble with. i want to edit the record active to 0, how can i go about this. i've just been guessing ****/
			if ((choice == 'y') || (choice == 'Y'))
			{ 
			fgetpos (filepointer,&pos);
			setpos (filepointer,&pos);
	                customer_details.active = 0;
			puts("customer has been removed");
			fclose (filepointer);
			return;
			}
	}
			
fread ( &customer_details, sizeof ( CUSTOMERS),1,filepointer);
}
puts("customer not found")
	
fclose (filepointer);
}

it doesn't crash but it appears to do nothing. How do i go about rewriting just that record. any help would be appreciated. I'll be making several similar functions so it would be good to figure this one out. Or should i just cut my losses and read in each line and edit it if neccessary and then send to a new file.

Problem 1:

int dueDate(int current_year, int current_month, int current_day, 
            int days,
            int* pyear, int* pmon, int* pday)
{
    int res = 0;
    struct tm t;
    t.tm_year = current_year - 1900;
    t.tm_mon  = current_month - 1;
    t.tm_mday = current_day + days;
    t.tm_hour = t.tm_min = t.tm_sec = 0;
    t.tm_isdst = 0; /* daylight saving time */
    if (mktime(&t) != (time_t)-1) {
        if (pyear) *pyear = t.tm_year + 1900;
        if (pmon)  *pmon  = t.tm_mon  + 1;
        if (pday)  *pday  = t.tm_mday;
        res = 1;
    }
    return res;
}

Am i right in thinking that the mktime lets you add values to each of the things in the structure and then analyses it so it makes sense as a date. Also, i don't mean to be stupid but how would i go about calling this function. The part in red is what i don't really understand? how do i pass these values on when i'm using the function. like i said i havn't been taught much so i'm kind of working arround my lack of knowledge of how c works at the moment.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.