Page 1 of 1

Random Sentence from a File

Posted: Sun Jan 08, 2006 5:20 pm
by gandil
Hi;
I wanna write a program which will show a random sentence from a specific file for each execution. Each sentence will separated by "\n".

I wrote a code but it isn't work. is there any suggestion.

Please help me...

My Code:

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <conio.h>
#include <math.h>


void main(void)
{
char **line,c;
int i=0,j=0,*slen,a,k;
FILE *myfile;

myfile = fopen("mycookies.txt","r"); 
if (myfile == NULL)
{
printg("Warning!Cannot open file.");
exit -1;
}


while ( !feof(myfile) )
{
fscanf(myfile,"%c",c);
while(c!='\n')
{
line[i][j]=c;
fscanf(myfile,"%c",&c);
j++; 
}
slen[i]=j;
i++

}

a=randomize(i);
for(k=0;k<slen[i];k++)
printf("%c",line[a][k]);

getch();
return 0; 

}

Posted: Sat Feb 18, 2006 9:45 pm
by Fr3eM4n
my advice is to use fgets() to read lines of input. And you can use it's return value to see if the the read was successfull or not.
http://www.cplusplus.com/ref/cstdio/fgets.html

Posted: Sat Feb 18, 2006 10:18 pm
by Krzysztof Duleba
In your code line and slen are uninitialized pointers.