check file existence using while loop

September 11, 2011 at 11:03:25
Specs: Windows 7
Hi I do not usually ask others to help me in my homework (I am not a lazy person, I really like to learn), but C programming is so new to me, and I have a difficult professor who is giving us difficult assignments :( I really need all the help i could get.

I am trying to check if the user has entered the correct input of the file's name, and program will check if the file exists. if file cannot be found, it will return an error message, and prompt user to retype the file name again.

So in these case, I decided to use while loop because it seems to be the best option:

FILE * input_file; //user-input file

printf("Input your file name:");
scanf("%s",filename);

input_file = fopen(filename,"r");

//check if the file exist
while(input_file != NULL)
{
printf("File do not exist. Try again\n");
fclose(input_file);
printf("Input your file name:");
scanf("%s",filename);
input_file = fopen(filename,"r");
}


See More: check file existence using while loop

Report •

#1
September 11, 2011 at 11:53:43
while(input_file != NULL)
Loop while the file is valid? Also, I'm not seeing the filename declaration, but I'm assuming it's valid and it's large enough to hold the data.

How To Ask Questions The Smart Way


Report •

#2
September 11, 2011 at 12:02:55
I tried both != and == conditions, but it still dump me a segmentation fault.

Here is the whole code thus far:

#include <stdio.h>

int main(void)
{
char buffer[256];
char *filename;

FILE * input_file; //user-input file

printf("Input your file name:");
scanf("%s",filename);

input_file = fopen(filename,"r");

//check if the file exist
while(input_file == NULL)
{
printf("File do not exist. Try again\n");
fclose(input_file);
printf("Input your file name:");
scanf("%s",filename);
input_file = fopen(filename,"r");
}

//got the file!
printf("file is right!\n");

while (!feof(input_file))
{
fgets(buffer,256,input_file);
printf("%s",buffer);
}

fclose(input_file);

return 0;
}


Report •

#3
September 11, 2011 at 12:43:41
char *filename;
And there's your problem. filename is not valid. You're saying filename will point to a valid char (or really, an array of chars), but you never get around to it. filename doesn't point to a valid address (presumably your complier is set up to do debug builds, so the pointer points to 0), and when scanf() tries to write to it, the write fails with a segfault.

Replace:
char *filename;
With:

char filename[256];

Also, you should get in the habit of using fgets() instead of scanf().

How To Ask Questions The Smart Way


Report •

Related Solutions

#4
September 11, 2011 at 19:47:15
I do not know that a * pointer is pointing to any addresses at all, now that I learned of it, I will be more careful and I have a better understand of how arrays work in C.

However, what do you mean on the last line? Does fgets() works similarly with scanf()? Could you please shed some light in this matter?


Report •

#5
September 13, 2011 at 05:59:11
in the case of keyboard input.

char buffer[200] = { 0 };
fgets(buffer, sizeof buffer, stdin);

side effect: if it will fit, the function will leave the new line char (enter pressed), so you need to remove it.

char *p;
if((p = strchr(buffer, '\n')) != NULL) { *p = '\0'; }

HTH


Report •

#6
September 13, 2011 at 07:42:13
Does fgets() works similarly with scanf()?
fgets() forces you to specify the size of your buffer, so you won't overflow. It's also designed to read a string without parsing, which is exactly what you're doing here. Also, since it doesn't perform parsing, you can combine fgets() with sscanf() and do your own, more forgiving parsing.

How To Ask Questions The Smart Way


Report •

Ask Question