Working with Direct Access Files in C++

后端 未结 2 1951
生来不讨喜
生来不讨喜 2021-01-07 15:54

I am extremely new to C++ (and programming in general really) and am working on a project that has me stumped (not hard to do ha). The project involves Direct Access Files.

2条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-07 16:30

    Firstly, you seem to have 5 member variables in your class although there are only 4 data in each item. That's dangerous.

    After fixing that, I'd just read the whole file into a vector of those objects. For writing, just use a little loop to hop around the vector according to the next number.

    You don't need all those getters and setters. Never mind what Grady Booch says: you are the only programmer and you can trust yourself not to screw up your own data. Even if you couldn't, you could just as easily screw it up with the setters as by directly accessing a public member variable.

    Just read and parse the file with cin, scanf or whatever you prefer. If the fields are all fixed width then reading a known number of characters with fread is probably your simplest option. Use atoi to make numbers out of strings.

提交回复
热议问题