These are not valid comments:
Code:
/ **************************************************
**********/
You have to start with /* and end with */ (no spaces between * and / ). Also, the // style comments are a modern effort, some compilers won't like them, so best stick to /* */ to be portable as poss for now.
You're missing a closing } in the last function, but you probably can't see it easily because the indentation isn't correct.
OK, another thing, what is the file client.dat to contain? Is it binary info made up of struct fclients, or is it pure text? The first is the way to go, but I don't know what you assignment requires. For now though, I'll assume it's the struct version.
If link lists are not allowed (which is one of the best ways to store your data), then can you use arrays? If so, you could make an array of struct fclient's, with say 100 elements. That way your database can hold 100 entries.
Assuming you're not allowed to do that either (which would be pretty dumb), the only other way I can think is to only have the one struct available, and populate it with data from the file, one at a time.
And, some comments on your struct:
Code:
struct fclient
{
int client_code;
char clientname;
char street_address;
char city;
char pros;
char country;
char postalorzip_code;
int deleted; //used as a boolean (0=false, 1=true)
};
The problem here is that each char variable is only one char long. You need to do something more like this:
Code:
#define MAX 100
struct fclient
{
int client_code;
char clientname[MAX];
char street_address[MAX];
char city[MAX];
char pros[MAX];
char country[MAX];
char postalorzip_code[MAX];
int deleted; //used as a boolean (0=false, 1=true)
};
As you're having trouble with the read/write part, here's some example code for you to ponder over. It simply :
- creates a struct fclient with some default info and displays it.
- Opens the client.dat file for binary append writing
- Writes out the struct and closes the file.
- Then, it opens the file again, this time for binary read.
- Loops round, reading each struct in the file and displaying it.
- Closes file, end of prog.
The first time you run this it will create client.dat and store/retrieve one record (struct). But, run it again, and it will store 1 struct, and retrieve 2. Next time, store 1, retrieve 3 and so on.
This is just an example to show you how struct writing/reading can work, you'll need to adapt it to suit your own needs. Hopefully though, it will start you off.
Code:
#include <stdio.h>
#define MAX 100
#define FILENAME "client.dat"
struct fclient
{
int client_code;
char clientname[MAX];
char street_address[MAX];
char city[MAX];
char pros[MAX];
char country[MAX];
char postalorzip_code[MAX];
int deleted; //used as a boolean (0=false, 1=true)
};
void PrintClient(struct fclient *);
int main(void)
{
FILE *fp;
struct fclient new_client = {
1,
"C Coding Ltd",
"1 Code Street",
"Code City",
"Somewhere",
"Code Country",
"c99 s11",
0 };
struct fclient another_client; /* use to receive data FROM the file */
printf ("Before writing to the file:\n");
PrintClient(&new_client);
/* First open file for binary write (append mode) */
if ((fp = fopen (FILENAME, "ab")) == NULL)
{
perror(FILENAME);
return (1);
}
/* Now write one struct out */
if ((fwrite(&new_client, sizeof(struct fclient), 1, fp)) != 1)
{
perror("write");
return (1);
}
fclose(fp);
/* Now open the file again for binary read */
if ((fp = fopen (FILENAME, "rb")) == NULL)
{
perror(FILENAME);
return (1);
}
/* Now loop, reading in all structs in the file */
while ((fread(&another_client, sizeof(struct fclient), 1, fp)) == 1)
{
printf ("\nAfter reading back from the file:\n");
/* each struct loaded gets passed to PrintClient */
/* but it's just as easy to use each one for whatever */
/* we want before the next is loaded */
PrintClient(&another_client);
}
fclose(fp);
return (0);
}
void PrintClient(struct fclient *client)
{
printf ("Client ID:\t%03d\n", client->client_code);
printf ("Client street:\t%s\n", client->street_address);
printf ("Client city:\t%s\n", client->city);
printf ("Client pros:\t%s\n", client->pros);
printf ("Client country:\t%s\n", client->country);
printf ("Client zipcode:\t%s\n", client->postalorzip_code);
printf ("Client deleted:\t%s\n", (client->deleted)?"Deleted":"Active");
}