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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:34:34+00:00 2026-05-27T09:34:34+00:00

Main description of the problem below, where it happens. But simply, I cannot figure

  • 0

Main description of the problem below, where it happens. But simply, I cannot figure out why I get error messages after asking

if (outf!=NULL){
    printf("Output file already exists, overwrite (y/n):");
    scanf("%c",yn);
}

Where outf is a file pointer to an existing file. Please read description halfway through code.

#include <stdio.h>
#include <stdlib.h>
#include <float.h>
#include <string.h>

int main() {

/* Declare file pointer */
    FILE *inf;
    FILE *outf;

    int linenumber,linecounter=0,linepresent;
    char filename[21];
    char detail[21];
    char linedetail[21];
    char outfilename[21];
    char letter,yn='y';
    int position;

/*INPUT DETAILS Ask user for file name and line number*/

    printf("Please enter an input filename and a linenumber: ");
//scan filename to char string and line number to int variable
    scanf("%s %i",&filename,&linenumber);

/*OUTPUT DETAILS Ask user for file name, letter & position, etc*/
    printf("Please enter an output filename, a letter and a position:");    
    scanf("%s %c %i",&outfilename,&letter,&position);

/* Open file for reading */
    inf=fopen (filename,"r");
    outf=fopen(outfilename,"r");
/*check that file exists*/
    if (inf!=NULL) {

Up until here everything works fine!
Then I try to find out if the outf file already exists. If outf points to an existing file, it DOES print “Output file already exists, overwrite (y/n):”

HOWEVER, as soon as it prints this I get error windows opening! This is probably an extremely rookie mistake – I’m still learning C. If there is no such file the program completes normally and bypasses the if statement okay.

        if (outf!=NULL){
            printf("Output file already exists, overwrite (y/n):");
            scanf("%c",yn);
        }
        if (yn=='y'){
    /*keep reading to end of file*/
            while (feof(inf)==0) {
                linecounter++;
    /*read each line and store the line number in detail[WORDS GO HERE]*/
                fscanf (inf,"%s", &detail);
    /*If we reach the line selected by the user*/
                if (linecounter==linenumber){
                    strcpy(linedetail,detail);
                    linepresent=1;
                }
            }
            if (linepresent==0) {
                printf("File only contains %i lines",linecounter);
            }
        } else {
            exit(1);
        }
    } else {
        printf("Input file not found");
    }

printf("%s",linedetail);

/* close the file */

    fclose(inf);
    fclose(outf);

    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-05-27T09:34:35+00:00Added an answer on May 27, 2026 at 9:34 am

    First, already mentioned problems: You’re opening the output file in reading mode. To open it for writing:

    outf=fopen(outfilename,"w");  /* Note the "w". */
    

    Also, scanf() accepts pointers to variables, not their values, so if you write scanf("%c", yn);, you will give scanf the character y as a pointer, which is nonsense. You need to do it like this: scanf("%c", &yn);.

    Even if you fix these, however, your program won’t do what you expect. If the file you’re trying to open for writing doesn’t exist, fopen() won’t return NULL, it will create a new file. Your code will always overwrite the output file if it exists. NULL is returned only if fopen couldn’t open/create the file (e.g. you didn’t have the permissions to do it), and you should handle it like this:

    outf=fopen(outfilename, "w");
    if(outf == NULL) {
        perror("Failed to open output file: ");
        fclose(inf);  /* Don't leave files opened. It's bad form. */
        exit(1);
    }
    /* Open succeeded, do your stuff here. */
    

    Note that no else block is needed after the if, because exit() ends the program immediately.

    Also, there is no such thing as a “pointer to a file”. FILE is just a structure that represents an open file.

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

Sidebar

Related Questions

Problem Description: Occassionally when debugging, I get the following error. I'm using visual studio
Problem description : - Step 1: Take input FILE_NAME from user at main thread.
Problem Description: We have a service which has applications for main mobile OS’s. We
Problem Description: my app has a main window. when you click a button it
Edit 4: The main issue has been resolved - turned out the problem was
Problem Description I receive an non-descriptive "error" message whenever attempting to call an HTML
I am implementing a protocol library. Here a simplified description. The main thread within
Main problem: how do i group elements by their Date, only if continuous and
Problem description If I make a non-modal window as a child window through setting
When I try to compile to following code, I get two errors: Description Resource

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.