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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:56:14+00:00 2026-06-16T03:56:14+00:00

the printf statement here only prints out the last word inside of the file.

  • 0

the printf statement here only prints out the last word inside of the file. Why is that?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
    FILE *fp;
    int c;
    char string[100];
    fp = fopen("exam.txt", "r");
    c = getc(fp);
    while(c!=EOF)
    { 
        fscanf(fp, "%s", string);
        c = getc(fp);
    }
    fclose(fp);
    printf("%s", string);
    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-06-16T03:56:15+00:00Added an answer on June 16, 2026 at 3:56 am

    Because you only print once at the end…

    printf("%s", string); 
    

    You need to print inside this loop:

    while(c!=EOF)
    { 
        fscanf(fp, "%99s", string);
        printf("%s\n", string);  // now you can see the files as you read it.
         c = getc(fp);
    } 
    

    if you want to see each line. You’re just overwriting string each time.

    Also, you don’t initialize int c before using it.

    Break down of fscanf():

     fscanf(fp,       // read from here   (file pointer to your file)
            "%99s",   // read this format (string up to 99 characters)
             string); // store the data here (your char array)
    

    Your loop condition is while the next character isn’t EOF meaning End Of File (a condition that happens after all the data has been read out of the file)

    So:

    while (we're not at the end of the file)
         read up a line and store it in string
         get the next character
    

    You’ll note your algorithm doesn’t check for anything in that string, it just writes to it. This will overwrite the data in there each time. That’s why you only see the last line from your file, because you keep over writing string and the last thing in there happens to be the last line you read before seeing the EOF character and breaking out of the while loop.

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

Sidebar

Related Questions

#include<stdio.h> #include<signal.h> #include<stdlib.h> void handler(int signo) { printf(First statement); system(date); exit(EXIT_SUCCESS); } int main()
The following code works partially. Kindly help to figure out the mistake #include<stdio.h> #include<stdlib.h>
in C if I have a printf statement containing say %.2f, it says that
How to fix this statement? System.out.printf( 1 | %d | %d | %d\n ,
The "goto" statement comes straight out of ASM or any other assembler language. Here's
I wanted to ask whether the following statement where printf() has been provided with
I'm a beginner in C and I have a statement like: printf(%d,(1^6)); which produces
I'm encountering an error sprintf statement. I added a printf command to help investigate,
I'm trying to create a print statement that can print any type. I want
Here's a piece of code that I'm using to populate view-based NSTableView with data:

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.