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

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

最佳答案

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

然后您将整个结构转储到一个文件中,但是因为每个字符串都以空字符结尾,并且因为字符串之间存在未初始化的值(碰巧也包含空字符),您会在您的输出。

我建议您不要只是将内存转储到文件中。正确地完成这项工作要好得多。如何将数据写入文件,如下所示:

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 文档中查找字段宽度修饰符。

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

关于c - 如何将以下内容转换为 char []?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17792050/

10-11 23:14
查看更多