.
Judging from the previous posts, I don't think this is a c++ question so I'll keep it 'C' syntax to show an example of another way to do this.
Start off by putting all your 'read' stuff in it's own function:
Code:
char* yourReadFunction()
{
long fsize;
char stars[255];
static char buf[255];
char fname[] = "MyFilename.ext";
FILE *fp;
//try opening file
if((fp=fopem(fname,"rt"))==NULL)
return (char *)NULL;
/*
getting the whole file like this isn't
a good idea for just one string...
//get file-size
fseek(fp,0,SEEK_END);
fsize=ftell(fp);
rewind(fp);
//read-in file, don't overrun buffer...
fread(buf, 1, sizeof(buf)-1, fp);
*/
//instead, just get the first -significant- line,
//bunp past any CRLFs on empty lines.
while(!feof(fp)){
fgets(buf, sizeof(buf)-1, fp);
if( (strlen(buf))>2 )
break;
}
//done with file
fclose(fp);
//clear and set stars buffer
memset(stars, 0, sizeof(stars));
memset(stars, '*', strlen(buf);
//print to stdout, set cursor to overwrite
printf("%s\r", stars);
//return file-buffer to caller
return buf;
}
Once the calling function has the buffer, you can check the keyboard input against the buffer-contents to see if the user gets a match on any letters then print a new string with correct letter guesses in the right positions and '*' in unguessed positions.
Using '\r' instead of '\n' will keep the console output always on the same line.
When dealing with returned buffers, it's a good idea for the caller to get it's own local copy, so rather than depending on the scope of a static variable in another function, do something like this:
Code:
int main(int argv, char **argc)
{
char buf[255];
strcpy(buf, yourReadFunction());
if(*buf==NULL){
printf("Read Function failed\n");
return 0;
}
//continue processing buf[]...
return 0;
}
Don't copy/paste any of this code and it expect it to run without errors, none of it has been checked/compiled, it's just to give you some ideas.
.