Segmentation Fault

I cannot figure out where I am getting a segmentation fault in this co

Page 1 of 1

7 Replies - 1207 Views - Last Post: 31 October 2009 - 09:11 AM Rate Topic: -----

#1 afunkhou  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 13-September 09

Segmentation Fault

Posted 30 October 2009 - 10:20 PM

 #include <stdio.h>

int fill(char a[], char fn[]);
void Print(char a[], int n);
int countVowels(char a[], int n);

int main()
{
		char a[10000];
		int n, v;
		char fn[30];
		printf("name of file:");		
				scanf("%s", &fn);		
				n = fill(a, fn);
		Print(a,n);
		v = countVowels(a, n);
		printf("The number of vowels in the file is %d", v);
		return 0;
}

int countVowels(char a[], int n)
{
		int i, count;
				count = 0;
		for(i = 0; i < n; i++)
		{
				if(a[i] == 'a' || a[i] == 'e'|| a[i] == 'i' || a[i] == 'o' || a[i] == 'u' || a[i] == 'A' || a[i] == 'E' || a[i] == 'I'|| a[i] == 'O' || a[i] == 'U')
						count = count + 1;
		}
		return count;
}

void Print(char a[], int n)
{
		int i;
		for(i = 0; i < n; i++)
				printf("%c", a[i]);
		printf("\n");
}

int fill(char a[], char fn[])
{
		FILE *stream;
		int n;
		n = 0;
		fscanf(stream, "%c", &a[n]);
		while(!feof(stream))//while not end of file stream, do this:
		{
				n++;
				fscanf(stream, "%c", &a[n]);
		}
		fclose(stream);
		return n;
}


This post has been edited by afunkhou: 30 October 2009 - 10:21 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Segmentation Fault

#2 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Segmentation Fault

Posted 30 October 2009 - 10:32 PM

It's time for you to learn about using a debugger. With some training, you should be able to at least isolate the problem.
Was This Post Helpful? 0
  • +
  • -

#3 jbeme  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 93
  • Joined: 30-October 09

Re: Segmentation Fault

Posted 30 October 2009 - 11:02 PM

Assisgn each element in the array to a single variable before it enters your function. example: x = a[];
Toy around with that for a bit and you might come to your solution.

View Postjbeme, on 30 Oct, 2009 - 09:58 PM, said:

Assisgn each element in the array to a single variable before it enters your function. example: x = a[];
Toy around with that for a bit and you might come to your solution.
Try printing a line a in different spots that print out the variable, see if its what you expect.

This post has been edited by jbeme: 30 October 2009 - 10:59 PM

Was This Post Helpful? 0
  • +
  • -

#4 teknique  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 8
  • Joined: 11-April 06

Re: Segmentation Fault

Posted 30 October 2009 - 11:24 PM

Your problem would probably have to do with the array char a[10000], because you return n for the number of characters within that file so if n > 10000 your other functions are going to keep looking past the end of the array and cause your segmentation fault trying to read from other memory locations.

For instance if I made a text file with 10001 characters your program would break.

This post has been edited by teknique: 30 October 2009 - 11:25 PM

Was This Post Helpful? 0
  • +
  • -

#5 lanysmiles  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 03-October 09

Re: Segmentation Fault

Posted 31 October 2009 - 03:58 AM

why don't you try to comment out [using /* */ ] different sections of your code, and recompiling to find where the fault is?
Was This Post Helpful? 0
  • +
  • -

#6 afunkhou  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 13-September 09

Re: Segmentation Fault

Posted 31 October 2009 - 08:22 AM

Alright, I've tried a few of the things suggested. I tried assigning the arrays to a variable, but that didn't seem do do anything different in the program. I also tried commenting out each function to see where the problem was, and I still got a segmentation fault each time, except when I commented out "fill", and that still gave me a segmentation fault except that I got a bunch of question marks and stuff before it printed "segmentation fault" I also expanded the size of the array, but I have tried testing the file on a program that only says "hello".

Could it be a problem with the pointers? Does anyone see a problem with those?

I'm pretty sure the problem is somewhere in my 'fill' function, because that was the only one that did anything differently when I ran it.

Also, I changed my main to do another printf function so it looks like this:

 int main(){
		char a[100000];
		int n;
		int v;
		char fn[30];
		printf("name of file:");
		scanf("%s", fn);
		printf("okay, I will count the vowels in %s\n", fn);
		n = fill(a, fn);
		Print(a,n);
		v = countVowels(a, n);
		printf("The number of vowels in the file is %d", v);
		return 0;
}



The second printf function is the new one, and when I run the program now, it will prompt me to enter the filename, print out "okay, I will count the vowels in file" and then the program does not terminate.
Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6092
  • View blog
  • Posts: 23,612
  • Joined: 23-August 08

Re: Segmentation Fault

Posted 31 October 2009 - 08:47 AM

FILE *stream;
        int n;
        n = 0;
        fscanf(stream, "%c", &a[n]);


Wouldn't you REALLY like to open that stream before you operate on it? Who knows WHERE that FILE * is pointing?
Was This Post Helpful? 1
  • +
  • -

#8 afunkhou  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 13-September 09

Re: Segmentation Fault

Posted 31 October 2009 - 09:11 AM

Oh my goodness! I can't believe I didn't catch that!

Thank you!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1