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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:14:29+00:00 2026-06-01T04:14:29+00:00

I am trying to check while not end of file in c programming. Can

  • 0

I am trying to check while not end of file in c programming.

Can anyone please help, I am not able to understand what is that I am doing wrong in following code.
It prints the last set of rows twice.

/* Reads the Input file. */
fp = fopen("matrix_p2.txt","r");
if(fp == NULL)
{
    printf("*** ERROR: Could not open file matrix_p2.txt\n");
}

p = 2;
while(!feof(fp))
{
    printf("n= %d, m= %d", n,m);

    for(i = n; i < N/p; i++)
        for(j = m; j < M; j++)
            fscanf(fp, "%d",&(A[i][j]));

    print_blk(A, n, m, N/p, M);
    //n = n + N/p;

    size[0] = N;
    size[1] = M;
}

fclose(fp);

INPUT:

1 2 3 4
2 3 4 5
3 4 5 6
4 5 6 7

OUTPUT:

1 2 3 4
2 3 4 5

3 4 5 6
4 5 6 7

3 4 5 6
4 5 6 7

But I want output as followS:

OUTPUT

1 2 3 4
2 3 4 5

3 4 5 6
4 5 6 7

  • 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-01T04:14:31+00:00Added an answer on June 1, 2026 at 4:14 am

    That’s simply how feof works; it doesn’t tell you if you’re about to hit end-of-file, it tells you if you have hit end-of-file. (That is: it doesn’t tell you when you’ve read the last character; it tells you when you’ve read past the last character.) The simplest solution is to change this:

            while(!feof(fp))
    

    to this:

            while(1)
    

    and this:

                for(i = n; i < N/p; i++)
                    for(j = m; j < M; j++)
                        fscanf(fp, "%d",&(A[i][j]));
    

    to this:

                for(i = n; i < N/p; i++)
                    for(j = m; j < M; j++)
                        fscanf(fp, "%d",&(A[i][j]));
                if(feof(fp))
                    break; // hit end-of-file while getting the array
    

    so that you test for end-of-file after you’ve read in the array (or haven’t) and before you do anything with it.

    (It would be even better, though, to actually examine the return-value of fscanf and handle any error conditions such as end-of-file.)

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

Sidebar

Related Questions

I'm trying to check if a file exists or not in Tcl, but I
I have been trying for quite a while to check if submitted links are
I'm currently trying to expand my basic news feature so that comments can be
I'm trying to improve our build process, and to that end I've been looking
I'm trying to write a something that monitors a log file and adds a
I have a txt file that has a change-log.I'm trying to display the new
I am trying to check if the 'Listivew.Tag property is nothing'. I used to
I am trying to check a Max-Heap to see whether a parent has children
I'm trying to check if a gravatar exists. When I try the approach recommended
I'm trying to check, using an automated discovery tool, when JAR files in remote

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.