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

  • Home
  • SEARCH
  • 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 6895475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:54:47+00:00 2026-05-27T06:54:47+00:00

I am writing a program that reads the first 20 lines of a text

  • 0

I am writing a program that reads the first 20 lines of a text file. When the first 20 lines are read, the user is given the option to continue reading the next 20 lines or to quit the program. What’s happening me however is it prints the 20 lines, the user prompt comes up, and then it automatically prints the next 20 lines, without waiting for the user’s input. After that, it will print the user prompt and then wait for the input. I know it’s a simple issue but I’m not seeing it! I’ve amended the code a little bit as per replies to my question so far:

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

int main(void)
{
FILE *fp;
char fname[20];
char c, input;
int line;
line = 0;

    printf("Please enter the name of the file you wish to view\n");
    scanf("%s", fname);

    fp = fopen(fname, "r");

    if (fp == NULL)
    {
    printf("The file did not open correctly");
    exit(0);
    }

    else
    {
    printf("The file opened correctly\n");
    }

        while(c != EOF && input != 'q')
        {   
            c = getc(fp);
            printf("%c", c);

                if  (c == '\n')
                {
                line++;

                    while (line==20)
                    {
                    line = 0;
                    printf("Press return to view the next 20 lines or press q to quit:");
                    scanf("%c", &input);

                        if (input == 'q')
                        {               
                            return 0;
                        }

                        else if (input == '\n')
                        {
                            line++;
                        }
                    }
                }
        }
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-05-27T06:54:48+00:00Added an answer on May 27, 2026 at 6:54 am
    line = line++;
    

    Postincrement, your line is always 0. Use just:

    line++;
    

    Edit: Ok, so this is not the problem. I’ve run your program and the problem is that when you enter the file name to the first scanf, you press enter so the program reads it. For some reason this enter (or newline) is read inside the second scanf as the input. Just add a dummy scanf after the first one to absorb this newline:

    printf("Please enter the name of the file you wish to view\n");
    scanf("%s", fname);
    scanf("%c", &input); // dummy scanf
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a program that reads a file, whose first two lines are:
I am writing a small program that will read a plain ASCII text file
I am writing a program that reads the input from a file and then
I'm writing an application that manipulates a text file. The first half of my
I am writing a program that will read lines from an infile using getline
I am writing a program that takes in an input file from the user.
I am writing my first clojure program, and want to read lines from stdin.
Im writing a program that should read input via stdin, so I have the
In a program that I'm writing, I wanted to make a ConfigParser that's read
I'm writing a program which is supposed to read two strings that can contain

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.