C:Getline不会读取完整的文档

发布于 2025-01-30 02:12:27 字数 801 浏览 3 评论 0原文

我需要在带有getline的大XML文件中编写C读取程序,问题只有大约30亿行的2085。在以下代码中,问题发生。

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

int main()                                          
{
    size_t l = 0;
    char *line;

    line = NULL;
    while (getline(&line,&l,stdin)>0) {

        getline(&line,&l,stdin);
        printf("%s\n", line);
        printf("%i\n", i);

        free(line);
        line = NULL;
    }
    return 0;
}

我的第一个目标是用printf显示线条,以便我可以使用。 2080年直到2090年的行都不是空的。 如果我将循环条件更改为&gt; = 0约80.000,并且如果我将printf评论为130.00行。但是相反,我会遇到一个细分错误。在这种情况下,我真的不知道该如何解决。 我正在使用Visual Studio代码编辑和Mysys2 mingw在Windows 11上运行代码。 首先找不到getline函数,我通过复制将这个页面,因为#include头球似乎不起作用。

I need to write a program in C reading in a big XML file with getline, the problem is only 2085 of the approxemately 3 billion lines are read. In the following code the problem occurs.

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

int main()                                          
{
    size_t l = 0;
    char *line;

    line = NULL;
    while (getline(&line,&l,stdin)>0) {

        getline(&line,&l,stdin);
        printf("%s\n", line);
        printf("%i\n", i);

        free(line);
        line = NULL;
    }
    return 0;
}

My first goal would be to display the lines, with printf so I can work with that.
Neither line from 2080 til 2090 is empty.
If I change the loop-condition to >=0 about 80.000 are read in and if I comment the printf out, about 130.00 lines. But instead I get an segmentation error. Which I really don't know how to solve in this case.
I am using Visual Studio Code to edit and MYSYS2 MinGW to run the Code, on Windows 11.
First the getline Function couldn't be found, I resolved it by Copy-Pasting the getline Code of this page, because the #include header's seemed not to work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

懒的傷心 2025-02-06 02:12:27
  1. 在阅读中的行时,您会跳过每一行,而则在中再次阅读,然后在当时的身体中再次读取它,
  2. 您的内存泄漏(与点1相同的原因)
int main(void)                                          
{
    size_t l = 0;
    ssize_t retVal;
    char *line;

    line = NULL;
    do 
    {
        if((retVal = getline(&line,&l,stdin)) != -1)
        {
            printf("Buff_size:%zu Line_length: %zd Line:\"%s\"\n", l, retVal, line);

        }
    }while(retVal != -1);
    free(line);
    return 0;
}
  1. You skip every second line as you read the line in the while then you read it again in the while body
  2. You have a memory leak (same reason as point 1)
int main(void)                                          
{
    size_t l = 0;
    ssize_t retVal;
    char *line;

    line = NULL;
    do 
    {
        if((retVal = getline(&line,&l,stdin)) != -1)
        {
            printf("Buff_size:%zu Line_length: %zd Line:\"%s\"\n", l, retVal, line);

        }
    }while(retVal != -1);
    free(line);
    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文