gpt4 book ai didi

c - 搜索文件被程序拒绝

转载 作者:行者123 更新时间:2023-11-30 16:23:03 26 4
gpt4 key购买 nike

我是这个网站的新手,并且认为尝试询问是否有人知道此代码可能出现的问题可能是个好主意,因为它找不到已存在的文件。现在,我尝试更改 "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/

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