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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:40:32+00:00 2026-05-26T03:40:32+00:00

I am having a program where fscanf(fp,%[^\n]s,line); is used for reading a line. If

  • 0

I am having a program where

fscanf(fp,"%[^\n]s",line);

is used for reading a line.

If I put in a while loop,

while(!feof(fp))
    fscanf(fp,"%[^\n]s",line);

the above code works for first line and for the rest, I am getting

line as NULL. ( line = “” )

My file contains many lines even many blank lines. How can I make the above code work?

  • 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-26T03:40:33+00:00Added an answer on May 26, 2026 at 3:40 am

    First, the conversion specifier would be %[^\n] (no s on the end).

    Secondly, you don’t want to use the %[ conversion specifier without an explicit size; otherwise you run the risk of a buffer overflow:

    char line[132];
    ...
    fscanf(fp, "%131[^\n]", line);
    

    Third, this will leave the newline in the input stream, potentially fouling up the next read.

    Finally, you don’t want to use feof as your loop condition, since it won’t return true until after you try to read past EOF, causing your loop to execute one too many times.

    Frankly, I think the better option is to use fgets(); it will read everything up to and including the next newline or one less than the specified size. IOW, if line is sized to hold 20 characters and the input line has 80 characters (including the newline), fgets will read 19 characters and append the 0 terminator into line. If the input line is 10 characters, it will read the whole input line into line (including the newline).

    fgets will return NULL on EOF or error, so you should structure your loop as

    while (fgets(line, sizeof line, fp))
    {
      // do something with line
    }
    if (feof(fp))
      // hit end of file
    else
      // error on read.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having problems redirecting stdio of another program using subprocess module. Just reading from
what is the complexity of a program having only one loop, is it log
All, What is a really simple way of having a program do more than
I am working in Linux, Eclipse CDT, g++, with Boost library. Having existing program
Having successfully gotten a sample program working, I'm now starting to do Real Work
I'm having a problem converting my program from VS2005 to VS2008. When I run
I'm having problems compiling the following program. I'm using gcc -framework Foundation inherit8.1m and
I'm having problems with a third-party program which is using FindWindow() to find if
I am having trouble running a Java program with Windows Powershell 2.0. Any help
I'm having some problems with my program in VS .NET 2003. I initially wrote

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.