我是这个网站的新手,所以尝试问一下是否有人知道此代码有什么问题可能是一个好主意,因为它找不到一个已存在的文件。现在,我已经尝试过更改{
"name": "xxx",
"version": "x.0.x",
"private": true,
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start",
"test": "jest"
},
"dependencies": {
"react": "16.6.3",
"react-native": "0.57.8",
"react-native-animatable": "^1.3.1",
"react-native-firebase": "^5.2.0",
"react-native-i18n": "^2.0.15",
"react-native-linear-gradient": "^2.5.3"
},
"devDependencies": {
"babel-jest": "23.6.0",
"jest": "23.6.0",
"metro-react-native-babel-preset": "0.51.1",
"react-test-renderer": "16.6.3"
},
"jest": {
"preset": "react-native"
}
}
和"wb"
,因为我没有主意(大声笑),并且还要确保每一行都不会使事情变得有趣,因为我没有遇到任何错误。
当我运行该程序时,我添加了一个具有姓名和数字的人,然后希望能够以两种不同的方式(姓名和数字)搜索该人,在这种情况下,就是带有数字的人。我得到的结果总是和消息“错误”,基本上表示“找不到文件”。
如果有不清楚的地方,或者如果我错过了一些事情,请告诉我,我将尽最大努力确保正确执行并遵守页面规则。谢谢!
答案 0 :(得分:0)
有太多错误,无论是语法错误还是逻辑错误,都值得一提。建议您学习使用调试器,但是以下代码标识了几个问题。寻找内联注释,并遵循建议:
#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);
}