stdin is a FILE*. What's the problem?
yes stdin is a FILE* but (ch = getchar()) is not. When user input EOF
(Ctrl-D), then ch has that value, how to check that ?
/* A program that will ask the user for input and then will sort the input alphabetically
and will print it on stdout
* As of now it does not sort any words but it will very soon. I am writing it in parts
* when parts work fine,then we put them in one place
* Since I did not want to put any limitation on input size in this program, I have used
* dynamic memory allocation to solve the problem. My intent in creating and then solving
* this problem was purely of C learning (as defined by ANSI standard) and nothing else. I
* wanted to learn C and hence this problem and quite heavy discussion of it on comp.lang.c.
* I owe many thanks to the brilliant help I got in comp.lang.c
*
* VERSION 1.0
*
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
enum { WORD_SIZE = 3, WORD_ERR = -1 };
enum { GSW_OK, GSW_ENOMEM, GSW_ENORESIZE, GSW_EINPUT } ;
enum { GI_OK = 0 , GI_ERR = WORD_ERR };
int get_words( char**, size_t* );
int get_single_word( char*** );
int main( void )
{
char* pword;
int input_err;
size_t words_count;
pword = NULL;
words_count = 0;
input_err = WORD_ERR; /* we will update the status in function call to get_input() */
input_err = get_words( &pword, &words_count );
if( WORD_ERR != input_err )
{
printf("words_count = %u\n", words_count);
}
/* call to - sort_input(...)
call to - printf_input(...) */
free( pword );
return 0;
}
int get_words( char** pword, size_t* w_cnt )
{
int err_catcher;
*w_cnt = 0;
while( (err_catcher = get_single_word(&pword)) && ( **pword != 0) )
{
if( GSW_OK == err_catcher )
{
++*w_cnt;
}
else
{
return GI_ERR;
}
}
return GI_OK;
}
int get_single_word( char*** ppc )
{
unsigned ele_num;
int ch;
size_t word_length, word_length_interval;
char* word_begin;
char* new_mem;
ele_num = 0;
word_length = WORD_SIZE;
word_length_interval = 2;
**ppc = malloc(word_length * sizeof(**ppc));
word_begin = **ppc;
if( NULL == word_begin )
{
return GSW_ENOMEM;
}
while( (EOF != (ch = getchar())) && isspace(ch) )
{
continue; /* Leading whitespace */
}
if( EOF != ch )
{
*word_begin++ = ch;
ele_num = 1;
}
while( (EOF != (ch = getchar())) && (! isspace(ch)) )
{
if( (word_length - 1) == ele_num++ )
{
new_mem = realloc( **ppc, (word_length_interval * word_length * sizeof *new_mem) );
if( new_mem )
{
word_begin = new_mem + (word_begin - **ppc);
word_length *= word_length_interval;
**ppc = new_mem;
}
else
{
*word_begin = '\0';
return GSW_ENORESIZE;
}
}
*word_begin++ = ch;
}
if( feof(ch) )
{
*word_begin = '\0';
}
else if( ferror(ch) )
{
return GSW_EINPUT;
}
return GSW_OK;
}
=========================== OUTPUT =======================================
[arnuld@dune ztest]$ gcc -ansi -pedantic -Wall -Wextra sort-input.c
sort-input.c: In function `get_single_word':
sort-input.c:147: warning: passing arg 1 of `feof' makes pointer from integer without a cast
sort-input.c:151: warning: passing arg 1 of `ferror' makes pointer from integer without a cast
[arnuld@dune ztest]$