How do I change the contents of the file to *

This is a discussion on How do I change the contents of the file to * within the C++ Programming forums, part of the General Programming Boards category; I need to change the contents of this file to * For each letter in the file I need a ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    11

    How do I change the contents of the file to *

    I need to change the contents of this file to *
    For each letter in the file I need a *
    It's a programme to play hangman so I need the stars to 'cover' the letters that the user will have to guess. I just don't know how to print the contents of the file using stars instead. I don't want to put stars in the file instead of the letters because the user will have to guess them later. this is what I have written so far:

    Code:
                   char filename[FILENAME_MAX + 1];
    			FILE *fpin, *fstar;
    			char temp, x, i;
    			char readfile[SIZE];
    			char *read;
    			
    			
    			printf("\nGet the filename with the unknown word: ");
    			scanf("%s", filename);
    			
    			fpin=&filename[FILENAME_MAX +1];
    				
    				
    				if((fpin = fopen(filename, READONLY)) == NULL){
    					
    					printf("Cannot open file\n");
    					exit(EXIT_FAILURE);
    				
    				}
    				
    			printf("\nReady to start");
    			
    				if(fputc(x, fpin) == EOF){
    					if(!feof(fpin)){
    					printf("\nError reading from input file");
    					}
    					else if{
    					printf("\nEnd Of File");
    					}
    					else{
    					printf("\nmain: error writing to output file");
    					break;
    					}
    					}
    				
    			
    				i=fputc(x, fpin);
    				
    				printf("\n%c", i);

  2. #2
    Registered User
    Join Date
    Oct 2010
    Posts
    11
    I also thought I could count the number of entries in the file and then print that number of stars but I don't know how to print the number of stars to correspond the number of letters in the string

    Code:
    char filename[FILENAME_MAX + 1];
    			FILE *fpin, *fstar;
    			char temp, x, i;
    			char readfile[SIZE];
    			char *read;
    			
    			
    			printf("\nGet the filename with the unknown word: ");
    			scanf("%s", filename);
    			
    			fpin=&filename[FILENAME_MAX +1];
    				
    				
    				if((fpin = fopen(filename, READONLY)) == NULL){
    					
    					printf("Cannot open file\n");
    					exit(EXIT_FAILURE);
    				
    				}
    length=strlen(filename);
    printf(“\nNumber of characters in the string is=%d”,length);
    I just don't know how I would print the stars now

  3. #3
    Registered User
    Join Date
    Apr 2008
    Location
    Australia
    Posts
    55
    get the number of letters in the file, or size/length of the string, store that in int & use that for a loop to print stars.

    i.e.
    Code:
    int numberLetters = filename.size();
    for(int i = 0; i<numberLetters;i++)
    {
    cout<<stars<<"\n";
    }

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    11
    I've never used cout<<stars<<"\n";

    Does that just mean printf?

  5. #5
    Registered User
    Join Date
    Apr 2008
    Location
    Australia
    Posts
    55
    cout is for output, so yeah.

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    11
    I've tried a few different ways to write it with printf.

    Code:
    printf("%d", i);
    But I don't see how that will print the stars.
    How would I use printf to print the stars?

  7. #7
    Registered User
    Join Date
    Apr 2008
    Location
    Australia
    Posts
    55
    That might be because %d is not used for char
    printf - C++ Reference

  8. #8
    Registered User
    Join Date
    Oct 2010
    Posts
    5
    .
    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.
    .

  9. #9
    Registered User
    Join Date
    Oct 2010
    Posts
    11
    Thank you so much for that code. It's so helpful.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. combining file contents with command line
    By pxleyes in forum C Programming
    Replies: 4
    Last Post: 04-12-2004, 10:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21