Reading text question
Posted: Wed May 31, 2006 8:55 pm
How do I make GE read text file (e.g. "a.txt"), select random line there and copy line's text to variable for further use? Thanks in advance.
char textArray[10][256]; //Max 10 text of 255 characters
int nText = 0; //Number of texts read
void readText(char * fileName)
{
char line[256];
FILE *arq = fopen(fileName, "r");
if(arq)
{
while(fgets(line, 255, arq) && nText < 10)
{
if(strlen(line) > 0) //Dont put empty lines
{
strcpy(textArray[nText], line);
nText++;
}
}
}
}
readText("mytext.txt");
int n = rand(nText);
strcpy(randomText.text, textArray[n]);
Murd-Machine[_] wrote:How do I read text with:
/n - to change lines
char textArray[10][256]; //Max 10 text of 255 characters
int nText = 0; //Number of texts read
void readText(char * fileName)
{
char line[256];
int i;
FILE *arq = fopen(fileName, "r");
if(arq)
{
while(fgets(line, 255, arq) && nText < 10)
{
if(strlen(line) > 0) //Dont put empty lines
{
//Make multiline if found | char
for(i = 0; i < strlen(line); i++)
{
if(line[i] == '|') line[i] = 10; //10 = Line-Feed, look the ASCII table
}
//Copy to array
strcpy(textArray[nText], line);
nText++;
}
}
fclose(arq);
}
if(if(line[0] == '*')
{
//set value if found}
else
{
//set value if not found
}
strcpy(textArray[nText], &line[1]);