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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:09:58+00:00 2026-06-03T23:09:58+00:00

I am trying to implement some code to pick out the last line of

  • 0

I am trying to implement some code to pick out the last line of a text file, and am currently using this example, but I cannot figure how to prevent the code from printing out “(null)” when it cannot find the file, at the moment when it cannot find the file, it will print out “file cannot open at last line: no such file or directory” then it will print the null thing below, and for this particular application i would rather that it just print out the “file cannot open” part, but not print out the null part, if anyone could point me in the right direction for this I would really appreciate it, thanks!

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

#ifndef max
#define max(a, b) ((a)>(b))? (a) : (b)
#endif

long GetFileSize(FILE *fp){
    long fsize = 0;

    fseek(fp,0,SEEK_END);
    fsize = ftell(fp);
    fseek(fp,0,SEEK_SET);//reset stream position!!

    return fsize;
}
char *lastline(char *filepath){
    FILE *fp;
    char buff[4096+1];
    int size,i;
    long fsize;
    if(NULL==(fp=fopen(filepath, "r"))){
        perror("file cannot open at lastline");
        return NULL;
    }
    fsize= -1L*GetFileSize(fp);
    if(size=fseek(fp, max(fsize, -4096L), SEEK_END)){
        perror("cannot seek");
        exit(0);
    }
    size=fread(buff, sizeof(char), 4096, fp);
    fclose(fp);
    buff[size] = '\0';
    i=size-1;
    if(buff[i]=='\n'){
        buff[i] = '\0';
    }
    while(i >=0 && buff[i] != '\n')
        --i;
    ++i;
    return strdup(&buff[i]);
}

int main(void){
    char *last;

    last = lastline("data.txt");
    printf("\"%s\"\n", last);
    free(last);
    return 0;
}
  • 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-03T23:09:59+00:00Added an answer on June 3, 2026 at 11:09 pm

    You could add these two lines after the line last = lastline("data.txt");. The reason NULL is printed, is because you are printing the result of the function lastline(). This function will return NULL on failure.

    if( last != NULL )
        printf( "\"%s\"\n", last );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently trying to implement some code using Android to detect when a
I'm trying to implement some basic entities using Hibernate/JPA. Initially the code was deployed
I'm currently trying to implement some kind of file browser / explorer into a
I am trying to implement share this method. I am using the code as
I've been trying to implement unit testing and currently have some code that does
I am trying to implement some C code in Java by using SWIG 1.3.
I am trying to implement some code on my web page to auto-scroll after
I was just trying out some code, not a experienced coder. I implemented(at least
Trying to implement some nested loops that are spitting out good old nested html
This might be a shot in the dark, but I am trying to implement

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.