Hello folks,
I hope my question is not wasting anyones time here. I was trying to make a program that will assist me in my daily work, the idea being to automatically generate a file that contains some inforamation about products and so forth.
I decided to put this information into a structure and then create a file and read the contents of struct to it.
I can get the program to work when I do not use a seperate function, but i cannot seem to get the program to work while having a seperate function. My knowledge of passing structures seems to be a bit limited.
here is the function:
Code:
void createFile(char pc, struct CALL){
FILE *nf;
nf = fopen(pc, "w");
if (nf == NULL) {
fprintf("Can't open created file %s!\n", pc);
exit(1);
}
if (nf != NULL) {
fwrite(&c, 1, sizeof(CALL), nf);
fclose(nf);
}
}
function prototype:
Code:
void createFile(char filename, struct CALL);
and the call to the function:
I really tried lots of different attempts and looked at many websites and resource but i still get the same problems. Can someone point out where I went wrong...sure there are numerous errors.
Anyway this is the program that works without function(so you know that i am trying )
Code:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>
char *genCallNr();
//void createFile(char *callnr);
struct CALL {
int qty;
char contract[5], owner[5], partnr[6], date1[14], date2[14];
char *callnr;
};
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);
int i;
FILE *nf;
char *pc;
char cn[12];
pc = cn;
pc = genCallNr();
printf("\n%s", pc);
nf = fopen(pc, "w");
if (nf == NULL) {
fprintf("Can't open created file %s!\n", pc);
exit(1);
}
if (nf != NULL)
fwrite(&c, 1, sizeof(struct CALL), nf);
fclose(nf);
}
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(char *callnr){
FILE *nf;
nf = fopen(callnr, "w");
if (nf == NULL) {
fprintf("Can't open created file %s!\n", callnr);
exit(1);
}
if (nf != NULL) {
fwrite(callnr, 1, sizeof(callnr), nf);
fclose(nf);
}
}
*/
there are problems with the information that is writen into the file by the above program but i will tackle them when the problem with the function is resolved...