I need to write a program, in C++, that will read from a .dat file

How do I make one? My book mentions a header line, etc. It does not go into any more detail and I can't find a description of how to do it online.

Am I making this harder than I need to?

Thanks,
MB1

Dani AI

Generated

You are not overthinking it, just mixing two ideas. The .dat extension does not define a special file format here; it is usually just a name. For a homework assignment like this, your instructor almost certainly means a plain text file with a header on the first line and rows of data after it. Your sample is fine. Save it as Scores.dat in the same folder as your program (or use an absolute path). As @Dave Sinkula and hinted, that is all you need. If you were asked for a binary file, that would be a different format entirely, as @Ancient Dragon noted.

Here is a simple way to read a header line once, then process each row as whitespace‑separated fields. It computes an average and a letter grade.

#include <fstream>
#include <iostream>
#include <string>

char letter(double avg) {
    if (avg >= 90) return 'A';
    if (avg >= 80) return 'B';
    if (avg >= 70) return 'C';
    if (avg >= 60) return 'D';
    return 'F';
}

int main() {
    std::ifstream in("Scores.dat");
    if (!in) { std::cerr << "Could not open Scores.dat\n"; return 1; }

    std::string header;
    std::getline(in, header); // skip the header line

    std::string name;
    int s1, s2, s3, s4, s5;
    while (in >> name >> s1 >> s2 >> s3 >> s4 >> s5) {
        double avg = (s1 + s2 + s3 + s4 + s5) / 5.0;
        std::cout << name << " avg=" << avg << " grade=" << letter(avg) << "\n";
    }
}

Tips:

  • Keep fields separated by spaces or tabs. Be consistent.
  • If names may contain spaces, read each whole data line with std::getline, then parse it with std::istringstream or switch to a CSV format.
  • If your instructor truly wants binary, they must specify the exact layout; text and binary are not interchangeable.

Recommended Answers

All 6 Replies

All I can figure is to make a .txt file and call it Scores.dat

In the file:

Names       Score1     Score2     Score3      Score4      Score5
Sims          91         89         73          81          93

I'm supposed to read from the .dat file and compute the scores and assign a letter grade. I've got the program but I have no idea on where to start to make a .dat file.
Thanks for getting back with me.

I've got the program

You've got a program that reads from the .dat file, so why not post that?

program but I have no idea on where to start to make a .dat file.

All I can figure is to make a .txt file and call it Scores.dat

I'm confused. As far as I know, a .dat file is just the same as any other text file. It just a name. Or do you draw a distinction?

I though there was a distinction. I assume I just write the headings with the appropriate information under the headings.

I didn't write the C++ code because I wrote it to read an array. I was confused with the .dat issue.

Yes a .dat is just a different file extention. You can create this in windows with just notepad and when you save it make it filename.dat. Actually, in Linix/Unix it is the same. Just save it with filename.dat.

Its been my experience that .dat files contain binary data, such as the computer's binary representation of an integer and not the ascii digits that you will find in a text file. .dat files normally can not be easily read by text editors such as Notepad.exe.
Exemple:

struct mystruct
{
   int a;
   long b;
   float c;
   double d;
};

int main()
{
    myruct s 

   // write the structure to the file
   ofstream out("mydata.dat", ios::binary);
   out.write((char *)&s,sizeof(s));
   out.close();

   // now read the file
   ifstream in("mydata.dat", ios::binary);
   in.read((char*)&s,sizeof(s));
   in.close();
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.