Thread: problem getting structure to work in my function

  1. #16
    Registered User
    Join Date
    Mar 2009
    Posts
    37
    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);
        }
    
    }

  2. #17
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #18
    Registered User
    Join Date
    Mar 2009
    Posts
    37
    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!

  4. #19
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    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.
    Last edited by Sebastiani; 05-28-2009 at 10:00 AM.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How properly inherit from template?
    By 6tr6tr in forum C++ Programming
    Replies: 118
    Last Post: 04-25-2008, 04:30 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Replies: 5
    Last Post: 02-08-2003, 07:42 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM