tags:

views:

102

answers:

4

I want to create a fault log. The fault log should be able to store the last 10 faults.

A fault has 3 piece of information: 1. Number of fault. 2. Fault name. 3. Date time of fault.

How can i do this using structs?

Or should i do it using arrays and if so how?

The fault log is for storage in memory only.

+2  A: 

I assume you want to store it in the memory, then you can use a combination of struct and array.

Something like will following will do:

typedef struct {
    int number;
    char* name; // You can use an array instead char name[MAX_FAULT_NAME_LENGTH]
    int timestamp;
} fault_entry;

fault_entry fault_log[10];

Of course this is hand-waving. If you want to store it to a file, you need to serialize. You need to think about what data-type to use for date/time and name. But it should help you get started.

Szere Dyeri
As you mentioned, it would be better to use a fixed width char array for the name string for cases where you would want to serialize.
Sean A.O. Harney
yeah i just want to store it in memory. i see what you did there. Is there a way i can order the array to keep it in order of newest fault first and oldest last?
Anonymouz
Yes, you can. In order to do that, everytime before you insert an element you copy all existing elements to the next position and drop the last element. Insert you new element at position 0. It is better to keep char* if you want to do these operations often vs an char array there.
Szere Dyeri
A better option that moving all the elements around every time you add one is just to keep the index of the current "top" element, and increment that (modulo 10) whenever you replace the current "top". This is a circular buffer, as per Mark Wilkins' answer: http://stackoverflow.com/questions/2124957/using-structs-or-multidimensional-arrays-in-c/2125005#2125005
caf
A: 

You should use an array of the struct type such as

#define NAME_MAXLEN 20
struct fault {
     int number;
     time_t time;
     char name[NAME_MAXLEN];
};

struct fault faults[10];

];

Sean A.O. Harney
A: 

Something along the lines of:

typedef struct fault
{
    int number;
    char *name;
    char *date;
} fault;

fault faults[10];

faults[0].number = 1;
faults[0].name = "Fault Number 1";
faults[0].date = "Today's Date";

/*etc*/

printf("%d\n", faults[0].number);
printf("%s\n", faults[0].name);
printf("%s\n", faults[0].date);

You will need to decide what time type to use of course. Here, i've used a string.

Gary Willoughby
+2  A: 

A log usually implies some kind of more permanent storage, which might mean that it should be written to a file. If so, then a structure is not necessarily required. It could be implemented as a function that accepts the required information and generates the other information (e.g., time/date).

But if it is indeed more of a temporary type of storage, then it could be stored in a simple circular array. Keep an index of the current position in the array and write to that position.

typedef struct {
   int faultNumber;
   char faultName[50];  // length should probably be a #define
   char faultDate[20];  // date in C could be stored in some kind of char array.
                        // or it could be something representing results of something
                        // like a time_t result.
} LOG_ENTRY;

LOG_ENTRY LOGS[10];
int iCurPos = 0;

Then add an entry at the current position and increment iCurPos and loop it back to 0 when it hits the end.

Mark Wilkins