Thread: How To Correctly Output of Struct Objects

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    96

    How To Correctly Output of Struct Objects

    I am having trouble displaying items such as the and Size of a file when I access the stat() function.

    http.c:234: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘__off_t’

    The same goes from when I try obtain the date using the struct tm and try to assign it to a buffer to be written to a client socket using:

    Code:
    	time_t tm;
    	tm = time(NULL);
    	ptr = localtime(&tm);
           sprintf(buf, "%s", ptr);
    I get:

    http.c:233: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘__time_t’

    How would I set the arguments up or cast these items so I no longer have these issues. Thanks

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    They return structs filled with information. You have to pick what you want to know from the struct.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    96
    I need to know the file size and last modification time as well as the well date today. I know that file size type is off_t and modification is of type time_t. How would I go about writing these to the screen or saving them correctly to my buffer to be outputted?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Members in the struct passed to stat:

    st_mtime
    Time of last modification of file.

    st_size
    Size of the file in bytes; a 64-bit integer for variations with the i64 suffix.

    As well, time_t is an int or a long, I believe.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by NuNn View Post
    I need to know the file size and last modification time as well as the well date today. I know that file size type is off_t and modification is of type time_t. How would I go about writing these to the screen or saving them correctly to my buffer to be outputted?
    You can either print the individual parts of the date/time structure returned by localtime() - it has fields like year, minute and such. Or you could use for example strftime() which will make a string from a time parameter - similar to sprintf(), but you have different "%..." parameters describing how to format the time-string (so you can have 2009-Jan-21 or 01/21/2009, for example).

    --
    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.

  6. #6
    Registered User
    Join Date
    Jun 2003
    Location
    Austria
    Posts
    55
    localtime( ) will return a struct tm*

    struct tm is defined as

    Code:
    struct tm {
        int tm_sec;         /* seconds */
        int tm_min;         /* minutes */
        int tm_hour;        /* hours */
        int tm_mday;        /* day of the month */
        int tm_mon;         /* month */
        int tm_year;        /* year */
        int tm_wday;        /* day of the week */
        int tm_yday;        /* day in the year */
        int tm_isdst;       /* daylight saving time */
    };
    to print a time e.g.

    Code:
    printf("%d:%d", ptr->tm_hour, prt->tm_min);

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Oh yes, it wants a time_t as argument. Dang C functions. Sometimes so confusing and so difficult to use.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Nov 2007
    Posts
    96
    Thank you guys very much , is there an issue with the tm_year variable as I am getting "109" as my year each time? Maybe y2k? lol

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    MSDN says:
    Year (current year minus 1900).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Nov 2007
    Posts
    96
    Alrighty, I have the time working appropriately but I am still having trouble with my stat output.

    Quote Originally Posted by Elysia View Post
    Members in the struct passed to stat:

    st_mtime
    Time of last modification of file.

    st_size
    Size of the file in bytes; a 64-bit integer for variations with the i64 suffix.

    As well, time_t is an int or a long, I believe.
    I have tried printf() with the signed decimal and floating point tags for both the file modification time and size but still am not receiving the proper output. What are the flags or cast which I need to use?:

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by NuNn View Post
    Alrighty, I have the time working appropriately but I am still having trouble with my stat output.



    I have tried printf() with the signed decimal and floating point tags for both the file modification time and size but still am not receiving the proper output. What are the flags or cast which I need to use?:
    Like discussed above, file modification time is a time_t type. Size will be a type off_t, which I expect is a "long long", which you would output with "%lld" or some such.

    --
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  2. What's wrong with my search program?
    By sherwi in forum C Programming
    Replies: 5
    Last Post: 04-28-2006, 09:57 AM
  3. Search Engine - Binary Search Tree
    By Gecko2099 in forum C Programming
    Replies: 9
    Last Post: 04-17-2005, 02:56 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 05:27 AM