Thursday, September 22, 2011

How do i change this code to let it read a txt file of any size and not repeat a line over?

int initialize_array(char phrase[], char puzzle[], char clue[]){

FILE* phraseFile; /*input file*/

char temp[100]; /*temporary array*/

int line; /*line from which word and clue are chosen*/

size_t i=0; /*counter for loop*/

int len; /*length of line from file*/





line=rand()%43; /*randomization of line*/



phraseFile = fopen(%26quot;clues.txt%26quot;, %26quot;r%26quot;); /*opens file*/



if (!phraseFile) {

fprintf(stderr, %26quot;Oops - file not opened !\n%26quot;); /*if there is error in opening file program is exited*/

exit(1);

}



while(line--){

fgets(temp, 100, phraseFile); /*gets line and stores in temp*/

}



len=strlen(temp);

if(temp[len-1]=='\n')

temp[len-1]=0; /*places the null terminating character after word*/



/*separates clues from word*/

strcpy(clue,strtok(temp, %26quot;%%26quot;));

char tmp[WORD_LENGTH];

strcpy(tmp,strtok(NULL,%26quot;%%26quot;));

strcpy(phrase, tmp + 1);

strcpy(puzzle,phrase);





/*strcpy(puzzle,phrase);*/



while(i%26lt;strlen(puzzle)){

if(isalpha(puzzle[i])){

puzzle[i] = '*'; /*hides word*/

}

++i;

}

fclose(phraseFile); /*closes file*/

return i;

}How do i change this code to let it read a txt file of any size and not repeat a line over?
sort file | uniq



Just read thru the file and count the number of lines, and then randomly choose the whichline=rand()%NumLines;



With a phraseFile, it's faster to create the file using fixed Width fields so that all the lines are ... let's say 80 characters.

You can get the size of the file very fast with stat().



Let's say it return 3200 bytes. Therefore, you know you have 40 phrases.

Then to randomly pick a phrase in the file, just fseek() into the file whichline*80 bytes.



To avoid duplicates, do what you did with an array to keep track of which ones you saw already.How do i change this code to let it read a txt file of any size and not repeat a line over?
Not entirely sure what you want but this might be it...

To have it read a text file made up of any number of lines you'll need to first count the number of lines in the file like so...



char ch;

int iLines = 0;



while(!feof(phraseFile))

{

fread(%26amp;ch, 1, 1, phraseFile);



//Look for carrige return chars...

if((ch == '\r')||(ch == '\n'))

iLines++;

}



//next rewind the file pointer to the start of the file...

rewind(phraseFile);



//then instead of line = rand()%43

line = rand()%iLines;



That will allow it to read any number of lines

As for not repeating any line you'll need to store the lines already read and check them by expanding on the

%26quot;line = rand()%iLines%26quot; line like this...



static int *LinesRead = NULL;

int *TempBuf;

static int NumLinesRead = 0;

bool bValid = false;

if(LinesRead)

{

//list already exists so find a line in the file we havent

//already read

while(!bValid)

{

Line = rand()%iLines;

bValid = true;

for(int i=0; i%26lt;NumLinesRead; i++)

{

if(Line == LinesRead[i])

bValid = false;

}

}

TempBuf = new int[NumLinesRead+1];

for(i=0; i%26lt;NumLinesRead; i++)

{

TempBuf[i] = LinesRead[i];

}



delete [] LinesRead;

LinesRead = TempBuf;

LinesRead[NumLinesRead] = Line;

NumLinesRead++;

}

else

{

//first time

Line = rand()%iLines;

LinesRead = new int[1];

NumLinesRead = 1;

LinesRead[0] = Line;

}



//now go to read the lines out of the file...



This way you can be sure that you wont read the same line twice plus it will handle a text file of any size.



Place this code at the top of the function, replacing

line=rand()43;



I havent compiled this code but it should work.

Good luck!

No comments:

Post a Comment