Like I said....
> fscanf(inp, "%s", string[i]);
Where do these point?
> char ch, *string[10];
They're not pointing anywhere.
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.
@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)
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.
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); }
Hard way...
Easy 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++; }
Hard 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])]++; }
Easy 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++; }
Code:while (fscanf(inp,"%32s",wordlist[wordcount++]));
Last edited by CommonTater; 05-12-2011 at 12:56 PM.
Yes, but is it a bit complicated(Talking about your code)
For example I have to print output like this
With your code how can it be?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);
Hard way...
Easy way... (using variable names from before)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);
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...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");
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.