gpt4 book ai didi

c - 我做错了什么?下面的代码不会将结构化数据的最后一条记录从 .dat 文件复制到 C 中的另一个 .dat 文件

转载 作者:行者123 更新时间:2023-11-30 21:12:07 26 4
gpt4 key购买 nike

#include<conio.h>

#include<stdio.h>

struct stud

{
int rollno;
char name[10];
char add[10];
};

void main()
{
struct stud st;

FILE *fp,*fpp;

char another='y',ch;

int a,choice,i;

printf("***Press 1 to Add record/Create database***\n");

scanf("%d",&a);

if(a==1) /*recording data begins here*/
{
fflush(stdin);
printf("ADD or CREATE NEW?(a/c): "); /*prompt message to add new record to existing database or to create new database*/
scanf("%c",&ch);

if(ch=='a') /*appending file*/
{
add:

ch='a';

fp=fopen("Studm.dat","ab");

while(another=='y' || another=='Y')
{
fflush(stdin);

printf("Enter RollNo. Name & Address\n");

scanf("%d%s%s",&st.rollno,st.name,&st.add);

fwrite(&st,sizeof(st),1,fp);

fflush(stdin);

printf("\nAdd another record?(y/n): "); /*prompt message to add another record*/
scanf("%c",&another);

}

}

if(ch=='c') /*to create new data file*/
{

fp=fopen("Studm.dat","wb");

printf("Enter RollNo. Name & Address\n");

scanf("%d%s%s",&st.rollno,st.name,&st.add);

fwrite(&st,sizeof(st),1,fp);

fflush(stdin);

printf("\nAdd another record?(y/n): ");

scanf("%c",&another);

fclose(fp);

if(another=='y' || another=='Y')
{

goto add; /*go to the append file block above*/
}


}

fclose(fp);

}

每当我在文本编辑器中打开源文件时,给出的所有名称(字符数据)都是可见的,这意味着存储和记录 block 工作正常。

    fflush(stdin);

printf("\n\nCopying data from studm.dat to stud1.data\n\n");

fp=fopen("Studm.dat","rb");

fpp=fopen("Stud1.dat","wb");

while(fread(&st,sizeof(st),1,fp)==1) /*loop block to copy data*/
{
fflush(stdin); /*tried every place for flushing the buffer, before and after fwrite with in the loop*/

fwrite(&st,sizeof(st),1,fpp);

}
fclose(fp);

fclose(fpp);

就像源文件一样,我在执行整个程序代码后在文本编辑器中打开目标文件,直到倒数第二条记录都在那里。

    printf("\n\nCopying done now reading data from file Stud1.dat\n\n");

fpp=fopen("Stud1.dat","rb");

while(fread(&st,sizeof(st),1,fp)==1)
{

printf("%d %s %s\n",st.rollno,st.name,st.add);

}

fclose(fpp);

getch();
}

我认为问题出在复制 block 上,但我不知道这是否与缓冲区有关,或者我编码错误或存在错误。

最佳答案

我做了一些更改以避免复杂性。 看在上帝的份上,不要在代码中使用 goto 标签来替换循环。

对于多次编辑,我们深表歉意。这是最终的工作代码。只需编译并执行即可。

#include<stdio.h>

struct stud {
int rollno;
char name[100];
char add[100];
};

void main() {
struct stud st;
FILE *fp, *fpp;
int a, i, i_ret_val, another;
printf("***Press 1 to Add record/Create database***\n");
scanf("%d", &a);
fflush(stdin);
if (a == 1) /*recording data begins here*/
{
/*Since you are using the same file name throughout two cases are not
* required for adding and creating. Can achieve create in append mode fopen*/
fp = fopen("Studm.dat", "ab");
do {
fflush(stdin);
printf("Enter RollNo. Name & Address\n");
scanf("%d%s%s", &st.rollno, &st.name, &st.add);
printf("Fwrite returned %d\n", fwrite(&st, sizeof(st), 1, fp));
printf("\nAdd another record?1 for yes: \n"); /*prompt message to add another record*/
scanf("%d", &another);
} while (another == 1);
fclose(fp);
}
printf("\nNow Copying the file.\n");
fp = fopen("Studm.dat", "rb");
fpp = fopen("Stud1.dat", "wb");
i_ret_val = fread(&st, sizeof(st), 1, fp);
while (i_ret_val == 1) {
i_ret_val = fwrite(&st, sizeof(st), 1, fpp);
i_ret_val = fread(&st, sizeof(st), 1, fp);
}
fclose(fp);
fclose(fpp);
printf("\n\nCopying done. Now reading data from file Stud1.dat\n\n");
fpp = fopen("Stud1.dat", "rb");
i_ret_val = fread(&st, sizeof(st), 1, fpp);
printf("\nRetrieved from database\n");
while (i_ret_val == 1) {
printf("Roll No: %d, Name: %s, Address:%s\n", st.rollno, st.name,
st.add);
i_ret_val = fread(&st, sizeof(st), 1, fpp);
}
fclose(fpp);
}

关于c - 我做错了什么?下面的代码不会将结构化数据的最后一条记录从 .dat 文件复制到 C 中的另一个 .dat 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13162505/

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