The work of the program is as follows: it is written into a binary file in blocks of info about students. At each addition operation, the last block read from the file is checked for availability. If there is no space left, then a new one is created. The problem is this: when creating a block inside the function of adding and writing it, there are numerous letters "M" in the file, that is, as if the created block remained uninitialized, although the debugger says the opposite: enter image description here

The problem is solved if you create a global variable of the "block" type, but it seems to me that this is not the best way out of this situation. Below is the code of the method of adding a student to the file:

void AddStudent(Student tempStudent) { filePointer = fopen(FILE_NAME, "rb+"); if (fileSize == SIZE * BLOCK_SIZE) { numberOfBlocks++; } int pastePosition = -1; for (int i = 0; i < BLOCK_SIZE; i++) //Находим позицию в блоке для добавления студента { if (buffer.students[i].id == 0) { pastePosition = i; break; } } if (pastePosition != -1) //Если есть место в блоке, вставляем запись { buffer.students[pastePosition].id = tempStudent.id; strcpy(buffer.students[pastePosition].surname, tempStudent.surname); strcpy(buffer.students[pastePosition].name, tempStudent.name); strcpy(buffer.students[pastePosition].patronymic, tempStudent.patronymic); buffer.students[pastePosition].groupNumber = tempStudent.groupNumber; for (int i = pastePosition + 1; i < BLOCK_SIZE; i++)//Заполняем нулями остальные места в блоке { buffer.students[i].id = 0; strcpy(buffer.students[i].surname, ""); strcpy(buffer.students[i].name, ""); strcpy(buffer.students[i].patronymic, ""); buffer.students[i].groupNumber = 0; } fseek(filePointer, SIZE * BLOCK_SIZE * numberOfBlocks , SEEK_SET); cout << ftell(filePointer); system("pause"); fwrite(&buffer, sizeof(DataBlock), 1, filePointer); } else { DataBlock tempBlock; //Иначе создаём новую tempBlock.students[0].id = tempStudent.id; strcpy(tempBlock.students[0].surname, tempStudent.surname); strcpy(tempBlock.students[0].name, tempStudent.name); strcpy(tempBlock.students[0].patronymic, tempStudent.patronymic); tempBlock.students[0].groupNumber = tempStudent.groupNumber; for (int i = 1; i < BLOCK_SIZE; i++) //Заполняем нулями остальные места в блоке { tempBlock.students[i].id = 0; strcpy(tempBlock.students[i].surname, ""); strcpy(tempBlock.students[i].name, ""); strcpy(tempBlock.students[i].patronymic, ""); tempBlock.students[i].groupNumber = 0; } numberOfBlocks++; fseek(filePointer, SIZE * BLOCK_SIZE * numberOfBlocks, SEEK_SET); cout << ftell(filePointer); system("pause"); fwrite(&tempBlock, sizeof(DataBlock), 1, filePointer); } fseek(filePointer, 0, SEEK_END); fileSize = ftell(filePointer); fclose(filePointer); } 
  • What is the question if using a global variable you solved the problem. - Yaroslav
  • Local variable, garbage, during recording, the buffer is not completely filled - of course, the field is described as char [100], and only 20 characters are written ... No? - Harry
  • one
    Only cout << ftell(filePointer); - Yaroslav
  • when you create a local variable, it is not reset by automaton. But global - reset. Zero handles (memset for example, but neatly). And in general, this is a big security hole, but you think this is not so scary yet. - KoVadim
  • one
    You have not provided a definition for DataBlock. But I'll stay KO. Code of the form strcpy(tempBlock.students[i].surname, ""); in fact, it simply writes one zero to the very first byte, and does not touch the rest. - KoVadim

0