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 7768837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:01:10+00:00 2026-06-01T16:01:10+00:00

I have completed the assignment (yes it is for a programming class), but I

  • 0

I have completed the assignment (yes it is for a programming class), but I am afraid I didn’t go about it in the most efficient way possible. It is basically the uniq program, it will compare adjacent lines in a file and only print one copy of any repeated lines. A few notes: printUniq() is my own function that takes into account various flags, readline() is another function that reads a line of arbitrary length into a char * buffer using malloc and realloc. Here is the part I am worried about:

if(prevline != NULL)
{
  while(thisline != NULL)
  {
     while(thisline != NULL && strcmp(prevline, thisline) == 0)
     {
        count++;
        free(prevline);
        prevline = thisline;
        thisline = readline(stream);
     }
     printUniq(prevline, cflag, dflag, uflag, count);
     count = 1;
     free(prevline);
     if (thisline != NULL)
     {
        prevline = thisline;
        if((thisline = readline(stream)) == NULL)
        {
           printUniq(prevline, cflag, dflag, uflag, count);
        }
     }  
  }

Is there a better way to structure this program? I hate having to check thisline for NULL three times in a loop. The first NULL check in the outer while loop is necessary, and the next check in the nested while is needed in case the last lines are duplicates. The next check after the call to free basically checks if the “Duplicate loop” was exited because of thisline being null, and if not, it will allow the program to get another line. Then the next check is only there for the very last line in the file, because if it weren’t there, when readline returns a null (there were no more lines in the file), the loop exits and the prevline was never printed.

Anyways, any help is appreciated.

  • 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-06-01T16:01:11+00:00Added an answer on June 1, 2026 at 4:01 pm

    I sugest to read the file in only one place, since it will make code more manageable. Maybe something like this might work:

    prevline = NULL;
    count = 1;
    while ((thisline = readline(stream)) != NULL) // will stay in the loop for as long as it reads from file
    {
        if (prevline == NULL)
        { // this is the first read from file
             prevline = thisline;
             continue;
    
        }
    
        if (strcmp(thisline, prevline) == 0)
        {
             count++;
        } else // found a different line
             if (count > 1) // but after I already counted several identical
             {    // so I will print the line
                    printUniq(prevline, cflag, dflag, uflag, count);
                    count = 1;
             }
        free(prevline);
        prevline = thisline;
    }
    if (count > 1) and (prevline != NULL)
    {
         printUniq(prevline, cflag, dflag, uflag, count);
    }
    free(prevline);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have completed an assignment. However, I am unfamiliar with javac compiler error messages,
I'm working on an AVL tree assignment and I have a quick question about
I am writing an assignment in MASM32 Assembly and I almost completed it but
I have an assignment for a class that is to be done in C#.
for a programming assignment I have been asked to implement a solution to the
Yes, this is homework. I completed the assignment in it's entirety. Just need to
I'm a computer engineer student and i've got a basically simple assignment but i'd
I have the following assignment: Write a complete 8086 program to perform the calculator
I have completed every step in XMonad's tutorial for Mac. However, I cannot start
I have completed a simple database for a project. Only 6tables. Of the 6,

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.