I am having problem letting the user enter its filename, and if the user press e
ID: 3624940 • Letter: I
Question
I am having problem letting the user enter its filename, and if the user press enter it will open the default file. Here is a little of my code, I couldn't make it open the default file....FILE* openFile()
{
FILE* fp;
char fileName[81];
do
{
printf("Enter the file name to be open or press Enter to open the default file: ");
fgets(fileName, 80, stdin);
printf("%s***** ", fileName);
if(fileName[strlen(fileName)-1] = ' ')
fileName[strlen(fileName)-1] = '';
if(fileName == " ")
{
// printf("%s***** ", fileName);
fp = fopen("in.txt", "r");
}
else if( !(fp = fopen(fileName, "r")))
{
printf("No such file ");
}
else
fp = fopen(fileName, "r");
}while(!fp);
return fp;
}
Explanation / Answer
please rate - thanks
hope this helps
#include <stdio.h>
#include <conio.h>
FILE* openFile();
int main()
{FILE *input;
input=openFile();
getch();
}
FILE* openFile()
{
FILE* fp;
char fileName[81];
do
{
printf("Enter the file name to be open or press Enter to open the default file: ");
fgets(fileName, 80, stdin);
printf("%s***** ", fileName);
if(strlen(fileName)==1 )
{fp = fopen("in.txt", "r");
printf("here ");
}
else
if( !(fp = fopen(fileName, "r")))
{
printf("No such file ");
}
else
fp = fopen(fileName, "r");
}while(!fp);
return fp;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.