Hi,
I'm having some very hard time with my header files when working with linked lists.
I'm not too sure what I need to define in them except for the prototype functions.
The way to create strucs within the header files confuse me.
This first file is the header for the file that works with the linked lists.
//
// linked.h
//
//
// Created by on 12-03-06.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#ifndef _linked_h
#define _linked_h
#define BOOLEAN int
struct NODE
{
char username[50];
char password[50];
char usertype[50];
struct NODE *next;
}*head = 0;
void createList();
BOOLEAN add(struct NODE *p);
BOOLEAN valid (char username[50], char password[50]); a
#endif
As you can see, I choose to declare the struct in the header file. Is there anything wrong with this?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "linked.h"
#include "cipher.h"
#define MAXLENGTH 50
#define ENCRYPT 50
void createList(){
static const char filename[] = "password.csv";
FILE *file;
file = fopen(filename, "r+");
if (file == NULL){
printf("NO PASSWORD FILE");
exit(0);
}
size_t i=0, size = 1000;
char line[MAXLENGTH];
while(i<size && fgest(line,sizeof(line),file)){
NODE *input;
input = (NODE*) malloc(sizeof(NODE));
sscanf(line, "%[^','],%[^','],%s", input->username, input->password, input->usertype);
decrypt(input->username,ENCRYPT);
decrypt(input->password,ENCRYPT);
decrypt(input->usertype,ENCRYPT);
if ( add(input) == 0){
printf("Error: was unable to initialize password validation!!");
exit(0);
}
++i;
}
BOOLEAN add(struct NODE *p){
struct NODE *temp1;
temp1 = (NODE*) malloc(sizeof(NODE));
temp1 = head;
while(temp1->next !=NULL){
temp1 = temp1->next;
}
struct NODE *temp2;
temp2 = (NODE*)malloc(sizeof(node));
temp2->username =p->username; //fill in later
temp2->password =p->password; //fill in later
temp2->usertype =p->usertype; //fill in later
temp2->next = NULL;
temp1->next = temp2;
}
BOOLEAN valid(char username[50], char password[50]){
struct NODE *temp1;
temp1 = head;
while (temp1 != 0){
if((strcmp(temp1->username, username) == 0) && (strcmp(temp1->password, password) == 0)){
return 1;
}
temp1 = temp1->next;
}
return 0;
}
I also constantly get errors on how NODE isn't declared. Also when i try to say
temp2->username =p->username;
temp2->password =p->password;
temp2->usertype =p->usertype;
I've been stuck on this for hours. :(
Any help is appreciated.
Thanks