P
phaedrus
Hi all,
I can't figure out what's wrong with this piece of code. It generates
a segmentation fault. The only thing I can think of is that fopen()
doesn't like a variable being used in place of a file name. Here's the
code which I've heavily commented so you can see what it's SUPPOSED to
do:
#include <stdio.h>
int main(void)
{
FILE *fp;
int c;
int b=0;
char s[25]; //declare array 's' to hold the filename
printf("Enter local filename: ");
fgets(s,24,stdin); //read string from keyboard into
array 's'
fp = fopen(s , "r"); //open the file referenced by
's' in read-mode.
while((c = fgetc(fp)) != EOF)
{
b++; //count number of characters in the file
}
fclose(fp);
printf("The number of characters in %s is:%d " ,s,b); //print out
total number of chars.
return 0;
}
Any suggestions would be welcome!
I can't figure out what's wrong with this piece of code. It generates
a segmentation fault. The only thing I can think of is that fopen()
doesn't like a variable being used in place of a file name. Here's the
code which I've heavily commented so you can see what it's SUPPOSED to
do:
#include <stdio.h>
int main(void)
{
FILE *fp;
int c;
int b=0;
char s[25]; //declare array 's' to hold the filename
printf("Enter local filename: ");
fgets(s,24,stdin); //read string from keyboard into
array 's'
fp = fopen(s , "r"); //open the file referenced by
's' in read-mode.
while((c = fgetc(fp)) != EOF)
{
b++; //count number of characters in the file
}
fclose(fp);
printf("The number of characters in %s is:%d " ,s,b); //print out
total number of chars.
return 0;
}
Any suggestions would be welcome!