2012-10-17 101 views
1

我一直在嘗試幾個小時來將其轉換。我已經嘗試循環遍歷數組將字符轉換爲long,但這不起作用。我已經在網上看到了簡單的例子,但是它們沒有涵蓋更長字符數組的循環過程。我可以用什麼方法將這個char(SAMPLE)數組轉換爲long類型的一個變量?如何將char數組轉換爲C中的長變量?

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 */ 
    }; 

我的代碼

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))); 
+0

你想做什麼?是這種情況,文件中的每四個/八個字節代表一個長或什麼? – 2012-10-17 19:36:57

+0

如果字符表示十進制格式的字符串,請使用'atol'函數 –

+0

爲什麼要分配一個epoch,一個long變量,長指針? 也,我猜ar_date是一個指針......但指向什麼?炭? –

回答

2

man ar說:

在文件成員頭部的所有信息是可打印的ASCII。包含在標題中的 數字信息以十進制 數字(除了以八進制表示的ar_mode)存儲。因此,如果檔案 包含可打印的文件,則檔案本身是可打印的。

所以atol(ar_date)應該做的伎倆。

相關問題