gpt4 book ai didi

c - 关闭文件后丢失文件数据

转载 作者:行者123 更新时间:2023-11-30 17:16:24 25 4
gpt4 key购买 nike

我正在尝试制作一个文件来保存学校学生的记录,除了一件事之外一切都很好,那就是在我关闭程序后数据丢失了。有什么问题吗?

程序从这里开始:

    #include "stdafx.h"
#include <stdio.h>
#include <conio.h>
#include <iostream>
#include <string.h>

using namespace std;

这是将用于处理记录的结构。

    /*a struct ogrenci and name it ogr_t with typedef*/
typedef struct ogrenci
{
int kyt_no, ogrno, vize, final;
float ort;
char ad[10];
char soyad[10];
}ogr_t;
/*End struct*/

void creatfile();//To create the file "Kayit.dat" if it was not created .

函数原型(prototype)和main()

    int menu();
void addrecord(FILE *ptr);
void updaterecord(FILE *ptr);
void deleterecord(FILE *ptr);
void listrecords(FILE *ptr);
void searchrecord(FILE *ptr);
double findavg(FILE *ptr);

int _tmain(int argc, _TCHAR* argv[])
{
FILE *fp;
int request;
int choice;
printf("1- Creat the File \"Kayit.dat\" if it was not created .\n\n"
"#NOTE: this choice will clear the data saved if the file was created earlier .\n\n"
"2- To continue if the file is already created .\n");
scanf("%d", &choice);
switch (choice)
{
case 1:creatfile();
case 2:
system("cls");
if ((fp = fopen("kayit.dat", "rb+")) == NULL)
{
printf("File was not opened.");
}
else
{
while ((request = menu()) != 6)
{
switch (request)
{
case 1:addrecord(fp); break;
case 2:updaterecord(fp); break;
case 3:deleterecord(fp); break;
case 4:listrecords(fp); break;
case 5:searchrecord(fp); break;
default:printf("undefined\n");break;
}
}
fclose(fp);
}break;
default:printf("Undefined\n");break;
}
return 0;
}

在 main() 中询问用户后,该函数创建文件 kayit.dat

    void creatfile()
{
FILE *fp;
ogr_t bos_ogr = { 0, 0, 0, 0, 0.0, "", "" };
if ((fp = fopen("kayit.dat", "wb")) == NULL)
{
printf("File was not opened");
}
else
{ //to create a file and initialize it with an empty 100 student record .
for (int i = 0; i < 100; i++)
{
fwrite(&bos_ogr, sizeof(ogr_t), 1, fp);
}//end for
}
system("cls");
}

这是用户在 main() 中显示的菜单。

    int menu(void)
{
int x;
printf("What do you want to do :\n\n"
"1= Add record .\n"
"2= Update record .\n"
"3= Delete Record .\n"
"4= list all the records .\n"
"5= Search for a specific record .\n"
"6= Quit .\n");
scanf("%d", &x);
system("cls");
return x;
}

此函数使用随机访问添加一条新记录。

    void addrecord(FILE *ptr)
{
ogr_t record = { 0, 0, 0, 0, 0.0, "", "" };
int accnum;
char c;
/*Enter a record number and search if is was already input*/
printf(" Enter a record number for the new student < 1 - 100 > .");
scanf("%d", &accnum);
fseek(ptr, (accnum - 1)*sizeof(ogr_t), SEEK_SET);
fread(&record, sizeof(ogr_t), 1, ptr);
if (record.kyt_no!=0)
{
printf("this record is already full .\n");
printf("press and key to continue...\n");
if (c = _getche())
{
system("cls");
}
}
else
{
printf("%-13s%-13s%-13s%-13s%-13s\n", "Name", "SureName", "StudentNum", "MidtermNote", "FinalNote");
fscanf(stdin, "%s%s%d%d%d", record.ad, record.soyad, &record.ogrno, &record.vize, &record.final);
record.kyt_no = accnum;
fseek(ptr, (record.kyt_no - 1)*sizeof(ogr_t), SEEK_SET);
fwrite(&record, sizeof(ogr_t), 1, ptr);
}
system("cls");
}

更新用户选择的记录。

    void updaterecord(FILE *ptr)
{
ogr_t record1 = { 0, 0, 0, 0, 0.0, "", "" };
int accnum;
char c;
printf(" Enter the student's record number that you want to update < 1 - 100 > .");
scanf("%d", &accnum);
fseek(ptr, (accnum - 1)*sizeof(ogr_t), SEEK_SET);
fread(&record1, sizeof(ogr_t), 1, ptr);
if (record1.kyt_no == 0)
{
printf("This record is empty\n");
printf("press and key to continue...\n");
if (c= _getche())
{
system("cls");
}
}
else
{
printf("Enter the new informations\n");
printf("%-13s%-13s%-13s%-13s%-13s\n", "Name", "SureName", "StudentNum", "MidtermNote", "FinalNote");
fscanf(stdin, "%s%s%d%d%d", record1.ad, record1.soyad, &record1.ogrno, &record1.vize, &record1.final);
record1.kyt_no = accnum;
fseek(ptr, (record1.kyt_no - 1)*sizeof(ogr_t), SEEK_SET);
fwrite(&record1, sizeof(ogr_t), 1, ptr);
system("cls");
}

}

检查用户选择的记录是否为空后,删除该记录。

    void deleterecord(FILE *ptr)
{
ogr_t bos_ogr = { 0, 0, 0, 0, 0.0, "", "" };
ogr_t record2;
int accnum;
char c;
printf("Enter the record for the student that you want delete < 1 - 100 >\n");
fscanf(stdin, "%d", &accnum);
fseek(ptr, (accnum - 1)*sizeof(ogr_t), SEEK_SET);
fread(&record2, sizeof(ogr_t), 1, ptr);
if (record2.kyt_no == 0)
{
printf("The record is already empty .\n");
printf("press and key to continue...\n");
if (c = _getche())
{
system("cls");
}
}
else
{
fseek(ptr, (accnum - 1)*sizeof(ogr_t), SEEK_SET);
fwrite(&bos_ogr, sizeof(ogr_t), 1, ptr);
system("cls");
}
}

列出文件中的所有记录。

void listrecords(FILE *ptr)
{
ogr_t record_prnt;
char c;
printf("%-13s%-13s%-13s%-13s%-13s%-13s\n", "RecordNo", "Name", "Surname", "StudentNum", "MidtermNote", "FinalNote");
rewind(ptr);
fread(&record_prnt, sizeof(ogr_t), 1, ptr);
while (!feof(ptr))
{
if (record_prnt.kyt_no == 0)
{
record_prnt = { 0, 0, 0, 0, 0.0, "", "" };
}
else
{
printf("%-13d%-13s%-13s%-13d%-13d%-13d\n",
record_prnt.kyt_no, record_prnt.ad,
record_prnt.soyad, record_prnt.ogrno,
record_prnt.vize, record_prnt.final);
}
fread(&record_prnt, sizeof(ogr_t), 1, ptr);
}
printf("press any key to contiue ...\n");
if (c = _getche())
{
system("cls");
}
}

按照用户选择的条件搜索特定记录。

void searchrecord(FILE *ptr)
{
int request;
int recnum;
char name[20];
ogr_t ogr;
int mark;
printf("Enter by what criteria do you want to search .\n\n"
"1- By record num.\n"
"2- By Name.\n"
"3- By Grade.\n");
scanf("%d", &request);
system("cls");
switch (request)
{
case 1:
printf("Enter the record number .");
scanf("%d", &recnum);
fseek(ptr, (recnum - 1)*sizeof(ogr_t), SEEK_SET);
fread(&ogr, sizeof(ogr_t), 1, ptr);
printf("%-13s%-13s%-13s%-13s%-13s%-13s\n", "RecordNumber", "Name", "Surename", "StudentNum", "MidtermNote", "FinalNote");
printf("%-13d%-13s%-13s%-13d%-13d%-13d\n", ogr.kyt_no, ogr.ad, ogr.soyad, ogr.ogrno, ogr.vize, ogr.final);
break;
case 2:
printf("Enter the name of the student .\n ");
scanf("%s",name);
rewind(ptr);
fread(&ogr, sizeof(ogr_t), 1, ptr);
while (!feof(ptr))
{
if (strcmp(name, ogr.ad) == 0)
{
printf("%-13s%-13s%-13s%-13s%-13s%-13s\n", "RecordNumber", "Name", "Surename", "StudentNum", "MidtermNote", "FinalNote");
printf("%-13d%-13s%-13s%-13d%-13d%-13d\n", ogr.kyt_no, ogr.ad, ogr.soyad, ogr.ogrno, ogr.vize, ogr.final);
}
fread(&ogr, sizeof(ogr_t), 1, ptr);
}
break;
case 3:
printf("Enter the Mark the you want to search for.\n ");
scanf("%d",&mark);
printf("The student that had higher than the mark that you've enterd are \n\n");
rewind(ptr);
fread(&ogr, sizeof(ogr_t), 1, ptr);
while (!feof(ptr))
{
if (ogr.kyt_no!=0)
{
ogr.ort = ((ogr.final) + (ogr.vize)) / 2;
if ((ogr.ort)>=mark)//print the records that have a higher averager than the mark.
{
printf("%-13s%-13s%-13s%-13s%-13s%-13s\n", "RecordNumber", "Name", "Surename", "StudentNum", "MidtermNote", "FinalNote");
printf("%-13d%-13s%-13s%-13d%-13d%-13d\n", ogr.kyt_no, ogr.ad, ogr.soyad, ogr.ogrno, ogr.vize, ogr.final);
}
}
fread(&ogr, sizeof(ogr_t), 1, ptr);
}
break;
default:printf("Undefined choice.\n");
break;
}
}

程序结束。

最佳答案

在这种情况下,我要做的第一件事是......

Make sure for every fopen() call there is one fclose() call.

因此,在 createfile() 中,您以写入模式打开文件,并且不要关闭该文件。使用 rb+ 模式再次打开该文件,从而导致问题。

写入后,您应该在 createfile() 中关闭文件,这样您的代码就会像这样

void creatfile()
{
FILE *fp;
ogr_t bos_ogr = { 0, 0, 0, 0, 0.0, "", "" };
if ((fp = fopen("kayit.dat", "wb")) == NULL)
{
printf("File was not opened");
}
else
{ //to create a file and initialize it with an empty 100 student record .
for (int i = 0; i < 100; i++)
{
fwrite(&bos_ogr, sizeof(ogr_t), 1, fp);
}//end for

//Lets close the file
fclose(fp);
}
system("cls");
}

关于c - 关闭文件后丢失文件数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29648203/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com