Hey all, its my first post here and I'm looking forward to learning c with some of the other newbs like me.
My professor assigned a program that must read in lines from a file and print the last n number of lines in the most efficient way, so reading thru the file once to obtain a line count is out of the question, i must use a circular linked list. I have this code so far which seems like its just a few lines away from success.
Please help, thank you.
Input file
Code:
aaa
bbb
ccc
ddd
eee
fff
ggg
hhh
My code
Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node{
char string[80];
int value;
struct node *next;
};
int main()
{
void insert(struct node **, struct node **, int);
struct node *head, *tail, *temphead;
int num=0; int i = 0;
FILE *finp;
char *infile = "inp.dat";
finp = fopen(infile, "r");
head = tail = temphead = NULL;
printf("Enter list size\n");
scanf("%d", &num);
while(i<=num)
{
insert(&head, &tail, i);
i++;
}
temphead = head;
while(head->next != head)
{
if((fgets(head->string, 81, finp))!=NULL)
{
head = head->next;
printf("how many");
}
else
break;
}
head = temphead;
i=0;
while(head != tail)
{
i++;
printf("%s", head->string);
head = head->next;
}
}
void insert(struct node **h, struct node **t, int val)
{
struct node *temp;
temp = (struct node*)malloc(sizeof(struct node));
temp->value = val; temp->next = *h;
if(*h == NULL)
{
*h = *t = temp;
}
else
{
(*t)->next = temp; *t = (*t)->next;
}
}
my output
it shoud be putting the ddd in front and bumping the rest down.
Thanks!