E
E
Eugene Ignatik2014-06-10 18:59:32
C++ / C#
Eugene Ignatik, 2014-06-10 18:59:32

Problem with working with C files

Comrades! There is just a very big question:
In general, the task is "Develop a program for filling a binary file with integers in the interval [-150; 150]. The number of file components is entered from the keyboard. Zero the file components (directly in the file) with the maximum value (there may some)." Wrote, but for some reason, when I want to replace the numbers coinciding with the maximum, the cycle goes to infinity. It continues to read the file, and writes it first, adding more and more new portions, but EOF never reaches (put a comment near the problem area). The most interesting thing is that no matter how I worry, it will always either jump over the number when comparing, or vice versa will go in cycles. In general, I've been scratching my head for a long time ... I would be very grateful if you could help me figure out what kind of malice is going on :)
ps: maybe these are some features of the file access mode that I don't know about?

#include "stdafx.h"
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <iostream>
using namespace std;

int main()
{
  FILE *f;
  int i, size, max, iMax, val, maxIndex, datVar;
  setlocale(LC_CTYPE, "Russian");
  srand(time(NULL));
  for (;;){
    cout << "Введите количество компонент: ";
    scanf("%d", &size);
    if (size>0) break;
    else cout << "Неправильное количество компонент. Повторите ввод: ";
  }
  f = fopen("file1.bin", "wb");
  for (i = 0; i < size; i++){ //запись в файл рандомных чисел
    datVar = rand() % 5;
    fwrite(&datVar, sizeof(int), 1, f);
    cout << datVar << " ";
  }
  
  cout << "\n";
  fclose(f);
  
  f = fopen("file1.bin", "r+b");
  fseek(f, 0, SEEK_SET);
  fread(&val, sizeof(int), 1, f); //считываем в файле int'ы
  max = val;
  while (fread(&val, sizeof(int), 1, f)!=0){
    if (val >= max) { 
      max = val; 
    }	
  }
  int nulVar = 0;
  
  int ft = 0;
  fseek(f, 0, SEEK_END);
  int sizeF = ftell(f);
  fseek(f, 0, SEEK_SET);
  while (fread(&val, sizeof(int), 1, f) > 0)
  {
    if (val == max)
    {
      fseek(f, ftell(f) - sizeof(int), SEEK_SET);
      fwrite(&nulVar, sizeof(int), 1, f);
      fflush(f);//ВОТ РЕШЕНИЕ
    }
    
    if (ftell(f) == sizeF) break;
  }
  fseek(f, 0, SEEK_SET);
  while (fread(&val, sizeof(int), 1, f) > 0)printf("%d ", val);
  fclose(f);
  cout << "\n";
  system("pause");
  return 1;
}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
E
Eugene Ignatik, 2014-06-10
@eignatik

The problem was solved by adding fflush(f); after fwrite. It's just that fwrite did not translate the cursor in the file, and because of this, problems began during subsequent manipulations with the cursor. And fflush(); immediately flushes the buffer to the file and moves the cursor. In general, everything turned out to be easier.

S
Sergey Zabodalov, 2014-06-10
@zabbius

i would do like this:

while (fread(&val, sizeof(int), 1, f) > 0) // EOF == -1
{
if (val == max)
{
ft= ftell(f);
fseek(f, ft-sizeof(int), SEEK_SET); // отмотали на int назад
fwrite(&nulVar, sizeof(int), 1, f); // записали - теперь мы там же, где до ифа
}

M
Misha Krinkin, 2014-06-10
@kmu1990

1. Religion does not allow you to indent?
2. The program works fine for me and I see no reason why it shouldn't. Try to check what fseek, ftell and fwrite return, in theory they can also fail.
3. If the code is in C, then write in C, why are you including C++ headers?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question