N
N
nait1233212016-06-21 10:29:14
C++ / C#
nait123321, 2016-06-21 10:29:14

[Visual CLR] Why doesn't it edit the binary file?

Hello, the problem is this: I have a binary file with tests, I read it from a file in a richtextbox, all information is displayed in a text box, there I edit any word, after which I write all the text from the form to a file, and then an error occurs. Here is a screenshot:
lBfkc1gz8MU.jpg
Here is the code:

if (T == false)
  {
    FILE *file;
    S_Data data;
    file = fopen("test.txt", "r");
    richTextBox1->Clear();
    fread(&data, sizeof(data), 1, file);//читання з файлу
    richTextBox1->Text += marshal_as<String^>(data.question) + " " + marshal_as<String^>(data.answer1) + " " + marshal_as<String^>(data.answer2) + " " + marshal_as<String^>(data.answer3) + " " + marshal_as<String^>(data.answer4) + " " + marshal_as<String^>(data.answert);
    fread(&data, sizeof(data), 1, file);
    T = true;
    button1->Text = "Зберегти";
    while (!feof(file))  //поки не кінець файлу, виконуємо наступні дії
    {

      richTextBox1->Text += "\r\n";//переход на следующий ряд
      richTextBox1->Text += marshal_as<String^>(data.question) + " " + marshal_as<String^>(data.answer1) + " " + marshal_as<String^>(data.answer2) + " " + marshal_as<String^>(data.answer3) + " " + marshal_as<String^>(data.answer4) + " " + marshal_as<String^>(data.answert);
      fread(&data, sizeof(data), 1, file);
    }
    fclose(file);
  }
  else
    {
      T = false;
      button1->Text = "Тестування";
      FILE *file2;
      S_Data data;
      file2 = fopen("test.txt", "w");
    for (int i = 0; i < richTextBox1->Lines->Length; i++)//цикл по всем рядам
    {
      if (richTextBox1->Lines[i]->ToString() != "")//якщо текстовое поле не пустое
      {
        
        char*head = (char*)(void*)Marshal::StringToHGlobalAnsi(richTextBox1->Lines[i]->ToString());
        head = tok(data.question, head, ' ');
        if (head == 0)
        {
          MessageBox::Show("не вистачає питання", "помилка");
          return;
        }
        head = tok(data.answer1, head, ' ');
        if (head == 0)
        {
          MessageBox::Show("не вистачає 1 відопвіді", "помилка");
          return;
        }
        head = tok(data.answer2, head, ' ');
        if (head == 0)
        {
          MessageBox::Show("не вистачає 2 відповіді", "помилка");
          return;
        }
        head = tok(data.answer3, head, ' ');
        if (head == 0)
        {
          MessageBox::Show("не вистачає 3 відопвіді", "помилка");
          return;
        }
        head = tok(data.answer4, head, ' ');
        if (head == 0)
        {
          MessageBox::Show("не вистачає 4 відопвіді", "помилка");
          return;
        }
        head = tok(data.answert, head, ' ');
        if (head == 0)
        {
          MessageBox::Show("не вистачає правильної відопвіді", "помилка");
          return;
        }
        fwrite(&data, sizeof(data), 1, file2);//запис у файл
      }
    }
    fclose(file2);
    richTextBox1->Clear();
  }
  
}

Where is the mistake?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Kovalsky, 2016-06-21
@dmitryKovalskiy

You have code, studio and 2 (hopefully) hands. what prevents you from debugging to see the type of the exception? Or just walk through the algorithm? Your screenshot only says that the application crashed, and the code itself in this case is only half the success - the second is the environment (your machine on which you run this).

D
drc, 2016-06-22
@drc

After file2 = fopen("test.txt", "w"); It would be nice to check if the file has opened, otherwise it may not have opened, and you are trying to write there and close it later. And since the work with the file is binary, it's better to open it as wb.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question