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

查看:334
本文介绍了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] = '\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] = '\0';

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

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

        ++loop;
    }
    printf("%s\n", 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,都需要检查文件结尾.如果文件 not 中没有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天全站免登陆