Here's my whole code (generates the wanted number of words and write it into a file called input file and then the recursive function will reverse the order of words and write it into another file called output file). Function GenerateWords works very good but that recursive function is giving me a headache. I need that the user will not be limited with some small number of words (0-1000). It must be higher. I could define Words[10000] but this is not good. I must learn that dynamic allocation. I tried Salem's code but I get nowhere. Help me.
Code:
# include <stdio.h>
# include <ctype.h>
# include <stdlib.h>
# include <string.h>
# include <time.h>
//# include <curses.h> library in linux
# define BEGIN_LOWER_CASE_CHAR 97
# define END_LOWER_CASE_CHAR 122
void Reversed_Input_Order_Recursive (unsigned int NoOfWords, FILE *fin, FILE *fout);
void WaitForKey(void);
void GenerateWords(unsigned int NoOfWords, FILE *fin);
int main ()
{
unsigned int NoOfWords= 0;
char NameFin[255] = {0}, NameFout[255] = {0};
FILE *fin= NULL, *fout= NULL;
printf ("\nEnter number of words you want to generate: ");
scanf ("%u", &NoOfWords);
printf ("Enter input file name: ");
scanf ("%s", NameFin);
if ((fin = fopen (NameFin, "wt+")) != NULL)
{
GenerateWords (NoOfWords, fin);
}
else
{
printf ("\nERROR:\nI can not open file \"%s\".\nCheck if the file exists.", NameFin);
}
fclose (fin);
if ((fin = fopen (NameFin, "rt+")) != NULL)
{
printf ("Enter output file name: ");
scanf ("%s", NameFout);
if ((fout = fopen (NameFout, "wt+")) != NULL)
{
Reversed_Input_Order_Recursive (NoOfWords, fin, fout);
}
else
{
printf ("\nERROR:\nI can not write into the file \"%s\".\nCheck if you entered the right path and file name.", NameFout);
}
fclose (fout);
}
else
{
printf ("\nERROR:\nI can not open file \"%s\".\nCheck if the file exists.", NameFin);
}
fclose (fin);
return 0;
}
void Reversed_Input_Order_Recursive (unsigned int NoOfWords, FILE *fin, FILE *fout)
{
char **Words = malloc (sizeof (*Words) * NoOfWords), Word[100] = {0};
static unsigned int i = 0;
if (NoOfWords == 1)
{
fscanf (fin, "%s", Word);
Words[i] = malloc ( strlen (Word)+1 );
strcpy (Words[i], Word);
fprintf (fout, "%s", Words[i]);
}
else if (NoOfWords > 1)
{
fscanf (fin, "%s", Word);
Words[i] = malloc (strlen (Word)+1);
strcpy (Words[i], Word);
Reversed_Input_Order_Recursive (NoOfWords - 1, fin, fout);
fprintf (fout, "%s", Words[i]);
}
i++;
}
void WaitForKey ()
{
fflush (stdin);
getchar ();
fflush (stdin);
}
void GenerateWords (unsigned int NoOfWords, FILE *fin)
{
char Word[10] = {0};
time_t Seed= 0;
unsigned int i = 0, j = 0, Character = 0, Lower = 3, Upper = 0;
// start random generator
srand ((unsigned) time (&Seed));
while (i != NoOfWords)
{
Upper = Lower + (rand () % 4); // Upper value is between 3 and 6
for (j = 0; j < Upper; j++)
{
Character = BEGIN_LOWER_CASE_CHAR + (rand () % (END_LOWER_CASE_CHAR+1 - BEGIN_LOWER_CASE_CHAR));
Word[j] = (char) Character;
}
Word[j] = '\0';
i++;
fprintf (fin, "%s", Word);
}
}
/*
void clrscr () // erase screen in linux
{
static int init;
if (init == 0)
{
initscr();
init = 1;
}
clear();
refresh();
}
*/