gpt4 book ai didi

c - 如何在 C 中将 char 数组转换为变量 long?

转载 作者:太空宇宙 更新时间:2023-11-04 03:00:11 24 4
gpt4 key购买 nike

我已经尝试了几个小时来转换它。我试过遍历数组,将字符转换为长整数,但这行不通。我在网上看过一些简单的例子,但它们没有涵盖较长的 char 数组的循环过程。我可以使用什么方法将这个 char 数组 (SAMPLE) 转换为一个 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 */
};

我的代码

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

最佳答案

man ar 说:

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.

所以 atol(ar_date) 应该可以解决问题。

关于c - 如何在 C 中将 char 数组转换为变量 long?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12941998/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com