Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6110471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:31:10+00:00 2026-05-23T14:31:10+00:00

I’m doing my final project for my algorithms course in C. For the project,

  • 0

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...
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T14:31:10+00:00Added an answer on May 23, 2026 at 2:31 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have some data like this: 1 2 3 4 5 9 2 6
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to loop through a bunch of documents I have to put
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.