else statement to correct filename

#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <string>
#include <fstream>

/*
Mppk
Date 10/12/2010
a program to load user specified file into an




*/

int main(void) /*begins the function with no inputs*/
{
char filename[999]; /*this line creates an array named filename with a maximum of 999 different characters*/
int fpointer; /*intializes the pointer*/

printf ("enter the filename you wish to open... "); /*prints a line asking for user specified filename*/
scanf( "%s", filename ); /*scans the users input into the function*/

FILE *file_in; /*this parts makes the file load*/


if (file_in = fopen(filename, "r")) /*if funcion, when the file opens getc reads the characters incluing the spaces into a string*/
{
fpointer = getc(file_in);
}
else
{
printf ("incorect filename please enter again");
scanf( "%s", filename );
}

while (fpointer != EOF) /*while function, when not equal to end of file print the fpointer which is equal to the string of characters loaded*/
{
printf("%c", fpointer);
fpointer = getc(file_in);
}

fclose(file_in); /*closes the stream when end of file is reached*/
}



it loads the file as i wanted but when you put in an incorrect file name i cant seem to get it output the error message and load a new filename
Last edited on
You have a common mistake in your if statement. One way to code it is:
1
2
      if( ( file_in = fopen( filename, "r' ) != NULL )
 


This should help.
Topic archived. No new replies allowed.