Note that there are some explanatory texts on larger screens.

plurals
  1. POwhy my program "creating index" doesn't work?
    primarykey
    data
    text
    <p>I am coding a program which takes a text file as an input, makes the index of the words of it and prints the output(the index) in a file and in the screen. I coded as below and <strong>tried much to spot the problem or at least narrow it down, but I couldn't.</strong> If anyone can help or spots a problem in syntax or the logic of the code, I would be happy to know.</p> <pre><code>void main(int argc, char * argv[]) { //clearscreen clrscr(); //if arguments are less that default of the program if (argc &lt; 2) { cout &lt;&lt; "You should've input 3 arguments." ; return; } //opening the input file and defining a pointer which points to it as argv[1] FILE *fPtr = fopen(argv[1], "r+"); //defining a 2D array to hold maximum to 200 words holding maximum to 20 characters char words[200][20]; //initializing words 2D array with zero ASCII for(int i = 0; i &lt; 200; i++) for(int j = 0; j &lt; 20; j++) words[i][j] = 255; //defining an 2D whi array which is supposed to show how many times words are placed in which lines in a defined layout //it holds maximum to 200 words holding maximum 100 sentences int index[200][100]; //initializing the index 2D array with zero ASCII for(i = 0; i &lt; 200; i++) for(j = 0; j &lt; 100; j++) index[i][j] = 0; //this array of characters max to 2000 is supposed to hold each line which is gotten with fgets char buff[2000]; //initializing the buff array with zero ASCII for(i = 0; i &lt; 2000; i++) buff[i] = 0; //the max of the words used in the source file is 200. but this valuable named as 'last' says how many words are used in this source file //its initalized as no words is held int last = 0; //defining a pointer to char of punctuation mark characters. all ASCII codes expect for a - z, A - Z, 0 - 9 and newline char *punc; //initializing punctuation marks array for(i = 0; i &lt; 256; i++) { if(i == 10) continue; if(i &gt;= '0' &amp;&amp; i &lt;= '9') continue; if(i &gt;= 'a' &amp;&amp; i &lt;= 'z') continue; if(i &gt;= 'A' &amp;&amp; i &lt;= 'Z') continue; char *string = (char *) &amp;i; strncat(punc, string, 1); } //how many lines is read from the source file int lineread = 0; //a word which the processes are done on that char *word; //one line read while( fgets(buff, 1999, fPtr) != NULL) { //how many words is read from the source file in the current line int wordread = 0; word = strtok(buff, punc); //one word read while( word != NULL) { //sorting int k = 0; while(strcmp(word, words[k])&gt; 0) k++; if(strcmp(word, words[k])) { for(int l = last; l &gt;= 0; l--) { strcpy(words[l + 1],words[l]); for(int o = 0; o &lt;= lineread; o++) index[l + 1][o] = index[l][o]; } last++; strcpy(words[k],word); for(l = 0; l &lt;= lineread; l++) index[k][l] = 0; } index[k][lineread]++; wordread++;//go to next word word = strtok(NULL, punc); } lineread++;//go to next line } //closing the input file fclose(fPtr); //opening the output file and defining a pointer which points to it as argv[2] FILE *fPtr2 = fopen(argv[2], "w+"); //showing the index in cmd for(i = 0; i &lt;= last; i++) { printf("%-20s" , words[i]); fprinf(fPtr2, "%-20s" , words[i]); int m = 0; for(j = 0; j &lt;= lineread; j++) { if(m) { printf("%c", ','); fprintf(fPtr2, "%c", ','); } if(index[i][j]) { printf("%i", j + 1); fprintf(fPtr2, "%i" ,j + 1); } if(index[i][j] &gt; 1) { printf("(%i)", index[i][j]); fprintf(fPtr2, "(%i)", index[i][j]); m = 1; } } printf("\n"); fprintf(fPtr2, "\n"); } //closing the output file fclose(fPtr); } </code></pre> <p>now it errors as Abnormal program termination NULL pointer assignment. I have to use turbo c and I use DOS SHELL. there's a file names as "input.txt" in resourse. and in DOS SHELL I write this:</p> <pre><code>programname.exe input.txt output.txt </code></pre> <p>and my desired output if input.txt is this:</p> <pre><code>hello. hello. how are you? hello. desired output: hello 1(2),3 //2 times in line 2, 1 time in line 1 how 2 //1 time in line 2 are 2 //1 time in line 2 you 3 //1 time in line 3 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload