所有对象都具有相同的名称

发布于 2024-11-18 19:13:09 字数 1867 浏览 6 评论 0原文

我正在用 C 语言为我的算法课程做最后一个项目。对于该项目,我们必须获取一个输入文本文件,其中包含以下行:

P|A|0

E|0|1|2

前者表示要添加到我们在程序中使用的图形中的顶点,第二个标记是顶点的名称,最后一个标记是其在图结构的 vertices[] 数组中的索引。

我有一个 while 循环逐行执行该程序,它需要第一个标记来决定是否创建顶点或边,然后相应地继续。

当我完成文件遍历时,我调用我的 show_vertices 函数,它只是一个按顺序打印每个名称 (g->vertices[i].name) 的 for 循环。

问题是,名称应该出现在输出(%s)中的位置,我不断得到我收集的最后一个“token1”。在我使用的特定输入文件的情况下,它恰好是列表中最后一条边的源节点......这很奇怪,因为之后还有两个其他值通过 strtok() 函数传递。文件中的行如下所示:

E|6|7|1

它从索引 6 到 7 创建一条权重为 1 的边。该边效果很好。但是当我用 %s 调用任何 printf 时,它会出现“6”。不管。

这就是文件的遍历。

fgets(currLn, sizeof(currLn), infile);
maxv = atoi(currLn);
if(maxv = 0)
{
    //file not formatted correctly, print error message
    return;
}

t_graph *g = new_graph(maxv, TRUE);

while((fgets(currLn, sizeof(currLn), infile)) != NULL)
{
    token1 = strtok(currLn, "|");
    key = token1[0];

    if(key == 'P' || key == 'p')
    {
        token1 = strtok(NULL, "|");
        if(!add_vertex(g, token1))
        {
            //file integration fail, throw error!
            return;
        }
        //***If I print the name here, it works fine and gives me the right name!****
        continue;
    }
    if(key == 'E' || key == 'e')
    {
        token1 = strtok(NULL, "|");
        token2 = strtok(NULL, "|");
        token3 = strtok(NULL, "|");
        src = atoi(token1);
        dst = atoi(token2);
        w = atoi(token3);

        if(!add_edge(g, src, dst, w))
        {
            //file integration fail, throw error
            return;
        }
        continue;
    }
    else
    {
        //epic error message because user doesn't know what they're doing.
        return;
    }
}

如果我在这里运行 show_vertices ,我会得到:

0. 6  
1. 6  
2. 6  
etc...

I'm doing my final project for my algorithms course in C. For the project, we have to take an input text file that contains lines like:

P|A|0

or

E|0|1|2

The former indicates a vertex to be added to the graph we're using in the program, the 2nd token being the name of the vertex, and the last token being its index in the vertices[] array of the graph struct.

I've got a while loop going through this program line by line, it takes the first token to decide whether to make a vertex or an edge, and then proceeds accordingly.

When I finish the file traversal, I call my show_vertices function, which is just a for-loop that prints each name (g->vertices[i].name) sequentially.

The problem is that where the name should go in the output (%s), I keep getting the last "token1" I collected. In the case of the particular input file I'm using it happens to be the source node of the last edge in the list...which is odd because there are two other values passed through the strtok() function afterward. The line in the file looks like:

E|6|7|1

which creates an edge from indexes 6 to 7 with a weight of 1. The edge comes up fine. But when I call any printf with a %s, it comes up "6". Regardless.

This is the file traversal.

fgets(currLn, sizeof(currLn), infile);
maxv = atoi(currLn);
if(maxv = 0)
{
    //file not formatted correctly, print error message
    return;
}

t_graph *g = new_graph(maxv, TRUE);

while((fgets(currLn, sizeof(currLn), infile)) != NULL)
{
    token1 = strtok(currLn, "|");
    key = token1[0];

    if(key == 'P' || key == 'p')
    {
        token1 = strtok(NULL, "|");
        if(!add_vertex(g, token1))
        {
            //file integration fail, throw error!
            return;
        }
        //***If I print the name here, it works fine and gives me the right name!****
        continue;
    }
    if(key == 'E' || key == 'e')
    {
        token1 = strtok(NULL, "|");
        token2 = strtok(NULL, "|");
        token3 = strtok(NULL, "|");
        src = atoi(token1);
        dst = atoi(token2);
        w = atoi(token3);

        if(!add_edge(g, src, dst, w))
        {
            //file integration fail, throw error
            return;
        }
        continue;
    }
    else
    {
        //epic error message because user doesn't know what they're doing.
        return;
    }
}

If I run show_vertices here, I get:

0. 6  
1. 6  
2. 6  
etc...

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

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

发布评论

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

评论(1

醉城メ夜风 2024-11-25 19:13:09

你没有复制这个名字。因此,您最终得到一个指向单个静态数组的指针(由 strtok 返回),您可以在其中读取每一行。由于名称始终位于偏移量 2 处,因此该指针始终为 currLn+2。当您遍历并打印时,这将是您读到的姓氏。

在将其传递给(或传入)add_vertex 之前,您需要strdup(token1)

不,没有足够的信息来确定这就是答案。但我敢打赌,就是这样。

You aren't copying the name. So you end up with a pointer (returned by strtok) to single static array in which you read each line. Since the name is always at offset 2, it that pointer will always be currLn+2. When you traverse and print, that will be the last name you read.

You need to strdup(token1) before passing it to (or in) add_vertex.

No there isn't enough information to be certain this is the answer. But I'll bet money this is it.

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