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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:52:07+00:00 2026-06-18T03:52:07+00:00

I am trying to write a program which read the file and print lines

  • 0

I am trying to write a program which read the file and print lines from line#30 to line#50 but not able to achieve it.

main() {

FILE *fp;
char ch;

int nol = 0;
fp = fopen ("test.txt","r");

while (1){
    ch = fgetc(fp);

    if (ch == '\n')
        nol++;


    if (nol > 30 || nol < 50){
        printf ("value of NOL is %d\n", nol);
    }


    if (ch == EOF)
        break;


}
fclose (fp);
printf ("\nNumber of line in file: %d\n", nol);

I tried with if nol >=30 and nol <=50 condition and tried to print but that is not working. Any input will be of great help.

  • 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-18T03:52:09+00:00Added an answer on June 18, 2026 at 3:52 am

    First, the logic part.

    Generally we think of line numbers as beginning at 1 instead of 0. So initialise nol to 1.

    If you want to display lines 30 to 50, then you need to include them in the test. Use >= instead of > (same for <).

    And of course you need to use && instead of ||, otherwise your condition is always true.

    If you want to actually display the lines, you need something like this:

    if( nol >= 30 && nol <= 50 ) putc(ch);
    if (ch == '\n') nol++;
    

    Note the order of the statements.

    But really, you should look at using fgets if you want to display the lines, instead of reading one character at a time.

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

Sidebar

Related Questions

I'm currently trying to write a program which will read words from a text
I am trying to write a small program, which would read the context of
I am trying to write a c++ program that would read key frames from
I'm learning Java, and I'm trying to write a program that can read from
im trying to write a program that reads from a text file into a
I am trying to write a program in java which is able to download
I am trying to write a program which gets input files and prints included
I am trying to write a program in PHP which I had already written
I'm trying to write a program in C++ which runs Conway's Game of Life.
I am trying to write a simple java program which returns me all the

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.