-4

I've tried everything I can think of, I've tried Chegg and Google, but I'm still stuck on getting the number of creatures in a file. I've tried while and for loops, it's only supposed to be two lines of code. I can't even get it with more than that.

#include <iostream>
#include <cstdlib>
#include <string>
#include <vector>
#include <fstream>
#include <algorithm>
#include <iomanip>
using namespace std;

// Global variables
const int CREATURE_NAME_SIZE = 30;          // Max length of a creature name
const int CREATURE_TYPE_SIZE = 26;          // Max length of a creature type
const string FILENAME = "creatures.dat";    // Name of file that contains the data

// struct used to create records in file
struct Creature
{
    char name[CREATURE_NAME_SIZE];              // Name of the creature
    char creatureType[CREATURE_TYPE_SIZE];      // Type of creature
    int hp;                                     // Hit points for creature
    int armor;                                  // Armor class for creature
    int speed;                                  // Speed of creature
};

// This function returns true if the name of the left creature is less than the name of the right creature
// Use this function when running the sort function
bool sortByName(const Creature &lhs, const Creature &rhs)
{
    string name1(lhs.name), name2(rhs.name);
    return name1 < name2;
}

// Function declarations
// You will need to code the definitions for each of these functions
int getCreatureNumber(int numCreatures);
void displayCreature(fstream& file, int num);
void displaySorted(fstream& file);

int main()
{
    char choice;            // choice made by user for menu
    fstream creatureFile;   // file stream for input file
    int numCreatures;       // the number of creatures saved to the file

    // Open the creatureFile for input and binary (one statement of code)
    creatureFile.open(FILENAME, ios::out | ios::in | ios::ate | ios::binary);

    // Get the number of creatures in the file (two statements of code)
    // The number of creatures should be assigned to numCreatures
halfer
  • 19,824
  • 17
  • 99
  • 186
  • 5
    Your post seems to be missing the very code you say you tried. And we're not mind readers; we have no idea what `creatures.dat` even looks like, whether it is fixed-length records or require per-line dissection. In short, there isn't enough information to even hazard a guess, much less a definitive answer to your question. I can *guess*, based on the `ios::binary` open mode, you're using fixed length records. If that is the case, your post should say as much. If true, seek to the end of the file, (one stmt), then divide the file position by the size of a `Creature` instance (second stmt). – WhozCraig Sep 29 '18 at 17:30
  • 1
    Please edit your post with a sample of the input data. – Thomas Matthews Sep 29 '18 at 19:52

1 Answers1

-2

Probably this should work

Creature creature;
while(!creatureFile.eof())
{
    creatureFile.read((char *)&creature, sizeof(creature));
    numCreatures++;
}
cout<<"Count is : "<<numCreatures;

While loop checks for the end of file.

read() reads a Creature sized structure from creatures.dat.

addy
  • 52
  • 1
  • 2
  • 5
  • 1
    [`while(!creatureFile.eof())`](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) You better shouldn't advise that. – πάντα ῥεῖ Sep 29 '18 at 17:39