- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我为一个学校项目制作了这个程序。它运行得很好,只是我无法按照我的意愿将数据写入文本文件。我得到的文件文本如下:
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/
至少在某些 ML 系列语言中,您可以定义可以执行模式匹配的记录,例如http://learnyouahaskell.com/making-our-own-types-and-typeclasses -
这可能是其他人已经看到的一个问题,但我正在尝试寻找一种专为(或支持)并发编程而设计的语言,该语言可以在 .net 平台上运行。 我一直在 erlang 中进行辅助开发,以了解该语言,并且喜欢建立一个稳
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be
我正在寻找一种进程间通信工具,可以在相同或不同系统上运行的语言和/或环境之间使用。例如,它应该允许在 Java、C# 和/或 C++ 组件之间发送信号,并且还应该支持某种排队机制。唯一明显与环境和语言
我有一些以不同语言返回的文本。现在,客户端返回的文本格式为(en-us,又名美国英语): Stuff here to keep. -- Delete Here -- all of this below
问题:我希望在 R 中找到类似 findInterval 的函数,它为输入提供一个标量和一个表示区间起点的向量,并返回标量落入的区间的索引。例如在 R 中: findInterval(x = 2.6,
我是安卓新手。我正在尝试进行简单的登录 Activity ,但当我单击“登录”按钮时出现运行时错误。我认为我没有正确获取数据。我已经检查过,SQLite 中有一个与该 PK 相对应的数据。 日志猫。
大家好,感谢您帮助我。 我用 C# 制作了这个计算器,但遇到了一个问题。 当我添加像 5+5+5 这样的东西时,它给了我正确的结果,但是当我想减去两个以上的数字并且还想除或乘以两个以上的数字时,我没有
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 4 年前。 Improve th
这就是我所拥有的 #include #include void print(int a[], int size); void sort (int a[], int size); v
你好,我正在寻找我哪里做错了? #include #include int main(int argc, char *argv[]) { int account_on_the_ban
嘿,当我开始向数组输入数据时,我的代码崩溃了。该程序应该将数字读入数组,然后将新数字插入数组中,最后按升序排列所有内容。我不确定它出了什么问题。有人有建议吗? 这是我的代码 #include #in
我已经盯着这个问题好几个星期了,但我一无所获!它不起作用,我知道那么多,但我不知道为什么或出了什么问题。我确实知道开发人员针对我突出显示的行吐出了“错误:预期表达式”,但这实际上只是冰山一角。如果有人
我正在编写一个点对点聊天程序。在此程序中,客户端和服务器功能写入一个唯一的文件中。首先我想问一下我程序中的机制是否正确? I fork() two processes, one for client
基本上我需要找到一种方法来发现段落是否以句点 (.) 结束。 此时我已经可以计算给定文本的段落数,但我没有想出任何东西来检查它是否在句点内结束。 任何帮助都会帮助我,谢谢 char ch; FI
我的函数 save_words 接收 Armazena 和大小。 Armazena 是一个包含段落的动态数组,size 是数组的大小。在这个函数中,我想将单词放入其他称为单词的动态数组中。当我运行它时
我有一个结构 struct Human { char *name; struct location *location; int
我正在尝试缩进以下代码的字符串输出,但由于某种原因,我的变量不断从文件中提取,并且具有不同长度的噪声或空间(我不确定)。 这是我的代码: #include #include int main (v
我想让用户选择一个选项。所以我声明了一个名为 Choice 的变量,我希望它输入一个只能是 'M' 的 char 、'C'、'O' 或 'P'。 这是我的代码: char Choice; printf
我正在寻找一种解决方案,将定义和变量的值连接到数组中。我已经尝试过像这样使用 memcpy 但它不起作用: #define ADDRESS {0x00, 0x00, 0x00, 0x00, 0x0
我是一名优秀的程序员,十分优秀!