fread(&k, sizeof(k), 1, f);
if (k % 2 == 0 )
{
k *= 2;
// fayl ko'rsatkichini sizeof(int) bayt chapga surish
fseek(f, -sizeof(int), SEEK_CUR);
fwrite(&k, sizeof(int), 1, f);
// fayl ko'rsatkichini o'rnatish
fseek(f, ftell(f), SEEK_SET);
}
}
cout << "fayl elementlari\n";
rewind(f);
while (fread(&k, sizeof(k), 1, f))
cout << k << endl;
fclose(f);
return 0;
}
3 - misolni quyidagicha yechish ham mumkin.
#include
#include
using namespace std;
int main()
{
int n, k;
FILE *f;
f = fopen("binar", "wb+");
cout << "n="; cin >> n;
for (int i = 0; i < n; i++)
{
cin >> k;
fwrite(&k, sizeof(k), 1, f);
}
// fayl ko'rsatkichini fayl boshiga qo'yish
rewind(f);
while (!feof(f))
{
// fayl ko'rsatkichi o'rnini eslab qolish
int pos = ftell(f);
fread(&k, sizeof(k), 1, f);
if (k % 2 == 0 )
{
k *= 2;
// fayl ko'rsatkichini oldingi xolatiga o'rnatish
fseek(f, pos, SEEK_SET);
fwrite(&k, sizeof(int), 1, f);
// fayl ko'rsatkichi o'rnini sizeof(int) ga surish
pos += sizeof(int);
fseek(f, pos, SEEK_SET);
}
}
cout << "fayl elementlari\n";
rewind(f);
// fayl elementlarini chiqarish
while (fread(&k, sizeof(k), 1, f))
cout << k << endl;
fclose(f);
system (“pause”);
return 0;
}
3. Fayllar bilan ishlash bo’yicha amallar bajarish
Do'stlaringiz bilan baham: |