如何将以下内容转换为char[]?

如何将以下内容转换为char[]?,c,string,operating-system,C,String,Operating System,我使用strcpy和sprintf将一些值(例如长十进制)转换为ar_hdr struct的属性。当我打开归档文件时,它看起来像: 0000000 2 - s . t x t \0 \0 \0 \0 \0 \0 \0 \0 \0 0000020 1 3 7 4 0 8 7 6 3 2 \0 \0 5 0 1 \0 0000040 \0 \0 2 0 \0 \0 \0

我使用strcpy和sprintf将一些值(例如长十进制)转换为ar_hdr struct的属性。当我打开归档文件时,它看起来像:

0000000    2   -   s   .   t   x   t  \0  \0  \0  \0  \0  \0  \0  \0  \0
0000020    1   3   7   4   0   8   7   6   3   2  \0  \0   5   0   1  \0
0000040   \0  \0   2   0  \0  \0  \0  \0   1   0   0   6   4   4  \0  \0
0000060    8   2  \0  \0  \0  \0  \0  \0  \0  \0   `  \n   2   2   2   2
0000100    2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2
*
0000140    2   2   2   2  \n   2   2   2   2   2   2   2   2   2   2   2
0000160    2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2
0000200    2   2   2   2   2   2   2   2   2   2   2   2   2  \n        
0000216
我想在“”时替换所有“\0”(只是一个空白)。有人能给我一个如何解决这个问题的提示吗?谢谢 下面是我写的一些代码

int main (int argc, char **argv)
{
    char *archive = argv[1];
    char *read_file = argv[2];

    int in_fd;
    int out_fd;

    //char title[] = ARMAG;   //constant define in ar.h
    char buf[BLOCKSIZE];
    char tmpBuf[BLOCKSIZE];

    int num_read;
    int num_written;

    struct stat stat_file;
    struct ar_hdr my_ar;

    FILE *fp;

    //open read_file (i.e., text file)
    if (stat(read_file, &stat_file) == -1){
        perror("Error in Stat");
        exit(-1);
    }


    //open read file
    in_fd = open(read_file, O_RDONLY);
    if (in_fd == -1) {
        perror("Canot open/create output file");
        exit(-1);
    }

    //assign file info to struct dhr (my_ar)
    //all my_ar attributes = char []
    strcpy(my_ar.ar_name, read_file);
    sprintf(my_ar.ar_date, "%ld", stat_file.st_mtimespec.tv_sec);
    sprintf(my_ar.ar_uid, "%ld", (long)stat_file.st_uid);
    sprintf(my_ar.ar_gid, "%ld", (long)stat_file.st_gid);
    sprintf(my_ar.ar_mode, "%o", stat_file.st_mode);
    sprintf(my_ar.ar_size, "%lld", stat_file.st_size);
    strcpy(my_ar.ar_fmag, ARFMAG);




    //0666 - open archive
    out_fd = open(archive, O_CREAT | O_WRONLY | O_APPEND, 0666);
    if (out_fd == -1) {
        perror("Canot open/create output file");
        exit(-1);
    }


    //write my_ar struct to archive
    //num_written = write(out_fd, title, sizeof(title));
    off_t curpos = lseek(out_fd, SEEK_CUR, 0);  //get current position
    if(curpos == 0) {                           //if it is at beginning, it must be a new file
        num_written = write(out_fd, ARMAG, SARMAG);
        num_written = write(out_fd, &my_ar, sizeof(my_ar));
    }
    else
    {
        num_written = write(out_fd, &my_ar, sizeof(my_ar));
    }


    //add the file content to archive
    off_t in_fd_pos = lseek(in_fd, 0, SEEK_CUR);
    while ((num_read = read(in_fd, buf, BLOCKSIZE)) >0) {
        num_written = write(out_fd, buf, BLOCKSIZE);

        if (num_written != num_read) {
            perror("Error writing file");
            exit(-1);
        }
    }

    return 0;
}

根据
sprintf()
的手册页:

成功返回后,这些函数返回 打印的字符(不包括用于结束输出的
null
字节) 字符串)

因此,
sprintf()
为每个成员字段添加了一个
'\0'
。每个成员字段的其余
'\0'
s可能是由于
my\u ar
的初始化值造成的。您可以运行一个
expt
,通过将
my_ar
初始化为
non-null
值来查看情况是否如此


sprintf()
返回写入的字符数。因此,可以使用该字段将
'\0'
替换为空格

您的代码似乎将一些文本写入结构中的一些固定长度字符串中,但您没有为
struct ar_hdr
提供定义,因此我不能确定

然后将整个结构转储到一个文件中,但由于每个字符串都以nul结尾,而且字符串之间存在未初始化的值(碰巧也包含nul字符),因此输出中会出现不需要的垃圾

我建议你不要只是将内存转储到文件中。把工作做好要好得多。将数据写入如下文件如何:

FILE *out_file = fdopen(out_fd);  /* or use fopen in the first place */
fprintf("%s %s %s %s %s %s", my_ar.ar_name, my_ar.ar_date, my_ar.ar_uid,
        my_ar.ar_gid, my_ar.ar_mode, my_ar.ar_size);
如果仍然需要固定长度字符串,请在printf文档中查找字段宽度修饰符


更好的是,根本不要将数据存储为字符串,而是直接从原始数据(副本)打印出来。

all NUL到一个空格<代码>ptr=str;do{ptr=memchr(ptr,0,长度);if(ptr)*ptr++='';}while(ptr)你这样做似乎很奇怪。结构中的预期内容是什么?值应该左填充还是右填充?它们应该填充空格还是空值?使用宽度说明符并在将其复制到结构上之前打印到缓冲区似乎是解决问题的最佳解决方案,但我不太确定问题是否真的是我所认为的那样。谢谢你的提示。你能告诉我更多关于如何初始化非空值的细节吗?另外,如何用空格替换\0?对不起,我对C和编程非常陌生。+要回答,请按照我给你答案的格式。@Grijesh Chauhan:我不清楚如何用空格替换“\0”。你能给我更多的提示吗?谢谢。@user2203774我说我投票并格式化了你的答案(因为它是正确的),我还想让你学习格式化,所以我说
查看这个
,这是上面评论中的一个链接