Don't have a solution (yet), but I can point you to the actual error:
The file cannot be found (even though it is there).
I change the code a bit to get that answer:
Code:
#include <stdio.h>
int main(void)
{
FILE *fp;
int c;
int b=0;
char s[25];
printf("Enter local filename: ");
fgets(s,20,stdin);
fp = fopen(s,"r");
if(fp==NULL) {
printf("Error: can't open file.\n");
return 1;
}
else {
while((c = fgetc(fp)) != EOF)
{
b++;
}
fclose(fp);
printf("The number of characters in %s is:%d " ,s,b);
return 0;
}
}
Output of a test run:
Quote:
$./blaat
Enter local filename: one.txt
Error: can't open file.
$ ls one.txt
one.txt
|
A clean, although unexpected, run. How is the file closed when an error occurs? (don't have the c knowledge to answer that myself)
An strace shows the following (relevant part, without the above addition):
Quote:
write(1, "Enter local filename: ", 22Enter local filename: ) = 22
read(0, one.txt
"one.txt\n", 1024) = 8
brk(0) = 0x804a000
brk(0x806b000) = 0x806b000
open("one.txt
", O_RDONLY) = -1 ENOENT (No such file or directory)
--- SIGSEGV (Segmentation fault) @ 0 (0) ---
+++ killed by SIGSEGV +++
Process 24775 detached
|