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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:24:54+00:00 2026-05-26T21:24:54+00:00

I have a file with multiple strings, each string on a separate line. All

  • 0

I have a file with multiple strings, each string on a separate line. All strings are 32 character long (so 33 with the ‘\n’ at the end).

I am trying to read all the strings. For now, I just want to read them and not store them as follows:

char line[32];
while (!feof(fp)) {
    fgets(line, 32, fp);
}
printf("%s", line);

This prints out zero. Why isn’t it working?

Furthermore, I am trying to store a null terminator at the end of each string read. I changed the line array to length 33 but how would I make it that if '\n' is found, replace it with \0 and store that?

  • 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-26T21:24:55+00:00Added an answer on May 26, 2026 at 9:24 pm

    You code isn’t working because you are only allocating space for lines of 30 characters plus a newline and a null terminator, and because you are only printing out one line after feof() returns true.

    Additionally, feof() returns true only after you have tried and failed to read past the end of file. This means that while (!feof(fp)) is generally incorrect – you should simply read until the reading function fails – at that point you can use feof() / ferror() to distinguish between end-of-file and other types of failures (if you need to). So, you code could look like:

    char line[34];
    
    while (fgets(line, 34, fp) != NULL) {
        printf("%s", line);
    }
    

    If you wish to find the first '\n' character in line, and replace it with '\0', you can use strchr() from <string.h>:

    char *p;
    
    p = strchr(line, '\n');
    if (p != NULL)
        *p = '\0';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a file have multiple lines. For each line the format is String
I have a 200kb file, what I use in multiple pages, but on each
I have set up multiple targets in a single xml file. I expect all
I have multiple PHP strings (10-100k characters each) and want to get exact elements
I have a CSV file. It has many lines. In each line there are
I have a file which has multiple columns, whitespace separated. e.g: data1 data2 data3
We have an idl file with multiple interfaces defined, two of which have someting
Is it possible to have a file belong to multiple subpackages? For example: /**
I have a maven pom.xml file with multiple instances of a same goal defined
I have an ASP.NET MVC page with multiple file uploads. After a failed post,

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.