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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:53:18+00:00 2026-05-30T18:53:18+00:00

I am getting an infinite loop (in while loop) with this function. I am

  • 0

I am getting an infinite loop (in while loop) with this function. I am new to working with files, so I feel like I am missing something…I can’t see what is wrong.

void cipher(FILE* password_ptr,int n)
{    
        if (password_ptr == NULL)
        {
                printf("Error:password_ptr points to null");
                return;
        }
        while(!feof(password_ptr))
        {
                fseek(password_ptr, 0, SEEK_CUR); // don't move
                int en=fgetc(password_ptr)+n;

                fseek(password_ptr, -1, SEEK_CUR); // move backwards one character
                if(fputc(en,password_ptr)!=en)
                {
                printf("Error:fputc didn't work");
                }
                fseek(password_ptr, 0, SEEK_CUR);
        }
        fclose(password_ptr);
};

Thanks!

  • 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-30T18:53:19+00:00Added an answer on May 30, 2026 at 6:53 pm

    A side effect of calling fseek() is that the EOF indication on the file gets cleared:

    C99 7.19.9.2/5 The fseek function:

    After determining the new position, a successful call to the fseek function undoes any effects of the ungetc function on the stream, clears the end-of-file indicator for the stream, and then establishes the new position.

    Note that your code also uses the common anti-pattern of a loop controlled by the feof() function. That function will not return EOF until after an I/O operation gets you to that point (sets the end-of-file indicator). In other words, even when you enter the loop, the fgetc() may fail due to being at the end of the file (that’s what will set the end-of-file indicator). But then a subsequent seek will clear that indicator. In the meantime, you’ll have operated on the EOF as if it were a normal, successful read.

    See:

    • Common C Programming Errors: Using feof() incorrectly
    • “while( !feof( file ) )” is always wrong

    You might want to try the following loop instead:

    int c;
    while((c = fgetc(password_ptr)) != EOF)
    {
        int en= c+n;
    
        fseek(password_ptr, -1, SEEK_CUR); // move backwards one character
        if(fputc(en,password_ptr)!=en)
        {
            printf("Error:fputc didn't work");
            break;
        }
    }
    

    You also need to think about how you want this bit of code to handle a situation where en is out of the range of an unsigned char. Since fputc() converts the character to be written to an unsigned char before writing it to the stream, if en is outside of that range the “fputc didn’t work” error will be displayed. This might happen, for example, if adding n to the character read by fgetc() is larger than 255.

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

Sidebar

Related Questions

Is there a way to do this without getting an infinite loop? while((my $var)
No matter what I try I keep getting an infinite loop with this function:
I keep getting my code caught in an infinite while loop. It is nothing
My saga with x86 assembly continues, I'm getting into an infinite loop with this
I am getting an infinite loop in the URL redirect after a user either
For some reason I'm having trouble getting my program to run a while loop.
I''m getting an infinite loop with the below code. When a user hits the
I'm getting an infinite redirect loop after adding SSL support to my site. I'm
I am working on a site now that seems to have an infinite loop
Getting started with NHibernate How can I generate identity fields in nHibernate using Hilo

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.