Originally Posted by
killpoppop
But as far as i can see no word is larger than 30 characters. But i'm obvious not on your wavelength =]
Yes, I noticed that, so the problem lies elsewhere. Unlike MK27, I experienced a crash when running your code.
The first thing I would do is to avoid dynamic memory allocation:
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
void read( FILE *text )
{
int i = 0;
int z;
char temp[30];
/* Reads in the words one at a time to an array */
while( ( z = fgetc(text) ) != EOF )
if( i < ( sizeof( temp ) - 1 ) && ( isalpha( z ) || z == '-' || z == '\'' ) )
{
temp[i] = toupper( z );
i++;
}
else if( i != 0 )
{
temp[i] = '\0';
printf( "%s %d\n", temp, strlen( temp ) );
i = 0;
}
}
int main( int argc, char *argv[] )
{
FILE *text;
if( argc != 2 )
{
printf( "Incorrect Syntax" );
}
else
{
text = fopen( argv[1], "r" );
read( text );
}
return 0 ;
}
Now, if you are to return a pointer from read() so that it can be linked into a linked list, you would need to use dynamic allocation instead of merely printing out the string (and in fact no printing should be done in read(), except maybe to print an error message).