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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:34:13+00:00 2026-06-14T10:34:13+00:00

So im working on learning how to do file I/O, but the book I’m

  • 0

So im working on learning how to do file I/O, but the book I’m using is terrible at teaching how to receive input from a file. Below is is their example of how to receive input from a file, but it doesn’t work. I have copied it word for word, and it should loop through a list of names until it reaches the end of the file( or so they say in the book), but it doesn’t. In fact if I leave the while loop in there, it doesn’t print anything.

#include <stdio.h>
#include <conio.h>
#define MAX 250
int main()
{

  char name[MAX]; 
  FILE*pRead;

  pRead=fopen("test.txt", "r");
  if (pRead==NULL)
    {
     printf("file cannot be opened");
    }else      


     printf("contents of test.txt");
     while(fgets(name,sizeof(name),pRead)!=NULL){
           {
            printf("%s\n",name);
            fscanf(pRead, "%s", name);
           }


             getch();
   }

Even online, every beginners tutorial I see does some variation of this, but I can’t seem to get it to work even a little bit.

  • 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-14T10:34:15+00:00Added an answer on June 14, 2026 at 10:34 am

    I believe your array is too small and therefore when you are reading fscanf overwrites memory causing bizarre behavior

    If you just want to read the file – presuming now there is one name per line followed by newline in the input file – just read the file using fgets() instead.

    #define MAXLINE 256
    
    char name[MAXLINE]; 
    
    while (fgets(name,sizeof(name),pRead)!=NULL)
    {
      // do whatever
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working with a wsdl file, and learning quite a lot from the whole
I'm working through a small file upload script (learning experience) and I noticed that
I am working on learning in-app billing but I am having a problem with
So I'm working on learning PDO, and making the transfer from the standard PHP
I have been working on learning the Android NDK the past few days, but
I'm working on learning Objective-C/Coaoa, but I've seem to have gotten a bit stuck
I am trying my hand at learning node.js using coffeescript but am having trouble
I am working through the book Learning WCF by Michele Bustamante, and trying to
I have been working on a project that dynamically creates a javascript file using
I am just learning MVC and im using Tkinter to do so, but while

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.