1

I have been trying for hours to convert this. I have tried looping through the array casting the chars to longs, but that wouldn't work. I have seen simple examples online, but they don't cover a looping process for longer char arrays. What is a method I could use to convert this array of char (SAMPLE) to one variable of type long?

ar.h

struct  ar_hdr       /* file member header */
     {
         char    ar_name[16];    /* '/' terminated file member name */
         char    ar_date[12];    /* file member date */
         char    ar_uid[6]       /* file member user identification */
         char    ar_gid[6]       /* file member group identification */
         char    ar_mode[8]      /* file member mode (octal) */
         char    ar_size[10];    /* file member size */
         char    ar_fmag[2];     /* header trailer string */
     };

My code

struct ar_hdr sample;
lseek(fileD, 24, SEEK_SET); //fileD is the file desriptor for the opened archive

//I start at 24 because of the 8 byte magic string for an archive starts the file "!<arch>\n"


int numRead = read(fileD, sample.ar_date, 12);
printf(sample.ar_date);

long epoch = (long *) *sample.ar_date;   //terrible coding here
printf("The current time is: %s\n", asctime(gmtime(&epoch)));
6
  • What are you trying to do? Is it the case that every four/eight bytes in the file represent a long or what? Commented Oct 17, 2012 at 19:36
  • if the characters represent a string in decimal format, use the atol function Commented Oct 17, 2012 at 19:39
  • why are you trying to assign epoch, a variable of long, with a pointer to long? also, I guess ar_date is a pointer... But pointer to what? char? Commented Oct 17, 2012 at 19:39
  • 1
    Please post the definition of ar_hdr Commented Oct 17, 2012 at 19:40
  • Bytes 24 through 36 represent the Data for the header ar.h in unix. That's why I use lseek to start at 24 and only read 12 bytes. ar_date is a pointer to the variable in the ar.h struct ar_hdr Commented Oct 17, 2012 at 19:44

1 Answer 1

2

man ar says :

All information in the file member headers is in printable ASCII. The numeric information contained in the headers is stored as decimal numbers (except for ar_mode which is in octal). Thus, if the archive contains printable files, the archive itself is printable.

So atol(ar_date) should do the trick.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.