小编xQu*_*are的帖子

打印分区表 - C程序

我试图使用C编程语言打印分区表,一切似乎工作正常:打开和阅读,但我不明白为什么它是打印垃圾值.

这是代码:

struct partition
{
    unsigned char drive;
    unsigned char chs_begin[3];
    unsigned char sys_type;
    unsigned char chs_end[3];
    unsigned char start_sector[4];
    unsigned char nr_sector[4];
};

int main()
{
    int gc = 0, i = 1, nr = 0, pos = -1, nw = 0;
    int fd =0;
    char  buf[512] ;
    struct partition *sp;
    printf("Ok ");

    if ( (fd = open("/dev/sda", O_RDONLY | O_SYNC )) == -1)
    {
         perror("Open");
         exit(1);
    }
    printf("fd is %d \n", fd);

    pos = lseek (fd, 0, SEEK_CUR); …
Run Code Online (Sandbox Code Playgroud)

c linux filesystems operating-system partitioning

9
推荐指数
1
解决办法
5096
查看次数

Unix域:connect():没有这样的文件或目录

如标题中所述,我的connect()调用带有相应地址的unix域类型套接字导致错误ENOENT:没有这样的文件或目录.

两个套接字已正确初始化,并相应地创建和绑定套接字文件.服务器和客户端套接字在不同的进程中运行,但客户端进程是fork() - ed和execl() - ed.这也是我解析客户端和服务器套接字的地址,我用它来设置客户端套接字.服务器进程正在使用pthreads.

这是我的connect()尝试:

struct sockaddr_un address;
address.sun_family = AF_UNIX;
memcpy(address.sun_path, filepath.c_str(), filepath.length());
address.sun_path[filepath.length()] = '\0';

if(-1 == connect(this->unix_domain_descriptor_.descriptor(),       \
                (struct sockaddr*)&address,                       \
                size))
{
    global::ExitDebug(-1, "connect() failed", __FILE__, __LINE__);
    return -1;
}
Run Code Online (Sandbox Code Playgroud)

我尝试了不同的大小值,例如:

//  this is from unix(7) man page. It doesn't work neither with nor without "+1"
socklen_t size =  offsetof(struct sockaddr_un, sun_path);
          size += strlen(address.sun_path) + 1;
Run Code Online (Sandbox Code Playgroud)
//  this is from one of my books about linux programming …
Run Code Online (Sandbox Code Playgroud)

c++ unix-socket

8
推荐指数
1
解决办法
1万
查看次数

使用strcpy和equal运算符分配字符串

以下代码有什么区别?

1.

char *p;
strcpy(p,"String");
Run Code Online (Sandbox Code Playgroud)

2.

char *p;
p = "String";
Run Code Online (Sandbox Code Playgroud)

指针指向同一个字符串,但有什么区别吗?

c pointers strcpy

3
推荐指数
1
解决办法
2703
查看次数