- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是这个网站的新手,并且认为尝试询问是否有人知道此代码可能出现的问题可能是个好主意,因为它找不到已存在的文件。现在,我尝试更改 "wb"
和 "rb"
因为我没有想法(笑),同时确保每一行都不会变得有趣,因为我没有收到任何错误。
当我运行这个程序时,我添加一个具有姓名和号码的人,然后我希望能够以两种不同的方式(姓名和号码)搜索该人,在本例中是使用号码。我得到的结果始终是消息“错误”,这基本上意味着“找不到文件”。
如果有些内容不清楚或者我遗漏了某些内容,请告诉我,我会尽力确保其正确完成并遵守页面规则。谢谢!
最佳答案
语法和逻辑上的错误太多,无法一一提及。建议您学习使用调试器,但以下代码确定了几个问题。查找内联评论,并遵循建议:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct emp
{
char name[1000];
char lastname[1000];
char number[1000];
char gender[1000];
char program[1000];
char age[1000];
char email[1000];
int no;
};
void search();
void save();
void load();
void add();
void modify();
void delete();
char mygetch();
char fname[] = { "mydb.dat" };
int main()
{
int ch;
while (1)
{
printf("1. add\n");
printf("2. Modify\n");
printf("3. delete\n");
printf("4. Search\n");
printf("5. load\n");
printf("6. Save\n");
printf("7. Exit\n");
printf("Enter one number:\n");
scanf("%d", &ch);
switch (ch)
{
case 1: add();
break;
case 2: modify();
break;
case 3: delete();
break;
case 4: search();
break;
case 5: load();
break;
case 6: save();
break;
case 7: exit(0); /// suggest changing this to "return 0;"
}
mygetch();
}
return 0; /// Warning: will never be executed.
}
char mygetch()
{
char YN;
char f1;
scanf("%c", &f1);
scanf("%c", &YN);
return (YN);
}
void add() {
struct emp f;
FILE *text;
text = fopen(fname, "ab"); // should test "text" for NULL. If NULL, exit with error
printf("Enter name:\n");
scanf("%s", &f.name); /// when working with strings, "&" is not needed (same for all remaining
printf("Enter last name:\n");
scanf("%s", &f.lastname);
printf("Enter age:\n");
scanf("%s", &f.age);
printf("Enter gender:\n");
scanf("%s", &f.gender);
printf("Enter email:\n");
scanf("%s", &f.email);
printf("Enter program:\n");
scanf("%s", &f.program);
printf("Enter personal number:\n");
scanf("%s", &f.number);
fwrite(&f, sizeof(f), 1, text); /// also incorrect arguments, need several calls, one for each string to write:
/// eg: fwrite(f.name, sizeof(f.name), 1, text);
fclose(text);
}
void save()
{
struct emp f1;
FILE *text, *File1;
char YN[10], name[51];
printf("Want to make a copy of the file?\n");
scanf("%s", YN);
if (strcmp(YN, "Y") == 0)
{
printf("Enter the new file name that should be saved:\n");
scanf("%s", name);
text = fopen(name, "wb");
File1 = fopen(fname, "rb");
while (1)
{
fread(&f1, sizeof(f1), 1, File1);
if (File1)
{
break;
}
fwrite(&f1, sizeof(f1), 1, text); /// also incorrect arguments, need several calls, one for each string to write:
/// eg: fwrite(f.name, sizeof(f.name), 1, text);
break;
}
fclose(text);
fclose(File1);
remove(fname);
strcpy(fname, name);
}
else
{
remove(fname);
}
}
void modify()
{
FILE *text, *File1;
struct emp f1, f; /// warning: unused variable f1; (remove it from this function only)
int no, found = 0, count = 0; /// warning: unused variable count; (remove it from this function only)
text = fopen(fname, "rb");
File1 = fopen("data.dat", "wb");
printf("Enter the student No. you want to modify:");
scanf("%d", &no);
while (1)
{
fread(&f, sizeof(f), 1, File1); //invalid argument. Use fread(f.number, sizeof(f.number)...
if (feof(text))
{
break;
}
if (f.no == no)
{
found = 1;
printf("Enter No:\n");
scanf("%s", f.no);
printf("Enter name:\n");
scanf("%s", f.name);
printf("Enter last name:\n");
scanf("%s", f.lastname);
printf("Enter age:\n");
scanf("%s", f.age);
printf("Enter gender:\n");
scanf("%s", f.gender);
printf("Enter email:\n");
scanf("%s", f.email);
printf("Enter program:\n");
scanf("%s", f.program);
printf("Enter personal number:\n");
scanf("%s", f.number);
fwrite(&f, sizeof(f), 1, File1); /// also incorrect arguments, need several calls, one for each string to write:
/// eg: fwrite(f.name, sizeof(f.name), 1, text);
}
else
{
fwrite(&f, sizeof(f), 1, File1); /// also incorrect arguments, need several calls, one for each string to write:
/// eg: fwrite(f.name, sizeof(f.name), 1, text);
}
}
fclose(text);
fclose(File1);
if (found == 0)
{
printf("Sorry No Record Found\n\n");
}
else
{
text = fopen(fname, "wb");
File1 = fopen("data.dat", "rb");
while (1)
{
fread(&f, sizeof(f), 1, File1);
if (feof(File1))
{
break;
}
fwrite(&f, sizeof(f), 1, text); /// also incorrect arguments, need several calls, one for each string to write:
/// eg: fwrite(f.name, sizeof(f.name), 1, text);
}
fclose(text);
fclose(File1);
}
void load()
{
char sentence[1000], filename[100], ch;
FILE *text, *Newfile;
printf("Enter a new file name with .dat in the end:\n");
scanf("%s", filename);
text = fopen(filename, "w");
if (text == NULL)
{
printf("Error!");
exit(1);
}
printf("Enter a sentence that should overwrite the existing information:\n");
scanf("%s", &sentence);
printf("%s\n", sentence);
fclose(text);
text = fopen(filename, "r");
printf("Enter the file name where all the information will be overwritten by the last sentence:\n");
scanf("%s", filename);
Newfile = fopen(filename, "w");
while (1) {
ch = fgetc(text); /// Warning: implicit conversion loses integer precision: "int" to "char"
/// fix by changing "ch" to type `int`
if (ch == EOF)
break;
else
putc(ch, Newfile);
}
printf("Overwritten has been successfully done!\n");
system("pause");
fclose(text);
fclose(Newfile);
exit(0);
}
void search()
{
FILE *text, *File1;
struct emp f1, f;
int count = 0, found = 0, x; /// warning: unused variable count; (remove it from this function only)
text = fopen(fname, "rb");
File1 = fopen("data.dat", "wb");
printf("Choose the way to search for a student:\n");
printf(" 1. Personal number\n 2. Name\n 3. Program\n 4. Statistics\n");
scanf("%d", &x);
if (x == 1)
{
char number[1000];
printf("Enter the personal number that you want to modify:\n");
scanf("%s", &number); // string, do not use "&"
while (1)
{
fread(&f1, sizeof(f1), 1, text); /// should always test and use return value for functions that provide it
/// in this case, contains the number of elements successfully read.
/// If an error occurs, fread returns 0 and sets errno to a nonzero value.
/// This would remove need for feof test below.
if (feof(text))
{
break; // remove execution flow from the loop
}
if (f1.number == number)
{
found = 1;
printf("Enter students personal number:\n");
scanf("%s", &f.number); // string, do not use "&"
printf("Enter student Name:\n");
scanf("%s", f.name);
printf("Enter last name:\n");
scanf("%s", f.lastname);
printf("Enter student age:\n");
scanf("%s", f.age);
printf("Enter program:\n");
scanf("%s", f.program);
printf("Enter student No.:\n");
scanf("%s", f.number);
printf("Enter email:\n");
scanf("%s", f.email);
printf("Enter gender:\n");
scanf("%s", f.gender);
fwrite(&f1, sizeof(f1), 1, text); /// also incorrect arguments, need several calls, one for each string to write:
/// eg: fwrite(f.name, sizeof(f.name), 1, text);
break;
}
else
{
fwrite(&f1, sizeof(f1), 1, File1); /// also incorrect arguments, need several calls, one for each string to write:
/// eg: fwrite(f.name, sizeof(f.name), 1, text);
break;
}
break; ///Warning: will never be executed. remove it
}
if (found == 0)
{
printf("File can not be found.\n");
}
else
{
text = fopen(fname, "w");
File1 = fopen("data.dat", "r");
while (1)
{
fread(&f1, sizeof(f1), 1, text);
fflush(stdin);
if (File1) {
break;
}
fwrite(&f1, sizeof(f1), 1, File1); /// also incorrect arguments, need several calls, one for each string to write:
/// eg: fwrite(f.name, sizeof(f.name), 1, text);
break;
}
fclose(File1);
fclose(text);
}
}
if (x == 2)
{
int name;
printf("Enter the student name that you want to modify:\n");
scanf("%d", &name); /// need "%s" format specifier
while (1)
{
fread(&f1, sizeof(f1), 1, text);
if (feof(text))
{
break;
}
if (f1.name == name)
{
found = 1;
printf("Enter student Name:\n");
scanf("%s", &f.name);
printf("Enter last name:\n");
scanf("%s", f.lastname);
printf("Enter student age:");
scanf("%s", f1.age);
printf("Enter program:");
scanf("%s", f1.program);
printf("Enter personal number:");
scanf("%s", f1.number);
printf("Enter email:");
scanf("%s", f1.email);
printf("Enter gender:");
scanf("%s", f1.gender);
fwrite(&f1, sizeof(f1), 1, text); // see other comments (for all fwrite calls)
break;
}
else {
fwrite(&f1, sizeof(f1), 1, File1);
break;
}
break; ///Warning: will never be executed. remove it
}
if (found == 0) {
printf("File can not be found.\n");
}
else {
text = fopen(fname, "wb");
File1 = fopen("data.dat", "rb");
while (1)
{
fread(&f1, sizeof(f1), 1, text);
fflush(stdin);
if (File1) {
break;
}
fwrite(&f1, sizeof(f1), 1, File1);
break;
}
fclose(File1);
fclose(text);
}
}
if (x == 3)
{
int program;
printf("Enter the student program that you want to modify:\n");
scanf("%d", &program);
while (1)
{
fread(&f1, sizeof(f1), 1, text);
if (feof(text))
{
break;
}
if (f1.program == program)
{
found = 1;
printf("Enter student number:\n");
scanf("%d", &f1.no);
fflush(stdin);
printf("Enter student Name:\n");
scanf("%s", f1.name);
printf("Enter last name:\n");
scanf("%s", f.lastname);
printf("Enter student age:");
scanf("%s", &f1.age);
printf("Enter program:");
scanf("%s", &f1.program);
printf("Enter personal number:");
scanf("%s", &f1.number);
printf("Enter email:");
scanf("%s", &f1.email);
printf("Enter gender:");
scanf("%s", &f1.gender);
fwrite(&f1, sizeof(f1), 1, text);
break;
}
else {
fwrite(&f1, sizeof(f1), 1, File1);
break;
}
break; ///Warning: will never be executed. remove it
}
if (found == 0) {
printf("File can not be found.\n");
}
else {
text = fopen(fname, "wb");
File1 = fopen("data.dat", "rb");
while (1)
{
fread(&f1, sizeof(f1), 1, text);
fflush(stdin);
if (File1) {
break;
}
fwrite(&f1, sizeof(f1), 1, File1);
break;
}
fclose(File1);
fclose(text);
}
}
if (x == 4)
{
text = fopen(fname, "rb");
printf("Showing all students statistics\n");
while (1)
{
fread(&f1, sizeof(f1), 1, text);
if (feof(text))
{
break;
}
printf("Student number:\n");
printf("%d\n", &f.no);
printf("Student name is:\n");
printf("%s\n", f.name);
printf("Student last name is:\n");
printf("%s\n", f.lastname);
printf("Student age is:\n");
printf("%s\n", f.age);
printf("Student personal number is:\n");
printf("%s\n", f.number);
printf("Student program is:\n");
printf("%s\n", f.program);
printf("Student gender is:\n");
printf("%s\n", f.gender);
printf("Student email is:\n");
printf("%s\n", f.email);
}
fclose(text);
}
fclose(File1);
fclose(text);
}
void delete()
{
FILE *text, *File1;
struct emp f1, f; /// Warning: unused variable "f". Remove it
int no, count = 0, found = 0; /// Warning: unused variable "count". Remove it
File1 = fopen("data.dat", "wb");
text = fopen(fname, "rb");
printf("Enter the student number that should be deleted:\n");
scanf("%d", &no);
while (1)
{
fread(&f1, sizeof(f1), 1, text);
if (feof(text))
{
break;
}
if (f1.no == no)
{
found = 1;
}
else
{
fwrite(&f1, sizeof(f1), 1, File1);
}
printf("File has been deleted\n");
break;
}
fclose(text);
fclose(File1);
if (found == 0)
{
printf("File not found.\n");
}
else
{
text = fopen(fname, "wb");
File1 = fopen("data.dat", "rb");
while (1)
{
fread(&f1, sizeof(f1), 1, File1);
if (feof(File1))
{
break;
}
fwrite(&f1, sizeof(f1), 1, File1);
break;
}
}
fclose(text);
fclose(File1);
}
关于c - 搜索文件被程序拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54149779/
我在我的应用程序中使用 Hibernate Search。其中一个子集合被映射为 IndexedEmbedded。子对象有两个字段,一个是 id,另一个是日期(使用日期分辨率到毫秒)。当我搜索 id=
The App Engine Search API有一个 GeoPoint 字段。可以用它来进行半径搜索吗?例如,给定一个 GeoPoint,查找位于特定半径内的所有文档。 截至目前,它看起来像 Ge
客户对我正在做的员工管理项目提出了这个新要求,以允许他们的用户进行自定义 bool 搜索。 基本上允许他们使用:AND、OR、NOT、括号和引号。 实现它的最佳方法是什么?我检查了 mysql,它们使
很想知道哪个更快 - 如果我有一个包含 25000 个键值对的数组和一个包含相同信息的 MySQL 数据库,搜索哪个会更快? 非常感谢大家! 最佳答案 回答这个问题的最好方法是执行基准测试。 关于ph
我喜欢 smartcase,也喜欢 * 和 # 搜索命令。但我更希望 * 和 # 搜索命令区分大小写,而/和 ?搜索命令遵循 smartcase 启发式。 是否有隐藏在某个地方我还没有找到的设置?我宁
我有以下 Marklogic 查询,当在查询控制台中运行时,它允许我检索具有管理员权限的系统用户: xquery version "1.0-ml"; import schema namespace b
我希望当您搜索例如“A”时,所有以“A”开头的全名都会出现。因此,如果名为“Andreas blabla”的用户将显示 我现在有这个: $query = "SELECT full_name, id,
我想在我的网站上添加对人名的搜索。好友列表已经显示在页面上。 我喜欢 Facebook 这样做的方式,您开始输入姓名,Facebook 只会显示与查询匹配的好友。 http://cl.ly/2t2V0
您好,我在我的网站上进行搜索时遇到此错误。 Fatal error: Uncaught Error: Call to undefined function mysql_connect() in /ho
声明( 叠甲 ):鄙人水平有限,本文为作者的学习总结,仅供参考。 1. 搜索介绍 搜索算法包括深度优先搜索(DFS)和广度优先搜索(BFS)这两种,从起点开始,逐渐扩大
我正在为用户列表使用 FuturBuilder。我通过 futur: fetchpost() 通过 API 获取用户。在专栏的开头,我实现了一个搜索栏。那么我该如何实现我的搜索栏正在搜索呢? Cont
我正在使用 MVC5,我想搜索结果并停留在同一页面,这是我在 Controller (LiaisonsProjetsPPController) 中执行搜索操作的方法: public ActionRes
Azure 搜索中的两种方法 Upload 与 MergeOrUpload 之间有什么区别。 他们都做完全相同的事情。即,如果文档不存在,它们都会上传文档;如果文档已经存在,则替换该文档。 由于这两种
实际上,声音匹配/搜索的当前状态是什么?我目前正在远程参与规划一个 Web 应用程序,该应用程序将包含和公开记录的短音频剪辑(最多 3-5 秒,人名)的数据库。已经提出了一个问题,是否可以实现基于用户
在商业应用程序中,具有数百个面并不罕见。当然,并非所有产品都带有所有这些标记。 但是在搜索时,我需要添加一个方面查询字符串参数,其中列出了我想要返回的所有方面。由于我事先不知道相关列表,因此我必须在查
当我使用nvcc 5.0编译.cu文件时,编译器会为我提供以下信息。 /usr/bin/ld: skipping incompatible /usr/local/cuda-5.0/lib/libcud
我正在使用基于丰富的 Lucene 查询解析器语法的 Azure 搜索。我将“~1”定义为距离符号的附加参数)。但我面临的问题是,即使存在完全匹配,实体也没有排序。 (例如,“blue~1”将返回“b
我目前有 3 个类,一个包含 GUI 的主类,我在其中调用此方法,一个包含数据的客户类,以及一个从客户类收集数据并将其放入数组列表的 customerList 类,以及还包含搜索数组列表方法。 我正在
假设我有多个 6 字符的字母数字字符串。 abc123、abc231、abc456、cba123、bac231 和 bac123 。 基本上我想要一个可以搜索和列出所有 abc 实例的选择语句。 我只
我有这个表 "Table"内容: +--------+ | Serial | +--------+ | d100m | <- expected result | D100M | <- expect
我是一名优秀的程序员,十分优秀!