0

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
  • @nobugz: Don't assume the code is running running somewhere that has a filesystem. Commented Jan 23, 2010 at 23:06
  • Just trying to cover the 99% case where it does. When that's not available, they'll know. Hopefully. Commented Jan 24, 2010 at 0:18

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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.
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?
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.
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: stackoverflow.com/questions/2124957/…
2

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.

Comments

0

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];

];

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.