ctime and sting copy

This is a discussion on ctime and sting copy within the C Programming forums, part of the General Programming Boards category; I am trying to convert a field from the stat info of a file into a string. I have created ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    32

    ctime and sting copy

    I am trying to convert a field from the stat info of a file into a string.

    I have created a typedef of time_type which is a char array of 50.

    The code I am trying to use is

    *strcpy(out_file.time_last_mod, ctime(&file_info.st_mtime);

    and I am getting a parse error.

    out_file is defined as a struct with time_last_mod as a time_type field, and st_mtime comes straight from the stat type info.

    can anyone please suggest why the parse error?

    Many thanks
    sue

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    95
    time structs arent strings, therefore it shouldnt be a strcpy, for the use of dos.h (I know it isnt 100% compatible), it would be something like:

    Code:
      struct time t1;
      struct time t2;
    
      gettime(&t1);
    
      t2.ti_hour = t1.ti_hour;
      t2.ti_min = t1.ti_min;
      t2.ti_sec = t1.ti_sec;
    all the values of hour, min and sec are integers. Hope this helps

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    32
    Thanks Bull,

    I understood that the function ctime should convert a time to a string.....is that correct?

  4. #4
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    That's true:

    Code:
    time_t my_time;
    
    time( &my_time );
    strcpy(out_file.time_last_mod, ctime(&my_time);
    Also take a look at the localtime function.
    Check the manual pages.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    It's missing a parenthesis on the end, maybe you left it off when posting.
    >*strcpy(out_file.time_last_mod, ctime(&file_info.st_mtime);

    strcpy(out_file.time_last_mod, ctime(&file_info.st_mtime));

  6. #6
    B26354 Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633

    Re: ctime and sting copy

    Originally posted by Sue Paterniti
    *strcpy(out_file.time_last_mod, ctime(&file_info.st_mtime);

    can anyone please suggest why the parse error?
    Hi Sue,

    If you posted your strcpy statement exactly how you have it in your program, then the parse error is the astrisk in front of strcpy(). Remove it and try to compile.

    Also, Swoopy is correct in saying you are missing a closing parenthesis. If the structure member 'time_last_mod' is a character array, Swoopy's example should work fine.
    Jason Deckard

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    32
    Thanks everyone,

    Your help is appreciated - I have made the modifications and the compile is now telling me
    "passing arg 2 of strcpy makes pointer from integer without a cast"

    It seems to be unhappy with the ctime bit of the strcpy.

    Any thoughts....

    Thanks
    Sue

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >"passing arg 2 of strcpy makes pointer from integer without a cast"
    You did include time.h, right? If you don't include the header file with the proper function declaration then the compiler will assume an unknown function to return int. Also check your spelling.

    If it's neither of those then post your code so that it's easier to identify the problem.

    -Prelude
    My best code is written with the delete key.

  9. #9
    Registered User
    Join Date
    Apr 2002
    Posts
    32

    whole code

    I thought I would post the whole pgm - in case something else is causing the error.

    I guess more info is better than less.......
    Thanks Sue vi bistruct.c
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <stdio.h>
    #include <string.h>



    #define NAMESIZE 256
    #define TIMESIZE 50

    typedef char name_type[NAMESIZE + 1];
    typedef char time_type[TIMESIZE +1];

    //typedef struct
    //{
    // char in_name[NAMESIZE];
    //} file_in;

    typedef struct
    {
    name_type name; /*File or directory name*/
    u_short mode; /*protection and file type*/
    short user_id; /*User ID of owner*/
    short group_id; /*Group ID of owner*/
    off_t size; /*file size in bytes*/
    time_type time_last_mod; /*modify time as string*/
    } file_sig;


    main(int argc, char *argv[])
    {

    struct stat file_info; /* define the stat variable*/
    file_sig out_file; /* define the output file variable*/

    name_type in_file; /* define the input file variable*/

    // initialise the input and output file names
    //in_file_name = argv[2];

    //out_list_name = argv[3];

    // The file pointers
    FILE *fp,*fp1; /*The output file*/
    //FILE *fp1; /*The input file*/


    // open the input and output files

    // if (( fp = fopen(argv[2], "r")) == NULL)
    // {
    // perror(argv[2]);
    // exit(1);
    // }

    // fp1 = fopen(argv[3], "wb");
    fp = fopen("/home/spaterniti/wrkshops/filelist", "r");
    fp1 = fopen("/home/spaterniti/wrkshops/b1", "wb");

    // if (( fp1 = fopen(argv[3], "wb")) == NULL)
    // {
    // perror(argv[3]);
    // exit(1);
    // }

    // while there are records read the text file
    // and collect the stat information for the selected file
    // and write a modified stat record to the output file

    while (!feof(fp))
    {
    printf("%c", &in_file);

    fscanf(fp, "%c", &in_file);
    stat(in_file, &file_info);


    // create the output data fields from the stat'd data

    strcpy(out_file.name, in_file);
    //printf("%s\n", &in_file);
    //printf("%s\n", &out_file.name);

    out_file.mode = file_info.st_mode;
    out_file.user_id = file_info.st_uid;
    out_file.group_id = file_info.st_gid;
    out_file.size = file_info.st_size;
    strcpy(out_file.time_last_mod, ctime(&file_info.st_mtime));


    //stat("/home/spaterniti/wrkshops/ws01.1.c", &file_info);

    // read each record from the list file
    // and collect and store the stat information
    fwrite(&out_file, sizeof(file_sig),1,fp1);
    }


    // close the files
    fclose(fp);
    fclose(fp1);

    exit(0);
    }

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I think Prelude has it figured out. Just #include <time.h>, and it should fix that error.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21