Question

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);

}
Was it helpful?

Solution

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.

OTHER TIPS

http://www.cplusplus.com/reference/clibrary/cstdio/fopen/

You are open the output file with the read flag. Try changing it to "w".

outf=fopen(outfilename,"w");

Although it is worth noting writing to a file opened with "w" will whack the old file. use "a" to append to the file.

You should pass the address of yn to scanf function.

scanf("%c", &yn);

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top