abort();

Sep 12, 2006 23:28



char **words;

char* getword(FILE *infile);
void swap(int up, int down);

int main(int argc, char* argv[])
{
FILE *infile;
char **temp;
int size = 10;
int wordcounter = 0;
int wordnumber = 0;
int i, j, current;
int occurances = 1;

if( (infile = fopen(*++argv*, "r")) == NULL)
{
printf("Opening source file failed \n");
return 1;
}
else
{
/*printf("Source file opened \n");*/
words = malloc(size * sizeof(char*));
if(!words)
{
printf("Memory allocation failed \n");
abort();
}
/*printf("Memory allocated \n");*/

for(wordcounter=0; (words[wordcounter]=getword(infile)) != NULL; wordcounter++)
{
/*printf("Wordcounter value %d \n", wordcounter);*/
if( wordcounter == size - 1)
{
size = size + 10;
temp = realloc(words, size * sizeof(char*));
if( temp == NULL )
{
printf("Memory reallocation in main loop failed \n");
abort();
}
else
words = temp;
/*printf("Memory in main reallocated \n");*/
}
wordnumber = wordcounter;
}
/*printf("First wordcounter loop closed \n");*/

for(i=0; i< wordnumber; i++)
{
printf("Sorting word %s \n", words[i]);
for(j=0; j< wordnumber; j++)
{
if( strcmp(words[j], words[i]) > 0)
swap(i, j);
}
} /* source code truncated */

This strange thingie above is half of main function in C program that reads source file and prints all words and number of their occurances to the output.
So, the question is, why the hell this shite doesn't want to process words?
Function getword() and word extracting main() loop are working correctly.
Alphabetical sorting works well enough.
There's no trouble in memory reallocating and data processing in swap.
... see that little * after *++argv? It should be fopen(*++argv, "r").
And it took one hour to resolve.
'Cause the damned thing kept telling me that file was opened successfully. I wonder, what the heck was this crap opening? *headdesk*
I'm not going to post the rest of main() here. The only thing I didn't do was deleting duplicates. That is, my program covered 6 from 7 specified points.
But it is has to be perfect or you won't pass.
... and I needed one hour more to allocate another array and debug it.
Exactly one hour spent for looking for this goddamned *.

@#$%^&*@#$%^&*!!!!!!!!

ife, real life

Previous post Next post
Up