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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T06:51:20+00:00 2026-05-21T06:51:20+00:00

I’m trying to read a specific line from a file and I can get

  • 0

I’m trying to read a specific line from a file and I can get the line number but I’m not sure how to go about doing it, this is what I have so far:

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

int main(int argc, char **argv)
{
  FILE *file;
  file = fopen("temp.txt","r");
  char tmp[256]={0x0};
  char *tmpline;
  int numline = 1;
  while(file != NULL && fgets(tmp, sizeof(tmp),file) !=NULL)
  {
    tmpline = strstr(tmp,"status:green");

    if(tmpline) {
      printf("%d - %s", numline, tmpline);
    }

    numline++;
  }
  if (file != NULL) fclose(file);

  return 0;
}

The test file looks like:

s1.server.com
127.0.0.1 
status:green

s2.server.com
127.0.0.1 
status:red

s3.server.com
127.0.0.1 
status:red

s4.server.com
127.0.0.1 
status:green

The output that I have is:

3 - status:green 
15 - status:green

But what I really want it to show is:

s1.server.com
s4.server.com

I want it to search for “status:green” then go back a few lines to show which server it belongs to

  • 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-21T06:51:20+00:00Added an answer on May 21, 2026 at 6:51 am

    It sounds as if you need to do one of two things.

    Simpler option: keep a little circular buffer of lines. Read into line 0, line 1, line 2, …, line n-1, line 0, line 1, etc. Then, when you see the text you want, look in entry (current_index – 2) mod buffer_size. (Here it sounds as if a buffer size of 3 will suffice.)

    More sophisticated option: actually parse the input so that for each block you work out the server name, its IP address and its status, and then display the information you need using that.

    The “more sophisticated option” would be substantially more work, but more robust if the syntax of your input ever changes (e.g., with optional extra lines with more information about the server — multiple IP addresses or multiple names, perhaps).

    There are some other things you could do that I think are worse. (1) Call ftell on each line and put the results of that in a circular buffer, and then use fseek when you see “status:green”. (2) Read the whole file using code like you currently have, building up a list of “good” servers’ line numbers. Then go through the file again and report the good ones. I think these are both uglier and less efficient than the approaches I listed above. There’s one possible advantage: you can adapt them to count in “stanzas” separated by blank lines, without needing to parse things properly. That would get you part of the flexibility of the “more sophisticated” approach I mentioned, without needing a proper parser.

    And here’s a hybrid possibility: don’t use a circular buffer, but one whose size can increase if need be. Start at the first entry in the buffer each time you see a blank line. Let the buffer grow if there are “long” stanzas. Then when you see “status:green”, do whatever processing you need to on the (presumably complete) stanza now held in your buffer.

    None of the above is necessary, of course, if you’re sure that the file format will never change.

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

Sidebar

Related Questions

I need to clean up various Word 'smart' characters in user input, including but
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
i want to parse a xhtml file and display in UITableView. what is the
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim

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.