Тема: Робота з файлами.Допоможіть з записом
програма повинна вводити дані абітурієнта, та змінювати оцінки за екзамени.
#include <stdio.h>
#include <locale.h>
#include <stdlib.h>
//#include <ctype.h>
//#include <conio.h>
//#include <string.h>
struct Ab {
int ID;
char name[15];
char surname[20];
char middle_name[15];
double atestat;
int mark_math;
int mark_ukr;
int ID_specialyty;
};
int enterChoice( void );
void textFile( FILE *readPtr );
void updateRecord( FILE *fPtr );
void newRecord( FILE *fPtr );
void deleteRecord( FILE *fPtr );
int main(){
FILE *cfPtr;
int choice;
setlocale(LC_ALL,"Rus");
if ( ( cfPtr = fopen( "abiturient.dat", "ab+" ) ) == NULL ) {
printf( "Ôàéë íå ìîæëèâî âiäêðèòè.\n" );
}
else {
while ( ( choice = enterChoice() ) != 5 ) {
switch ( choice ) {
case 1: textFile( cfPtr ); break;
case 2: updateRecord( cfPtr ); break;
case 3: newRecord( cfPtr ); break;
case 4: deleteRecord( cfPtr ); break;
default:
printf( "Íåâiðíèé íîìåð\n" );break;
}
}
fclose( cfPtr );
}
return 0;
}
//--------------------------------------------------------------------
void textFile( FILE *readPtr )
{
FILE *writePtr;
struct Ab inf = { 0, "", "", "", 0.0, 0, 0, 0 };
if ( ( writePtr = fopen( "abiturient_list.txt", "rb+" ) ) == NULL ) {
printf( "Ôàéë íå áóëî çíàéäåíî.\n" );
}
else {
rewind( readPtr );
fprintf( writePtr, "\n%-5s%-10s%-10s%-15s%-15s%-10s%-10s%-5s\n",
"Íîìåð", "Iì'ÿ", "Ïðiçâèùå", "Ïî áàòüêîâi", "Áàë àòåñòàòà",
"Ìàòåìàòèêà", "Ìîâà", "Ñïåöiàëüíiñòü\n" );
while ( !feof( readPtr ) ) {
fread( &inf, sizeof( struct Ab ), 1, readPtr );
if ( inf.ID != 0 ) {
fprintf( writePtr, "\n%-5d%-10s%-10s%-15s%-10.2f%-10d%-10d%-5d\n",
inf.ID, inf.name, inf.surname, inf.middle_name ,inf.atestat ,
inf.mark_math ,inf.mark_ukr ,inf.ID_specialyty);
}
}
fclose( writePtr );
}
}
//-------------------------------------------------------------------
void updateRecord( FILE *fPtr )
{
int account;
int marks1=0, marks2=0;
struct Ab inf = { 0, "", "", "", 0.0, 0, 0, 0 };
printf( "Ââåäiòü íîìåð àêàóíòà äëÿ çàìiíè äàíèõ ( 1 - 100 ): " );
scanf( "%d", &account );
fseek( fPtr, ( account - 1 ) * sizeof( struct Ab ),
SEEK_SET );
fread( &inf, sizeof( struct Ab ), 1, fPtr );
if ( inf.ID == 0 ) {
printf( "Àêàóíò #%d íå ìຠiíôîðìàöi¿.\n", account );
}
else {
printf( "\n%-5d%-10s%-10s%-15s%-10.2f%-10d%-10d%-5d\n\n",
inf.ID, inf.name, inf.surname, inf.middle_name ,inf.atestat ,
inf.mark_math ,inf.mark_ukr ,inf.ID_specialyty);
printf( "Ââåäiòü îöiíêó çà åêçàìåí ç ìàòåìàòèêè: " );
scanf( "%d", &marks1 );
inf.mark_math = marks1;
printf( "Ââåäiòü îöiíêó çà åêçàìåí ç ìîâè: " );
scanf( "%d", &marks2 );
inf.mark_ukr = marks2;
printf( "\n%-5d%-10s%-10s%-15s%-10.2f%-10d%-10d%-5d\n\n",
inf.ID, inf.name, inf.surname, inf.middle_name ,inf.atestat ,
inf.mark_math ,inf.mark_ukr ,inf.ID_specialyty);
fseek( fPtr, ( account - 1 ) * sizeof( struct Ab ),
SEEK_SET );
fwrite( &inf, sizeof( struct Ab ), 1, fPtr );
}
}
//-------------------------------------------------------------------
void newRecord( FILE *fPtr )
{
struct Ab inf = { 0, "", "", "", 0.0, 0, 0, 0 };
int accountNum;
printf("\n1.Êîìï'þòåðíi íàóêè\n2.Çåìëåâïîðÿäêóâàííÿ\n3.Ëiñíèêè\n");
printf( "Ââåäiòü íîìåð àêàóíòà ( 1 - 100 ): " );
scanf( "%d", &accountNum );
fseek( fPtr, ( accountNum - 1 ) * sizeof( struct Ab ),
SEEK_SET );
fread( &inf, sizeof( struct Ab ), 1, fPtr );
if ( inf.ID != 0 ) {
printf( "Â ðÿäêó #%d âæå º iíôîðìàöiÿ.\n",inf.ID );
}
else {
printf( "Iì'ÿ, Ïðiçâèùå, Ïî áàòüêîâi, Áàë àòåcòàòà, Ìàòåìàòèêà, Ìîâà, Ñïåöiàëüíiñòü\n: " );
scanf( "%s%s%s%lf%d%d%d", &inf.name, &inf.surname, &inf.middle_name,
&inf.atestat, &inf.mark_math, &inf.mark_ukr, &inf.ID_specialyty);
inf.ID = accountNum;
fseek( fPtr, ( inf.ID - 1 ) *
sizeof( struct Ab ), SEEK_SET );
fwrite( &inf,
sizeof( struct Ab ), 1, fPtr );
}
getch();
}
//-------------------------------------------------------------------
void deleteRecord( FILE *fPtr )
{
struct Ab inf;
struct Ab blankInf = { 0, "", "", "", 0, 0, 0, 0 };
int accountNum;
printf( "Ââåäiòü íîìåð ðÿäêà ùîá âèäàëèòè ( 1 - 100 ): " );
scanf( "%d", &accountNum );
fseek( fPtr, ( accountNum - 1 ) * sizeof( struct Ab ),
SEEK_SET );
fread( &inf, sizeof( struct Ab ), 1, fPtr );
if ( inf.ID == 0 ) {
printf( "Àêàóíòà ¹%d íå iñíóº.\n", accountNum );
}
else {
fseek( fPtr, ( accountNum - 1 ) * sizeof( struct Ab ),
SEEK_SET );
fwrite( &blankInf,
sizeof( struct Ab ), 1, fPtr );
}
}
//-------------------------------------------------------------------
int enterChoice( void )
{
int menuChoice;
printf(" Ïðîãðàìà Àáiòóðiºíò\n");
printf( "\nÇðîáiòü âàø âèáið\n"
"1 - Çáåðåæåííÿ\n"
"2 - Äîäàòè äî àêàóíòó\n"
"3 - Äîäàòè íîâèé çàïèñ\n"
"4 - Âèäàëèòè çàïèñ\n"
"5 - Âèéòè\n: " );
scanf( "%d", &menuChoice );
return menuChoice;
}