Like Tree2Likes

Segmentation fault( counts number of words program )

This is a discussion on Segmentation fault( counts number of words program ) within the C Programming forums, part of the General Programming Boards category; Like I said.... > fscanf(inp, "%s", string[i]); Where do these point? > char ch, *string[10]; They're not pointing anywhere....

  1. #16
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,532
    Like I said....
    > fscanf(inp, "%s", string[i]);
    Where do these point?

    > char ch, *string[10];
    They're not pointing anywhere.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  2. #17
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Quote Originally Posted by Salem View Post
    Like I said....
    > fscanf(inp, "%s", string[i]);
    Where do these point?

    > char ch, *string[10];
    They're not pointing anywhere.
    Deja vu...
    If you understand what you're doing, you're not learning anything.

  3. #18
    Registered User
    Join Date
    Apr 2011
    Posts
    48
    @Salem ,
    I want to create two dimensional array.How can I fix this error.
    and bt gives
    #0 0xb7e8bb05 in _IO_vfscanf_internal (s=Cannot access memory at address 0xffffffff
    ) at vfscanf.c:1053
    #1 0xb7e914ea in __isoc99_fscanf (stream=0x804b008, format=0x8048949 "%s")
    at isoc99_fscanf.c:35
    #2 0x08048648 in main ()
    (gdb)

  4. #19
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,532
    Well before we get into the complexities of memory allocation, begin with say.
    char strings[100][100];

    Now limit your loop to reading in a MAXIMUM of 100 strings, each with a maximum of 100 characters (including the \0).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #20
    Registered User
    Join Date
    Apr 2011
    Posts
    48
    Quote Originally Posted by Salem View Post
    Well before we get into the complexities of memory allocation, begin with say.
    char strings[100][100];

    Now limit your loop to reading in a MAXIMUM of 100 strings, each with a maximum of 100 characters (including the \0).
    Now, it doesn't give error.But, I am not sure... Is that all?
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    int main(void)
    {
    	char ch, string[100][100];
    	int i = 0,j, words = 0, one_word = 0, two_word = 0, three_word = 0,\
    four_word = 0, five_word = 0, six_word = 0, seven_word = 0, eight_word = 0;
    	FILE *inp;
    	inp = fopen("met.text", "r");
    	if(inp == NULL)	{
    		printf("met.text cannot be opened");
    		return 1;
    	}	
    	while(fscanf(inp,"%c",&ch) == 1){
    		//fscanf(inp,"%c",&ch);
    		if(isspace(ch) == 0){
    			fscanf(inp, "%s", string[i]);
    			i++;
    			words++;
    		}
    	}
    	for(j = 0;j <= words;++j){
    		if( strlen(string[j]) == 1)
    			one_word++;
    		else if( strlen(string[j]) == 2)
    			two_word++;
    		else if( strlen(string[j]) == 3)
    			three_word++;
    		else if( strlen(string[j]) == 4)
    			four_word++;
    		else if ( strlen(string[j]) == 5)
    			five_word++;
    		else if ( strlen(string[j]) == 6)
    			six_word++;
    		else if ( strlen(string[j]) == 7)
    			seven_word++;
    		else if ( strlen(string[j]) == 8)
    			eight_word++;
    	}
    	printf("%d words, one letter = %d, two letter = %d, three letter = %d,\
    four letter = %d, five letter = %d, six letter = %d, seven letter = %d,\
    eight letter = %d", words, one_word, two_word, three_word, four_word,\
    five_word, six_word, seven_word, eight_word);
    	fclose(inp);
    	return(0);
    }

  6. #21
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Hard way...

    Code:
    for(j = 0;j <= words;++j){
      if( strlen(string[j]) == 1)
        one_word++;
      else if( strlen(string[j]) == 2)
        two_word++;
      else if( strlen(string[j]) == 3)
        three_word++;
      else if( strlen(string[j]) == 4)
       four_word++;
      else if ( strlen(string[j]) == 5)
       five_word++;
      else if ( strlen(string[j]) == 6)
       six_word++;
      else if ( strlen(string[j]) == 7)
       seven_word++;
      else if ( strlen(string[j]) == 8)
       eight_word++;
    	}
    Easy way...
    Code:
    char wordlist[100][33];  // for 100 words up to 32 characters each
    int wordlen[32] = {0};
    int word;
    int wordcount;
    
    for (word = 0; word < wordcount; word++)
      { wordlen[strlen(wordlist[word])]++; }
    Hard way...
    Code:
    while(fscanf(inp,"%c",&ch) == 1){  <-- note this will cause the first letter of each word to be missed
    if(isspace(ch) == 0){
    fscanf(inp, "%s", string[i]);
      i++;
      words++;
    }
    Easy way...
    Code:
    while (fscanf(inp,"%32s",wordlist[wordcount++]));
    Last edited by CommonTater; 05-12-2011 at 12:56 PM.

  7. #22
    Registered User
    Join Date
    Apr 2011
    Posts
    48
    Quote Originally Posted by CommonTater View Post
    Hard way...

    Code:
    for(j = 0;j <= words;++j){
      if( strlen(string[j]) == 1)
        one_word++;
      else if( strlen(string[j]) == 2)
        two_word++;
      else if( strlen(string[j]) == 3)
        three_word++;
      else if( strlen(string[j]) == 4)
       four_word++;
      else if ( strlen(string[j]) == 5)
       five_word++;
      else if ( strlen(string[j]) == 6)
       six_word++;
      else if ( strlen(string[j]) == 7)
       seven_word++;
      else if ( strlen(string[j]) == 8)
       eight_word++;
    	}
    Easy way...
    Code:
    char wordlist[100][33];  // for 100 words up to 32 characters each
    int wordlen[32] = {0};
    int word;
    int wordcount;
    
    for (word = 0; word < wordcount; word++)
      { wordlen[strlen(wordlist[word])]++; }
    Hard way...
    Code:
    while(fscanf(inp,"%c",&ch) == 1){  <-- note this will cause the first letter of each word to be missed
    if(isspace(ch) == 0){
    fscanf(inp, "%s", string[i]);
      i++;
      words++;
    }
    Easy way...
    Code:
    while (fscanf(inp,"%32s",wordlist[wordcount++]));
    Yes, but is it a bit complicated(Talking about your code)
    For example I have to print output like this
    Code:
    printf("%d words, one letter = %d, two letter = %d, three letter = %d,\
    four letter = %d, five letter = %d, six letter = %d, seven letter = %d,\
    eight letter = %d", words, one_word, two_word, three_word, four_word,\
    five_word, six_word, seven_word, eight_word);
    With your code how can it be?

  8. #23
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Hard way...
    Code:
    printf("%d words, one letter = %d, two letter = %d, three letter = %d,\
    four letter = %d, five letter = %d, six letter = %d, seven letter = %d,\
    eight letter = %d", words, one_word, two_word, three_word, four_word,\
    five_word, six_word, seven_word, eight_word);
    Easy way... (using variable names from before)
    Code:
    int disp;
    
    printf("Read %d words from the file\n",wordcount); 
    printf("Letter sizes of words...\n");
    
    for (disp = 0; disp < 32; disp++)
      printf("%d letters = %d\t",disp,wordlen[disp]);
    
    printf("\n\n");
    Take note that throughout this I'm not only showing you ways to do this with a lot less code, I've expanded the whole thing to track words up to 32 letters each. I've shown you how to reduce your file reading to a simple one line process and reduced your word counting to two lines. Then just above reduced your printout to two lines as well... So what you actually have there is about a 20 line program...

    You really do need to sit down and plan these things before you start coding. The stuff I've shown you is nothing a programmer at the mid point of first semester shouldn't be able to do...
    Last edited by CommonTater; 05-12-2011 at 04:14 PM.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Program that counts total prime number?
    By exe101 in forum C Programming
    Replies: 2
    Last Post: 04-07-2009, 01:18 AM
  2. words counts
    By lingz82 in forum C++ Programming
    Replies: 3
    Last Post: 08-31-2006, 05:16 AM
  3. Segmentation fault on my program
    By blackswan in forum C Programming
    Replies: 2
    Last Post: 05-11-2005, 04:47 PM
  4. Segmentation fault in beginning or program
    By tameeyore in forum C Programming
    Replies: 1
    Last Post: 02-26-2005, 07:16 PM
  5. Segmentation fault !, program works:S
    By jspri in forum C Programming
    Replies: 8
    Last Post: 09-28-2004, 05:25 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21