Programming in C - Determining if a file exists...

Associate
Joined
27 Dec 2004
Posts
612
Location
Andover
Just wondering if anyone has an idea how to do this, i've created a simple program to read line and word counts from a text file in C. The program asks what file you would like to read from and continues from there, my problem is if i enter a file that doesn't exist it seg faults.

Does anyone know a way to test if a file name a user inputs exists using only stdio.h before it tries to open it?
 
while ((in = fopen(fileS[1],"rt")) == NULL)
{
printf("File does not exist on specified directory.\n");
printf("Enter the name of the text file: ");
scanf("%s", fileS);

I've tried to modify the code to allow me to input another file, will this not work? Everytime i try to ask for a new file it just says file does not exist on a specified directory.

Any ideas?

Edit: also just checked the original coding and it seems that it doesn't work in my program at all :(
 
Last edited:
Back
Top Bottom