C 在while循环中连接字符串 [英] C Concatenate string in while loop

查看:26
本文介绍了C 在while循环中连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将结构的一部分与十六进制值连接起来.我遍历循环中的每个字节并转换为十六进制,然后我想将所有十六进制连接成一个长字符串.

I'm trying to concatenate part of a struct with hex values. I run over every byte in the loop and convert to hex, then I want to concatenate all the hex into one long string.

然而,我在循环结束时只得到一个值.由于某种原因,字符串没有正确连接.知道我做错了什么吗?

However, I only end up with one value at the end of the loop. For some reason the string isnt concatenating properly. Any idea what Im doing wrong?

typedef struct OPTIONS_STR
{
    int max;
    int printName;
} OPTIONS;

void set_default_options(OPTIONS *options)
{
    options->max = -1;
    options->printName = 0;
}

void do_file(FILE *in, FILE *out, OPTIONS *options)
{
    char ch;
    int loop = 0;
    char buf[81];
    buf[0] = '';
    int sz1;
    int sz2;
    int sz3;

    int seeker = offsetof(struct myStruct, contents.datas);

    //find total length of file
    fseek(in, 0L, SEEK_END);
    sz1 = ftell(in);

    //find length from beggining to struct beginning and minus that from total length
    fseek(in, seeker, SEEK_SET);
    sz2 = sz1 - ftell(in);

    //set seek location at beginning of struct offset
    fseek(in, seeker, SEEK_SET);

    sz3 = sz2 + 1;
    char buffer[sz3];
    char msg[sz3];

    buffer[0] = '';

    while (loop < sz2)
    {
        if (loop == sz2)
        {
            break;
        }

        fread(&ch, 1, 1, in);
        sprintf(msg, "%02X", (ch & 0x00FF));
        strcpy(buffer, msg);

        ++loop;
    }
    printf("%s
", buffer);
}

int main(int argc, const char * argv[]) {

    OPTIONS options;
    set_default_options(&options);

    const char *current = "/myfile.txt";
    FILE *f = fopen(current, "rb");
    do_file(f, stdout, &options);
    fclose(f);

};

推荐答案

使用 strcat 而不是 strcpy.那应该可以解决您的问题.

Use strcat instead of strcpy. That should fix your problem.

为了提高效率,可以考虑使用像 char *p = buffer 这样的写指针,并使用类似 p += sprintf(p, "%02X", (ch & 0x00FF))

For efficiency look into using a write pointer like char *p = buffer and advance the write position with something like p += sprintf(p, "%02X", (ch & 0x00FF))

此外,您的 if(loop == sz2) break 检查是 while(loop < sz2) 检查的无用重复.如果 loop 等于或大于 sz2,while 循环将不会执行.

Also your if(loop == sz2) break check is a useless duplicate of the while(loop < sz2) check. The while loop won't execute if loop is equal or bigger than sz2.

还想知道为什么当您只需要一个字符时要使用 fread.fgetcgetc 似乎是更好的选择.

Also wondering why you use fread when you only want one character. fgetc or getc seems to be a better choice.

此外,无论您使用 fread 还是 getc,您都需要检查文件的结尾.如果文件中没有sz2字节怎么办?因为所有现代系统都是多进程和多用户的,所以有人可能会在调用 ftell 后缩短文件.你永远不应该假设事情,因为即使你只是检查它,它可以改变.做出这种假设是导致 TOCTTOU(检查时间到使用时间)错误的原因.

Also, no matter if you use fread or getc you need to check for the end of file. What if the file does not have sz2 bytes in it? Because all modern systems are multiprocess and multiuser, so someone might cut the file short after the call to ftell. You should never assume things because even if you just checked it, it can change. Making that assumption is what causes TOCTTOU (Time of Check To Time Of Use) bugs.

这篇关于C 在while循环中连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆