sorry for reviving this thread but ive been working so havnt had much of a chance to continue with my learning of c. when i try and open the file it doesnt seem to be finding it, where should the file be placed? here is my code, there may be something wrong:
list.h
Code:
/*List header file */
#include <stdio.h> /* Header file includes the file stdio.h
because that is where NULL is defined*/
typedef char DATA[30]; /* Creates names of types that are more
suggestive of their use*/
typedef struct linked_list {
DATA d;
struct linked_list *next;
} Element;
typedef Element* LINK;
time_lib.h
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define MAXSTRING 100
typedef struct {
clock_t begin_clock, save_clock;
time_t begin_time, save_time;
} time_keeper;
static time_keeper tk; /* tk is external to the functions and is used
for communication between the functions in this
file */
void start_time(void) /* when start_time() is invoked, the values returned
from clock() and time() are stored in tk */
{
tk.begin_clock = tk.save_clock =clock();
tk.begin_time = tk.save_time = time(NULL);
}
double prn_time(void) /* When prn_time() is invoked, new values from clock()
and time() are used to compute and print the elapsed user
time and the elapsed real time, and new values are stored
in tk*/
{
char s1[MAXSTRING], s2[MAXSTRING];
int field_width, n1,n2;
double clocks_per_sec = (double) CLOCKS_PER_SEC,
user_time, real_time;
user_time = (clock() -tk.save_clock) / clocks_per_sec;
real_time = difftime(time(NULL),tk.save_time);
tk.save_clock =clock();
tk.save_time =time(NULL);
sprintf(s1, "%.1f", user_time);
n1 = strlen(s1);
sprintf(s2, "%.1f", real_time);
n2 =strlen(s2);
field_width = (n1>n2)? n1 :n2;
printf("%s%*.1f%s\n%s%*.1f%s\n\n",
"User time: ", field_width, user_time, "seconds",
"Real time: ", field_width, real_time, "seconds");
return user_time;
}
list.c
Code:
#include "list.h"
#include "time_lib.h"
#include <stdlib.h>
#include <string.h>
/*Function to create a list recursivly*/
LINK create( DATA h, Element *t )
{
LINK y = calloc( 1, sizeof( Element ) );
strcpy( y->d, h );
if ( t ) t->next = y; /* Only do this if there is a previous element. */
return y;
}
/* Function to read data from file*/
FILE *readdata(void)
{
FILE *ifp; /*infile pointer*/
ifp=fopen("birthday.txt","r"); /*Open file birthday for reading only*/
if(ifp==NULL)
{
printf("File Cannot Be Opened\n");
}
return ifp;
}
/* Function to insert data into the list*/
LINK insert(FILE *ifp)
{
LINK head = NULL;
LINK tail = NULL;
DATA b;
while( fscanf( ifp, "%s", b ) == 1 ){
tail = create( b, tail );
if( !head ) { head = tail; }
}
printf("\n");
return head;
}
/* Function to count the number of elements in the list recursively */
int count(LINK head)
{
if(head==NULL) /* If the list is empty , 0 is returned , otherwise
the number of elements in the list is returned */
return 0;
else
return (1+count(head->next));
}
/*Function to print list*/
void print_list(LINK head)
{
/* This 'for' loop executes while head !=0 */
LINK next;
for( ; head ; head = next ) {
printf( "%s\n", head->d );
next = head->next;
}
}
int main(void)
{
LINK head = NULL;
FILE *ifp;
start_time(); /* function call to start time in time_lib.h */
readdata();
head=insert(stdin);
prn_time(); /* function call to print time in time_lib.h */
count(head);
print_list(head);
fclose(ifp);
return 0;
}