Random Sentence from a File

Write here if you have problems with your C source code

Moderator: Board moderators

Post Reply
gandil
New poster
Posts: 1
Joined: Sun Jan 08, 2006 2:28 pm

Random Sentence from a File

Post by gandil » Sun Jan 08, 2006 5:20 pm

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; 

}

Fr3eM4n
New poster
Posts: 4
Joined: Sun May 29, 2005 11:45 am
Location: Romania

Post by Fr3eM4n » Sat Feb 18, 2006 9:45 pm

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

User avatar
Krzysztof Duleba
Guru
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland
Contact:

Post by Krzysztof Duleba » Sat Feb 18, 2006 10:18 pm

In your code line and slen are uninitialized pointers.
For millions of years, mankind lived just like the animals. Then something happened which unleashed the power of our imagination. We learned to talk and we learned to listen...

Post Reply

Return to “C”