gpt4 book ai didi

c - 我无法用 C 语言在文本文件中写入结构

转载 作者:行者123 更新时间:2023-11-30 18:38:07 25 4
gpt4 key购买 nike

我为一个学校项目制作了这个程序。它运行得很好,只是我无法按照我的意愿将数据写入文本文件。我得到的文件文本如下:

AJames y$¥ °› †løvúövÿÿÿÿ$ „þB-28 ®kt¼› €@ ° @ Øþ ...tt¸?tìþ¢标记 $¥€@ € @ ° @ Àþ øþÌÿ IT tÕÑþÿÿÿ áD

我的 C 语言并不强,因此,即使浏览了在线论坛,我也无法找到具体的答案。以下是代码:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<process.h>
#include<string.h>

int main()
{
FILE *fp, *ft;
char tmp, choice;

struct fee
{ float r_no;
char name[50];
char add[50];
char f_name[40];
char stream[10];
float d_fee;
}f;
long int amount;
int flag;
long int total = 2000;
long int pending=0;
long int size;
char stream1[40];
float r_no1;

fp=fopen("FEES.txt","w");
if(fp==NULL)
{
puts("Cannot open file");
exit(1);
}

size = sizeof(f);
while(1)
{
system("cls");

printf("\n\t\tWelcome to Main Menu!!");

printf("\n\t\t1. Student Detail");

printf("\n\t\t2. Display Record");

printf("\n\t\t3. Deposit Fee");

printf("\n\t\t4. Pending Fee");

printf("\n\t\t5. Delete Record");

printf("\n\t\t0. Exit");

printf("\n\n\tYour choice : ");
fflush(stdin);
choice = getche();
switch(choice)
{
case '1':
fseek(fp,10,SEEK_SET);
tmp = 'Y';
while((tmp == 'Y') || (tmp == 'y'))
{
printf("\n\tEnter Roll No: ");
scanf("%f", &f.r_no);
printf("\n\tEnter Name: ");
scanf(" %s", f.name);
printf("\n\tEnter Address: ");
scanf(" %s", f.add);
printf("\n\tEnter Father Name: ");
scanf(" %s", f.f_name);
printf("\n\tEnter Deposited Fee: ");
scanf("%f", &f.d_fee);
printf("\n\tEnter Stream: ");
scanf(" %s", f.stream);
fwrite(&f, size, 1, fp);
printf("\nCreate Another Student Record(Y/N)");
tmp = getche();
}
break;

case '2':
system("cls");
rewind(fp);
printf("Roll No: \tName: \tAddress: \tFather's Name: \tStream: \tDeposited Fee: \tTotal Amount: ");
while(fread(&f, size, 1, fp))
printf("\n%d \t%s \t%s \t%s \t%s \t%f \t%ld", f.r_no, f.name, f.add, f.f_name, f.stream, f.d_fee, total);
getch();
break;

case '3':
tmp='Y';
while(tmp=='Y' || tmp=='y')
{
printf("\nEnter Roll Number to Check Balance");
scanf("%d", &r_no1);
printf("\nEnter the Stream");
scanf("%s", &stream1);
rewind(fp);
flag=0;
while(fread(&f, size, 1, fp))
{
if((f.r_no==r_no1)&&(strcmp(f.stream,stream1)))
{
if(f.d_fee<=total)
{
flag=1;
printf("\nEnter amount to Deposit");
scanf("%ld", &amount);
f.d_fee = f.d_fee + amount;
fseek(fp, -size, SEEK_CUR);
fwrite(&f, size, 1, fp);
break;
}
}
}
if(flag==0)
{
printf("Sorry! Invalid Roll No.");
getch();
}
printf("Deposit More Amount!");
fflush(stdin);
tmp=getche();
}
break;
case '4':
tmp='Y';
while(tmp=='Y'||tmp=='y')
{
printf("\nEnter Roll Number to check Pending Amount");
scanf("%d", &r_no1);
printf("\nEnter the Stream");
scanf("%s", &stream1);
rewind(fp);
flag=0;
while(fread(&f, size, 1, fp))
{

if((f.r_no==r_no1)&&(strcmp(f.stream,stream1)))
{flag=1;
pending=total-f.d_fee;
printf("\nYour pending amount is: %ld", pending);

getch();
}
}
if(flag==0)
{
printf("Sorry! Invalid Roll Number");
getch();
}

fflush(stdin);
tmp=getche();
}
break;

case '5':
tmp='Y';
while(tmp=='Y'||tmp=='y')
{
printf("\nEnter Roll Number to delete");
scanf("%d",&r_no1);
ft = fopen("TEMP.txt","w");
rewind(fp);
while(fread(&f, size, 1, fp))
{
if(f.r_no!=r_no1)
fwrite(&f, 1, size, ft);
}
fclose(fp);
fclose(ft);
remove("FEES.txt");
rename("TEMP.txt", "FEES.txt");
fp=fopen("FEES.txt", "r");
printf("Delete another record(Y/N)");
fflush(stdin);
tmp = getche();
}
break;

case '0':
fclose(fp);
printf("\n\t\tProgram Terminated!!");
exit(0);
}
}}

好的,按照建议,我使用了 fprintf() 而不是 fwrite(),现在我在程序中得到了这种强制。这是我所做的更改:案例“1”:

        tmp = 'Y';
while((tmp == 'Y') || (tmp == 'y'))
{
printf("\n\tEnter Roll No: ");
scanf("%f", &f.r_no);
printf("\n\tEnter Name: ");
scanf(" %s", f.name);
printf("\n\tEnter Address: ");
scanf(" %s", f.add);
printf("\n\tEnter Father Name: ");
scanf(" %s", f.f_name);
printf("\n\tEnter Deposited Fee: ");
scanf("%f", &f.d_fee);
printf("\n\tEnter Stream: ");
scanf(" %s", f.stream);
fprintf(fp, "%d %s %s %s %f %s \n", f.r_no, f.name, f.add, f.f_name, f.d_fee, f.stream);
printf("\nCreate Another Student Record(Y/N)");
tmp = getche();

如有任何帮助,我们将不胜感激!

最佳答案

您编写的不是一个文本文件,而是一个二进制文件,您无法使用纯文本编辑器检查它的内容。如果需要,您可以使用十六进制编辑器,其中一些编辑器甚至可以接受定义一个结构来逐条记录地验证文件记录。

或者您需要编写一个读取函数,将二进制数据直接读取到结构的实例中。然后根据其类型打印结构的每个成员。

请注意,"w" 模式适用于文本文件,但您应该使用 "wb" 来代替。

关于c - 我无法用 C 语言在文本文件中写入结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34932155/

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