简体   繁体   English

if 语句返回错误结果

[英]If statement returns wrong result

void test(char *buffer, int size)
{
    int length = strlen(buffer);
    for (int i = 0; i <= length; ++i)
    {
        if (buffer[i] == '"')
        {
            int _size = i + size;
            if (_size > length)
                continue;

            if (buffer[i + size] == '"')
            {

            }
        }
    }
}

This is how I read the file.这就是我阅读文件的方式。

FILE *file = NULL;
size_t filesize = 0;
uint8_t *filebuffer = 0;
            
file = fopen("tokens.txt", "r");
if (file)
{
fseek(file, 0, SEEK_END);
                filesize = ftell(file);
                fseek(file, 0, SEEK_SET);

                filebuffer = calloc(filesize + 1, 1);
                if (filebuffer)
                {
                    fread(filebuffer, 1, filesize, file);

                    for (size_t i = 0; i < filesize; i++)
                    {
                        if (filebuffer[i] == 0)
                            filebuffer[i] = '.';
                    }

                    char array[filesize];
                    strncpy(array, filebuffer, filesize);
                    array[filesize] = '\0';
                    test(array, 59);
                }
            }

"array" is char array[filesize];, "filesize" is ftell(file); "array" 是 char array[filesize];, "filesize" 是 ftell(file); (the file is valid and not NULL) the content of the file is asd"12345678912345678912345678912345678911111231231231231231232"asdasdasdasdasdasdss (文件有效且不为NULL)文件内容为asd"12345678912345678912345678912345678911111231231231231231232"asdasdasdasdasdasdss

在此处输入图像描述

for some weird reason it reaches to the "continue;"出于某种奇怪的原因,它达到了“继续”; when the statement is not true...当陈述不正确时...

Edit: I tried printing the values in the block of the if statement and for some reason I receive ->编辑:我尝试在 if 语句块中打印值,但由于某种原因我收到了 ->
Size: 122尺寸:122
Length: 84长度:84

Someone have any idea on how to solve it?有人对如何解决它有任何想法吗?

array[filesize] = '\0'; // access outside of array boundaries 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM