To my understanding, there is a way to parse input such that:
A million $ exit $$$ 16
The Cheit and its Punishment $$$ 8
War and Remembrance $$$ 12
Winds of War $$$ 12
How to Play Football $$$ 12
Ultrashort Pulses $$$ 8
Nonlinear Optics $$$ 8
etc..
Where the "$$$" separates between fields of data.
I'm looking to upgrade the phrase:
sscanf(line, " %200[^$][^$][^$]$$$%ld", name, &copies);
so it would fit line no. 1 in the example.
EDIT:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define NAME_LENGTH 200
#define ERROR -1
typedef int BOOL;
#define TRUE 1
#define FALSE 0
typedef struct book{
char name[NAME_LENGTH];
long copies;
struct book *next;
} Book;
Book* create_book(char name[], long copies){
Book *new_book = (Book*) malloc(sizeof(Book));
if (new_book != NULL) {
strcpy(new_book->name, name);
new_book->next = NULL;
new_book->copies = copies;
}
return new_book;
}
Book* add_first(Book *head, char name[], long copies){
Book *new_book = create_book(name, copies);
if (new_book == NULL)
return NULL;
new_book->next = head;
return new_book;
}
Book* add_last(Book *head, char name[], long copies){
Book *tail;
Book *new_book = create_book(name, copies);
if (new_book == NULL)
return NULL;
if (head == NULL)
return new_book;
tail = head;
while (tail->next != NULL)
tail = tail->next;
tail->next = new_book;
return head;
}
Book* add_sorted(Book *head, char name[], long copies){
Book* iter, *prev = NULL;
Book* new_book = create_book(name, copies);
if(new_book == NULL)
return head;
if (head == NULL)
return new_book;
if (!strcmp(new_book->name, head->name)){
new_book->next = head;
return new_book;
}
iter = head;
while ((iter != NULL) && (strcmp(new_book->name, head->name))){
prev = iter;
iter = iter->next;
}
prev->next = new_book;
new_book->next = iter;
return head;
}
int length(const Book *head){
if (head == NULL)
return 0;
return 1 + length(head->next);
}
void free_library(Book *head_book){
if (head_book == NULL)
return;
free_library(head_book->next);
free(head_book);
}
Book* find_book(Book *head, char name[]){
if (head == NULL)
return NULL;
if (strcmp(head->name, name) == 0)
return head;
find_book(head->next, name);
return NULL;
}
Book* delete_book(Book *head, char name[]){
Book *iter = head, *prev = NULL;
if (head == NULL)
return head;
if ((!strcmp(head->name, name)) == 1){
iter = head->next;
free(head);
return iter;
}
while (iter->next != NULL){
if ((!strcmp(head->name, name)) == 1){
prev->next = iter->next;
free(iter);
break;
}
prev = iter;
iter = iter->next;
}
return head;
}
Book* initBooksList(FILE *input){
Book *head_book = NULL, *existing_book = NULL;
long copies = 0;
char line[256] = {0}, name[NAME_LENGTH];
if (input == NULL){
printf("File did not open. Exit..\n");
return NULL;
}
while(!feof(input)){
if((fgets(line, 256, input) != NULL) && (head_book == NULL)){
sscanf(line, " %200[^$][^$][^$]$$$%ld", name, &copies);
printf("%s\n%ld\n", name, copies);
head_book = create_book(name, copies);
strcpy(line, "");
strcpy(name, "");
copies = 0;
}
else{
sscanf(line, " %200[^$][^$][^$]$$$%ld", name, &copies);
existing_book = find_book(head_book, name);
if(existing_book != NULL){
existing_book->copies += copies;
printf("%s\n%ld\n", name, existing_book->copies);
}
else{
add_sorted(head_book, name, copies);
printf("%s\n%ld\n", name, copies);
strcpy(line, "");
strcpy(name, "");
copies = 0;
}
}
}
return head_book;
}
void storeBooks(Book *head_book){
}
void returnBook(Book *head_book){
}
void borrowBook(Book *head_book){
}
int main(int argc, char *argv[]){
int i = 0;
FILE *ptr;
printf("%d\n", argc);
for(i = 0; i < argc; i++)
printf("argv[%d] = %s\n", i, argv[i]);
ptr = fopen(argv[1], "r");
initBooksList(ptr);
return 0;
}