/// <summary> /// Writes a record to the file. /// </summary> /// <param name="record">A record to write.</param> /// <param name="offset">Offset to start from.</param> private void WriteRecord(FileCabinetRecord record, int offset) { this.fileStream.Seek(offset, SeekOrigin.Begin); this.fileStream.Write(new byte[2], 0, sizeof(short)); this.fileStream.Write(BitConverter.GetBytes(record.Id), 0, sizeof(int)); this.fileStream.Write(WorkWithBytesHelper.MakeStringOffset(record.FirstName, StringSize), 0, StringSize); this.fileStream.Write(WorkWithBytesHelper.MakeStringOffset(record.LastName, StringSize), 0, StringSize); this.fileStream.Write(BitConverter.GetBytes(record.DateOfBirth.Year), 0, sizeof(int)); this.fileStream.Write(BitConverter.GetBytes(record.DateOfBirth.Month), 0, sizeof(int)); this.fileStream.Write(BitConverter.GetBytes(record.DateOfBirth.Day), 0, sizeof(int)); this.fileStream.Write(BitConverter.GetBytes(record.FavouriteNumber), 0, sizeof(short)); this.fileStream.Write(BitConverter.GetBytes(record.FavouriteCharacter), 0, sizeof(char)); this.fileStream.Write(WorkWithBytesHelper.MakeStringOffset(record.FavouriteGame, StringSize), 0, StringSize); this.fileStream.Write(WorkWithBytesHelper.DecimalToBytes(record.Donations), 0, sizeof(decimal)); }
/// <summary> /// Reads the record from the file. /// </summary> /// <param name="offset">Offset to read from.</param> /// <returns>Record read.</returns> public FileCabinetRecord ReadRecord(int offset) { this.fileStream.Seek(offset, SeekOrigin.Begin); byte[] reservedBytes = new byte[2]; this.fileStream.Read(reservedBytes, 0, 2); byte[] bytes = new byte[sizeof(int)]; this.fileStream.Read(bytes, 0, sizeof(int)); int id = BitConverter.ToInt32(bytes); bytes = new byte[StringSize]; this.fileStream.Read(bytes, 0, StringSize); string firstName = WorkWithBytesHelper.RemoveOffset(Encoding.ASCII.GetString(bytes)); this.fileStream.Read(bytes, 0, StringSize); string lastName = WorkWithBytesHelper.RemoveOffset(Encoding.ASCII.GetString(bytes)); bytes = new byte[sizeof(int)]; this.fileStream.Read(bytes, 0, sizeof(int)); int year = BitConverter.ToInt32(bytes); this.fileStream.Read(bytes, 0, sizeof(int)); int month = BitConverter.ToInt32(bytes); this.fileStream.Read(bytes, 0, sizeof(int)); int day = BitConverter.ToInt32(bytes); bytes = new byte[sizeof(short)]; this.fileStream.Read(bytes, 0, sizeof(short)); short favNumber = BitConverter.ToInt16(bytes); bytes = new byte[sizeof(char)]; this.fileStream.Read(bytes, 0, sizeof(char)); char favCharacter = BitConverter.ToChar(bytes); bytes = new byte[StringSize]; this.fileStream.Read(bytes, 0, StringSize); string favGame = WorkWithBytesHelper.RemoveOffset(Encoding.ASCII.GetString(bytes)); bytes = new byte[sizeof(decimal)]; this.fileStream.Read(bytes, 0, sizeof(decimal)); decimal donations = WorkWithBytesHelper.BytesToDecimal(bytes); return(new FileCabinetRecord(id, firstName, lastName, new DateTime(year, month, day), favNumber, favCharacter, favGame, donations)); }