-
You want me to post the code again?
Code:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>
struct CALL {
int qty;
char contract[5], owner[5], partnr[6], date1[14], date2[14];
char *callnr;
}CALL;
char *genCallNr();
void createFile(const char *Ppc, struct CALL c);
int main(void){
struct CALL c;
strcpy(c.contract, "DELL");
strcpy(c.owner, "EMEA");
strcpy(c.partnr, "615TW");
strcpy(c.date1, "030320091148");
strcpy(c.date2, "030320091148");
c.callnr = genCallNr();
printf("\n%s", c.callnr);
free (c.callnr);
printf("\n%s\t%s\t%s\t%s\n", c.contract, c.owner, c.partnr, c.callnr);
//createFile(c.callnr);
char *pc;
char cn[12];
pc = cn;
pc = genCallNr();
char *Ppc ;
Ppc = pc;
printf("\n%s", Ppc);
createFile(pc, c);
}
char *genCallNr(){
char *callnr = malloc(12);
time_t now;
struct tm *ts;
char buf[12];
now = time(NULL);
ts = localtime(&now);
strftime(buf, sizeof(buf), "1%y%m%d%H%M", ts);
//printf("\n%s", buf);
strncpy(callnr, buf,12);
// printf("\n%s", callnr);
return callnr;
}
void createFile(const char *Ppc, struct CALL c){
FILE *nf;
nf = fopen(Ppc, "w");
if (nf == NULL) {
fprintf("Can't open created file %s!\n", Ppc);
exit(1);
}
if (nf != NULL) {
fwrite(&c, 1, sizeof(CALL), nf);
fclose(nf);
}
}
-
You mean when you load it into a editor? Yes, you use fwrite() to write to the file, which is a binary format, which means that you get exactly the same format as you have in memory written to the disk. This is particularly bothersome with the callnumber, because you are simply storing the address of the callnr string, which isn't particularly useful [particularly if you free the memory later on (or exit the app and start it again later) and then load the data from the file again, it will point to some random bit of memory - not a good plan].
On the note of storing callnr - it would probably make more sense to store the callnr as a string within the struct. You allocate a small, fixed amount of data every time for callnr. This serves very little purpose - and it probably "costs" more memory than you think - because there is an overhead of about 16 bytes for every allocation, and the size is rounded up to an even 16 or 32 bytes too. So you have a 4 byte pointer in your struct, allocate 12 bytes, and use up about 20 bytes of overhead. Why not just have a 12 long string in the struct, and pass that to the generate-call-number function.
--
Mats
-
matsp, thanks for taking the time to respond. I will have to digest what you have said and try to fix things later. I should get back to my work, unfortunately!
-
Just to simplify things, I'd recommend using a constant size for all strings. It may not be as efficient, but unless your really strapped for memory, it probably isn't going to matter much. Also, instead of returning a dynamically allocated string from genCallNr, why not just write the result directly to callnr?
Code:
#define TEXT_LENGTH 14
struct CALL {
int qty;
char
contract[TEXT_LENGTH],
owner[TEXT_LENGTH],
partnr[TEXT_LENGTH],
date1[TEXT_LENGTH],
date2[TEXT_LENGTH],
callnr[TEXT_LENGTH];
}CALL;
void genCallNr(char *callnr){
time_t now;
struct tm *ts;
now = time(NULL);
ts = localtime(&now);
strftime(callnr, TEXT_LENGTH, "1%y%m%d%H%M", ts);
}
int main(void){
struct CALL c;
strncpy(c.contract, "DELL", TEXT_LENGTH);
strncpy(c.owner, "EMEA", TEXT_LENGTH);
strncpy(c.partnr, "615TW", TEXT_LENGTH);
strncpy(c.date1, "030320091148", TEXT_LENGTH);
strncpy(c.date2, "030320091148", TEXT_LENGTH);
genCallNr(c.callnr);
createFile(&c);
// ...
}
void createFile(struct CALL *c){
FILE *nf;
nf = fopen(c->callnr, "w");
if (nf == NULL) {
fprintf("Can't open created file %s!\n", Ppc);
exit(1);
}
if (nf != NULL) {
fwrite(c, 1, sizeof(CALL), nf);
fclose(nf);
}
}
I didn't actually compile it, but I think the general idea is clear.