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]);
Users browsing this forum: No registered users and 1 guest