Tell me, please, what is the error.
I have three functions: writing to a file, reading from a file, and counting records in a file.
There are 3 arguments in my program: 1st character, 2nd number of records that I want to output, and 3rd file name. Class declaration:
#pragma once #include <iostream> #include <string> #include "String.h" #include <fstream> #include <vector> #define string String #define END std::endl typedef double d; typedef int i; class City { private: string Name; i Ppl; d Area; i Birth; i Schools; std::ofstream wRecord; std::vector<string> lines; std::ifstream readRecord; public: City(); ~City(); void showInfo(); void getData(); void diskIn(int n,int r ); void diskOut(int num); static int diskCount(); }; Definition of class methods:
#include "City.h" #include "String.h" #include <iostream> #include <fstream> #include <io.h> City::City() : Name("NoName"), Ppl(0), Area(0.0), Birth(0), Schools(0) {} City::~City() { wRecord.close(); readRecord.close(); } void City::showInfo() { std::cout << "Name\t\t" << "People\t\t" << "Area\t\t" << "Birth\t\t" << "Schools\t\t" << END; std::cout <<""<<Name<<"\t\t" << Ppl<<"\t\t" << Area<<"\t\t" << Birth<<"\t\t" << Schools<< END; } void City::getData() { std::cout << "Enter a name of the city: "; (std::cin >> Name).get(); std::cout << "Enter quanity of people in the city: "; std::cin >> Ppl; std::cout << "Enter area of the city: "; std::cin >> Area; std::cout << "Enter a year of foundation of the city: "; std::cin >> Birth; std::cout << "Enter quanity of schools in the city: "; std::cin >> Schools; } void City::diskIn(int n,int r)//Чтение { std::ifstream readRecord; readRecord.open("6.bin", std::ios::binary); readRecord.seekg(n * sizeof(City)); std::cout << n << END; for (int i = 0; i < r; i++) { readRecord.read((char*)this, sizeof(*this)); } } void City::diskOut(int num) //Запись { std::ofstream wRecord; wRecord.open("6.bin", std::ios::binary); for (int i = 0; i < num; i++) { wRecord.write((char*)this, sizeof(*this)); } } int City::diskCount()//Подсчет { std::ifstream readRecord; readRecord.open("6.bin", std::ios::binary); readRecord.seekg(0, std::ios::end); return (int)readRecord.tellg() / sizeof(City); } My object:
City c; if (argv[1] == create){ int n = atoi(argv[2]); std::cout << "1 " << argv[1] <<"2 "<<argv[2] <<"3 "<<argv[3]<< END; for (int j = 0; j < n; j++) { c.getData(); c.diskOut(n); } } if (argv[1] == read) { int n = City::diskCount(); int r = atoi(argv[2]); std::cout << "1 " << argv[1] << "2 " << argv[2] << "3 " << argv[3] <<"r"<<n <<END; std::cout << "In file: " << n << "cities" << END; for (int i = 0; i < n; i++) { c.showInfo(); c.diskIn(i,r); } } My problem is that I cannot read from the recording file, only the default constructor is diskIn . The diskIn clearly in diskIn . It is necessary to read not the entire file, but a certain amount in it, if I remove my cycles from diskIn и diskOut , then the program works, BUT reads everything.
I want to ask the user how much he wants to see the records, if he wants more than what is in the file, then output the appropriate message and then output all the entries. And if there are 2 entries out of 3, then output exactly 2.
Cityclass declaration. In 99% of cases, read like this -readRecord.read((char*)this, sizeof(*this));- it's just run into trouble ... Yes, and what does your reading in a cycle in the same place mean? ... - HarryString, it has been checked several times with no problems. Read in a loop to countreadRecord.seekg(n * sizeof(City));how many in the file place - Nikita Gusev