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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T11:28:43+00:00 2026-05-19T11:28:43+00:00

I need to read the last added line to a log file , in

  • 0

I need to read the last added line to a log file, in realtime, and capture that line being added.

Something similar to Tail -f.

So my first attempt was to use Tail -f using NSTask.

I can’t see any output using the code below:

    NSTask *server = [[NSTask alloc] init];
    [server setLaunchPath:@"/usr/bin/tail"];
    [server setArguments:[NSArray arrayWithObjects:@"-f", @"/path/to/my/LogFile.txt",nil]];

    NSPipe *outputPipe = [NSPipe pipe];
    [server setStandardInput:[NSPipe pipe]];
    [server setStandardOutput:outputPipe];

    [server launch];
    [server waitUntilExit];
    [server release];

    NSData *outputData = [[outputPipe fileHandleForReading] readDataToEndOfFile];
    NSString *outputString = [[[NSString alloc] initWithData:outputData encoding:NSUTF8StringEncoding] autorelease];
    NSLog (@"Output \n%@", outputString);

I can see the output as expected when using:

[server setLaunchPath:@"/bin/ls"];
  1. How can i capture the output of that tail NSTask?

  2. Is there any alternative to this method, where I can open a stream to file and each time a line is added, output it on screen? (basic logging functionality)

  • 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-19T11:28:44+00:00Added an answer on May 19, 2026 at 11:28 am

    This is a little tricky to do your way, as readDataToEndOfFile will wait until tail closes the output stream before returning, but tail -f never closes the output stream (stdout). However, this is actually pretty simple to do with basic C I/O code, so I whipped up a simple FileTailer class that you can check out. It’s not anything fancy, but it should show you how it’s done. Here’re the sources for FileTailer.h, FileTailer.m, and a test driver.

    The meat of the class is pretty simple. You pass it a block, and it reads a character from the stream (if possible) and passes it to the block; if EOF has been reached, it waits a number of seconds (determined by refresh) and then tries to read the stream again.

    - (void)readIndefinitely:(void (^)(int ch))action
    {
        long pos = 0L;
        int ch = 0;
    
        while (1) {
            fseek(in, pos, SEEK_SET);
            int ch = fgetc(in);
            pos = ftell(in);
            if (ch != EOF) {
                action(ch);
            } else {
                [NSThread sleepForTimeInterval:refresh];
            }
        }
    }
    

    You can call it pretty simply, like this:

    FileTailer *tail = [[[FileTailer alloc] initWithStream:stdin refreshPeriod:3.0] autorelease];
    [tail readIndefinitely:^ void (int ch) { printf("%c", ch); }];
    

    (Caveat: I wrote the FileTailer class pretty fast, so it’s kind of ugly right now and should be cleaned up a bit, but it should serve as a decent example on how to read a file indefinitely, à la tail -f.)

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

Sidebar

Related Questions

I need to read the data from a file that can be either comma
I need to read in an Intel Hex file which looks something like this:
In php, I need to read a file that has no read access (the
I have a plain text file that I need to read in using C#,
I need to read in all but the last x lines from a file
I have a log file that continually logs short lines. I need to develop
I read text data from big file line by line. But I need to
I need to read in a rolling log file in .NET 3.5sp1. I'm wondering
I need to read small sequences of data from a 3.7 GB file. The
I need to read a file in MB chunks, is there a cleaner way

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.