Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/grails/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C读取conf文件_C_File - Fatal编程技术网

C读取conf文件

C读取conf文件,c,file,C,File,您好,我在尝试读取conf文件时遇到问题(逐行) 这是我的代码: bool EndWith(const char* haystack, const char* needle) { bool rv = 0; if (haystack && needle) { size_t needle_size = strlen(needle); const char* act = haystack; while (NULL

您好,我在尝试读取conf文件时遇到问题(逐行)

这是我的代码:

bool EndWith(const char* haystack, const char* needle)
{
    bool rv = 0;
    if (haystack && needle)
    {
        size_t needle_size = strlen(needle);
        const char* act = haystack;
        while (NULL != (act = strstr(act, needle)))
        {
            if (*(act + needle_size) == '\0')
            {
                rv = 1;
                break;
            }
            act += needle_size;
        }
    }

    return rv;
}
    FILE *file2 = fopen ("config.conf", "r");
    const size_t line_size = 300;
    char* line = malloc(line_size);
    while (fgets(line, line_size, file2) != NULL)  {
        puts(line);

        if(EndWith(line, "toto")) {
            puts("yes");
        }
    }
和我的conf文件:

vm.user_reserve_kbytes = 131072 toto
vm.vfs_cache_pressure = 100 toto
vm.zone_reclaim_mode = 0 toto
我的代码返回2“是”,但如果我的conf文件,如果我在第一行中添加换行符,我会读取我的3行代码,我的代码返回3“是” 为什么?

我想返回3“是”,而不需要在conf文件的第0行中添加换行符

错误报告:

vm.user_reserve_kbytes = 131072 toto

yes
vm.vfs_cache_pressure = 100 toto

yes
vm.zone_reclaim_mode = 0 toto
正确返回(添加换行符)


我测试了解决方案Filip Kočica(感谢您的帮助),但我有同样的问题:

守则:

bool EndWith(const char* haystack, const char* needle)
{
    if (haystack == NULL || needle == NULL)
    {
        return false;
    }
    const char* p;
    if ((p = strstr(haystack, needle)) != NULL)
    {
        if (!strcmp(p, haystack + strlen(haystack) - strlen(needle)))
        {
            return true;
        }
    }
    else
    {
        return false;
    }
    return false;
}



int main(int argc, char **argv)
{
        FILE *file2 = fopen ("config.conf", "r");
        const size_t line_size = 300;
        char* line = malloc(line_size);
        while (fgets(line, line_size, file2) != NULL)  {
            puts(line);

            if (EndWith(line, "za"))
            {
                puts("it does.");
            }

        }



        return 0;
}
我的文件config.conf:

vm.user_reserve_kbytes = 131072 za
vm.vfs_cache_pressure = 1001 za
vm.zone_reclaim_mode = 01 za
1]

返回值为2,因为最后一行没有以新行字符结尾
\n

每次调用getline时,它都会从文件中给出一行(如果还有另一行的话)。 不需要检查此行是否以
NL
字符结尾。 只需计算getline没有返回
EOF
的次数

获取
使
处于不正常状态

while (fgets(line, line_size, file2) != NULL)
{
    puts(line);
    puts("yes");

    if(EndWith(line, /*special strings*/ ))
    {

    }
}
2]

配置文件:

vm.user_reserve_kbytes = 131072 za
vm.vfs_cache_pressure = 1001 za
vm.zone_reclaim_mode = 01 za
输出:

it does.
vm.user_reserve_kbytes = 131072 za

it does.
vm.vfs_cache_pressure = 1001 za

it does.
vm.zone_reclaim_mode = 01 za

什么是空行?我想他说的是空行。你的文件是通过
\n
完成的吗?(如果没有,最后一行不会在
\n
之前结束,因此不会打印
yes
)我编辑我的邮件,它不是“空格行”,而是“换行符”,很抱歉,文件的最后一行结尾没有换行符。我想检查我的行是否以特殊字符串结尾,我受此链接启发:但是的,我认为我与eofOk有问题,所以请检查特殊字符串,但不检查
\n
:-)是的,我编辑了我的邮件,我想检查我的行是否以“toto”结尾。我为您做了这件事,它应该可以工作。只需将函数
end替换为
。谢谢您的帮助,但我也有同样的问题,我将您的代码添加到新消息中
vm.user_reserve_kbytes = 131072 za
vm.vfs_cache_pressure = 1001 za
vm.zone_reclaim_mode = 01 za
it does.
vm.user_reserve_kbytes = 131072 za

it does.
vm.vfs_cache_pressure = 1001 za

it does.
vm.zone_reclaim_mode = 01 za