Frage

I am trying to code a game in C that deals with selection of races. Each races has their own "stories" and when the user chooses to read one of their stories, what I want to happen is,

While the program is running on Command Prompt, it will display the content I have typed in that specific text file about the story of the selected race.

This is what I have done so far.

void Race(char nameRace[20])
{
     int race_choice,race_choice2,race_story;
     FILE *race;
     FILE *race1;
     FILE *race2;
     FILE *race3;

     printf("The Races: 1.Human   2.Elf   3.Orc\n");
     printf("Press 1 for Details of Each Races or 2 for selection: ");
     scanf("%d",&race_choice);
     if (race_choice==1)
     {
          printf("Which Race do you wish to know about?\n\t1.The Human\n\t2.The Elf\n\t3.The Orc\n\t: ");
          scanf("%d",&race_story);
          if (race_story==1)
          {
               race1=fopen("race1.txt","r");
               fgetc(race1); // This does not display what I have typed on the race1.txt file on Command prompt.
               // And I plan to write 2~3 paragraphs on the race1.txt file.
               printf("\nGo Back to the Selection?(1 to Proceed)\n ");
               scanf("%d",&race_choice2);
               if (race_choice2==1)
               {
                    printf("\n\n");
                    Race(nameRace);
               }
               else
               {
                    wrongInput(race_choice2);// This is part of the entire code I have created. This works perfectly.
               }

          }
     }
}

Please help me? :) Please!

War es hilfreich?

Lösung

The functionality you seem to be lacking is the ability to read a text file and output it. So it might be a good idea to code up a function which does just this, and then you whenever you need to display the contents of a file you can just pass a file name to our function and let it take care of the work, e.g.

static void display_file(const char *file_name)
{
    FILE *f = fopen(file_name, "r");      // open the specified file
    if (f != NULL)
    {
        INT c;

        while ((c = fgetc(f)) != EOF)     // read character from file until EOF
        {
            putchar(c);                   // output character
        }
        fclose(f);
    }
}

and then within your code would just call this as e.g.

display_file("orcs.txt");

Andere Tipps

fgetc function reads, and returns single character from file, it doesn't prints it. So you will need to do following:

while(!feof(race1)) { // Following code will be executed until end of file is reached


char c = fgetc(race1); // Get char from file
printf("%c",c); // Print it
}

It will print contents of race1 char-by-char.

I think you'll probably want to read the file line by line, so it's best to use fgets() instead of fgetc().

Example:

while(!feof(race1)) // checks to see if end of file has been reached for race1
{
    char line[255]; // temporarily store line from text file here
    fgets(line,255,race1); // get string from race1 and store it in line, max 255 chars
    printf("%s",line); // print the line from the text file to the screen.
}

If you replace fgetc(race1) with the chunk of code above, it may work. I have not tried running it but it should work.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top