我无法访问字符串中给出的内存地址

问题描述 投票:0回答:1

所以我的问题是我必须将文件的内容读入缓冲区(即 void*)。

我搜索了很多,但找不到任何有关将字符串中的内存地址转换为实际 void* 的信息,并且可能有一个愚蠢的解决方案,但我必须在这里询问。

这是我的代码:

int ReadFile(char *f, void *p, ssize_t cont)
{
    struct stat s;
    ssize_t n;
    int fd;

    if (stat(f, &s) == -1 || (fd = open(f, O_RDONLY)) == -1)
    {
        perror("ERROR: ");
        return -1;
    }
    if (cont == -1) 
        cont = s.st_size;
    if ((n = read(fd, p, cont)) == -1)
    {
        perror("ERROR: ");
        close(df);
        return -1;
    }
    printf("Read %ld bytes from %s in %p\n", n, f, p);
    close(df);
    return 0;
}

我觉得这个功能还不错。

我的问题在于,当我调用它时,程序会要求输入,就像外壳一样,这可以是请求的示例:

> read file.txt 0xffffffff 15

作为 0xffffffff 的内存地址,

read
将发送获取的字节,我的程序将此内存地址作为字符串获取,如下所示:

"read file.txt 0xffffffff 15"

我已经有了将所有这些字符串分成几段的步骤,例如:

"read"
"file.txt"
"0xffffffff"
"15"

但是当我必须向函数

read
传递所需的参数时,我找不到将
"0xffffffff"
转换为
0xfffffff
的方法。

c string pointers memory-address
1个回答
0
投票

我找不到将“0xffffffff”转换为0xffffffff的方法。

unsigned val;
sscanf("0xffffffff", "0x%x", &val);  //you can also "%x"

// now val contains the value needed 

"0xffffffff"
替换为你的字符串。

© www.soinside.com 2019 - 2024. All rights reserved.