C 丢失我的文件';在关闭文件后删除数据
我正试图制作一个保存学校学生记录的文件,除了一个之外,一切都很顺利,那就是在我关闭程序后,数据正在丢失。 有什么问题吗 节目从这里开始:C 丢失我的文件';在关闭文件后删除数据,c,C,我正试图制作一个保存学校学生记录的文件,除了一个之外,一切都很顺利,那就是在我关闭程序后,数据正在丢失。 有什么问题吗 节目从这里开始: #include "stdafx.h" #include <stdio.h> #include <conio.h> #include <iostream> #include <string.h> using namespace std; 函数原型和main()
#include "stdafx.h"
#include <stdio.h>
#include <conio.h>
#include <iostream>
#include <string.h>
using namespace std;
函数原型和main()
在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");
}
此函数使用随机访问添加新记录
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 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;
}
}
节目结束了 在这种情况下,我要做的第一件事就是 确保每个fopen()调用都有一个fclose()调用 因此,在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");
}
void创建文件()
{
文件*fp;
ogr_t bos_ogr={0,0,0,0,0.0,“,”};
if((fp=fopen(“kayit.dat”,“wb”))==NULL)
{
printf(“文件未打开”);
}
其他的
{//创建一个文件并用一个空的100学生记录初始化它。
对于(int i=0;i<100;i++)
{
fwrite&bos_ogr,sizeof(ogr_t),1,fp;
}//结束
//让我们关闭文件
fclose(fp);
}
系统(“cls”);
}
(1)请使用适当的缩进设置代码格式。这真的很难读。(2) 这是很多代码。请编辑您的帖子,欢迎来到stackoverflow。这对于获得有用的答案来说是太多的代码了,而且您甚至没有在一个地方给出一个可运行的程序。但是我注意到createfile
将清空文件的内容(如果存在)。这是故意的吗?在createfile
中,您在完成写入文件后忘记关闭文件。@alexis在main()函数中,我询问用户是创建文件还是继续创建文件(如果已经创建),因此文件将仅根据用户的要求创建,但如果您选择以“wb”的形式打开文件所有信息都将丢失问题是我正在运行程序,我选择在第一次运行时创建文件并输入一些记录,在第二次运行时,我选择不创建新文件,因为它已经创建,而是我想继续读取记录,但即使我不再创建文件,它也会丢失!你是天才@PaulR问题解决了多亏了一个笨蛋我读了100遍代码我都没注意到。
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;
}
}
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");
}