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

  • SEARCH
  • Home
  • 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 7637495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:57:16+00:00 2026-05-31T07:57:16+00:00

I have a program that simulates a text editor. It lets users add lines

  • 0

I have a program that simulates a text editor. It lets users add lines of text to a list in whatever particular manner they choose depending on the command they send.

One of the functions lets users move backwards through the list to view their lines (there’s another one that lets them move forward, but that one’s not being problematic).

There’s also functions to let users insert or append text. Insert has the line put in before the current line while append has it set after. One problem I’m having is the way insert puts in text.

User hits i for insert, puts text in via the standard input (stdin), and then hits CTRL + D (in a Linux environment) to simulate NULL and to return back to command mode. After that, if you go to navigate through the list, it seems to enter the last line at the top of the list and everything follows suit backwards. At one point, I had inserted 4 lines of text and it did an infinite loop of the last 2 lines and ruined the text file.

I believe it has to do with my logic in linking the lists, but I’m having a hard time visualizing them. Here are the problematic functions:

void insert_line(char *t)
{
    /* Allocate and clear (i.e. set all to 0) */
    struct line *new_line = calloc(1, sizeof(struct line));

    new_line->text = t;

    if(current_line == NULL)
        head = current_line = new_line;
    else
    {
        new_line->prev = current_line->prev;
        new_line->next = current_line;
        current_line->next = new_line;
        current_line = new_line;

        if(current_line->prev == NULL)
            head = current_line;
    }
}

This must be terribly mucked up – the way it infinite loops the text sometimes and always puts the text in backwards. This is how I utilize the insert function:

else if(command[0] == 'i')
    {
        char * line;
        while((line = get_line(stdin)) != NULL)
            insert_line(line);
     }

get_line reads the text one line at a time and returns it until EOF is reached. I know the get_line function is working because my instructor wrote it for us to use.

//
// Function: previous_line
// Moves the current_line pointer to the previous node, if any, in the linked-list.
//
void previous_line(void)
{
    if(current_line == NULL)
        printf("Error: No Lines Exist.\n");
    else if(current_line->prev != NULL) {
        current_line = current_line->prev;
        printf("%s\n", current_line->text);
    }
    else
        printf("Error: Already beginning-of-line.\n");
}

This one is weird, when I append text in the middle of text, the next_line function works fine, but then when I run this to go back through the list, it shows nothing of what I’ve added.

  • 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-31T07:57:18+00:00Added an answer on May 31, 2026 at 7:57 am

    Draw it on paper (a box for each line and some arrows for next and prev)

    This bit has problems – should be fairly clear when you draw it.

    new_line->prev = current_line->prev;
    new_line->next = current_line;
    current_line->next = new_line;
    current_line = new_line;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a program that simulates mouse click. Code is something like this: [DllImport(user32.dll,
I have a program which simulates a physical system that changes over time. I
I have a program that simulates dice rolls and compares them to values in
I have a program that simulates best-fit memory management. Basically, while there are available
Okay, I have a struct, TextBlock , that simulates moving blocks of text around
I have a program on my computer that simulates a server on the internet
I have an assignment that simulates a dice game. As part of the program,
I have a question considering a program that stimulates a stack (not using any
I have program that has a variable that should never change. However, somehow, it
I have program that runs fast enough. I want to see the number of

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.