【发布时间】:2016-01-21 19:00:38
【问题描述】:
我为一个学校项目制作了这个程序。它运行得很好,只是我无法根据需要将数据写入文本文件。我得到的文件文本如下:
AJames y$¥ °› †løvúövÿÿÿÿ$ „þB-28 ®kt¼› €@ ° @ ¬þ ...tt¸?'tìþ¢it Mark $¥€@ €@ ° @ Àþ øþÌÿ 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();
任何帮助将不胜感激!
【问题讨论】:
-
这个
fflush(stdin);是未定义的行为。 -
使用
fprintf而不是fwrite。 -
将结构写入文件不会将其转换为文本,它只是写入结构的原始字节。您需要使用
fprintf()根据需要格式化每个字段。 -
哦,还有。您的代码非常混乱且难以阅读。您可能希望改进您的编码风格,以使您的程序更易于调试。
-
我会为每个选项制作一个单独的过程,以使代码更易于阅读。
标签: c structure text-files codeblocks file-handling