- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在加载函数时遇到了问题,它似乎永远无法工作,我能做些什么来帮助调试此代码;注:需要将文件实际加载到结构数组中
typedef struct
{
char FirstName[20];
char LastName[20];
char StreetAddress[20];
char Email[20];
char PhoneNumber[20];
char DateofBirth[20];
}phone;
void AddEntry(phone*);
void DeleteEntry(phone*);
void LoadEntry(phone*);
void SearchForNumber(phone*);
void Modify(phone*);
void SavetoFile(phone*);
void PrintDataEntry(phone*);
int counter = 0;
char FileName[256];
FILE *f;
FILE *pWrite;
int main(void)
{ int oldp=counter;
int p=counter;
phone*phonebook;
phonebook = (phone*)malloc(sizeof(phone)*100);
int iSelection = 0;
if (phonebook ==NULL)
{
printf("Out of Memory. The program will now exit\n");
return 1;
}
else
{}
do{
printf("%d",oldp);
printf("\n\n\t(1)\tAdd person");
printf("\n\t(2)\tSave added persons");
printf("\n\t(3)\tLoad Phone book Entries");
printf("\n\t(4)\tModify a person's data");
printf("\n\t(5)\tSearch for Phone Number");
printf("\n\t(6)\tDelete person");
printf("\n\t(7)\tPrint the phone book");
printf("\n\t(8)\tExit the phone book");
printf("\n\nWhat would you like to do?");
scanf("%d",&iSelection);
switch(iSelection)
{
case(1):
{
AddEntry(phonebook);
break;
}
case(2):
{
SavetoFile(phonebook);
break;
}
case(3):
{
LoadEntry(phonebook);
break;
}
case(4):
{
Modify(phonebook);
break;
}
case(5):
{
SearchForNumber(phonebook);
break;
}
case(6):
{
DeleteEntry(phonebook);
break;
}
case(7):
{
PrintDataEntry(phonebook);
break;
}
case(8):
{
break;
}
}
}while(iSelection <= 9);
}
void Modify(phone*phonebook)
{ int oldp=counter;
int x = 0, i = 0;
char searchTarget[20];
printf("\nLast name:");
scanf("%s",searchTarget);
for(x=0;x<counter;x++)
{
if(!strcmp(searchTarget,phonebook[x].LastName))
i = x;
}
if(i)
{
printf("\nFirst name:");
scanf(" %[^\n]",phonebook[i].FirstName);
printf("\nStreet Address:\n");
scanf(" %[^\n]",phonebook[i].StreetAddress);
printf("\nEmail:\n");
scanf("%s",phonebook[i].Email);
printf("\nPhone Number:\n");
scanf("%s",phonebook[i].PhoneNumber);
}
else
printf("That contact was not found, please try again.");
}
void AddEntry(phone*phonebook)
{ int oldp=counter;
pWrite = fopen("phonebook_contacts.txt","a");
if(pWrite == NULL)
{
perror("The following error occurred");
exit(EXIT_FAILURE);
}
else
{
realloc(phonebook,sizeof(phone));
printf("\nFirst Name:\n");
scanf(" %[^\n]",phonebook[counter].FirstName);
printf("\nLast Name:\n");
scanf(" %[^\n]",phonebook[counter].LastName);
printf("\nStreet Address:\n");
scanf(" %[^\n]",phonebook[counter].StreetAddress);
printf("\nEmail:\n");
scanf("%s",phonebook[counter].Email);
printf("\nPhone Number:\n");
scanf("%s",phonebook[counter].PhoneNumber);
printf("\n\tPerson was successfully added to Phonebook\n");
fprintf(pWrite,"%s,%s,%s,%s,%s\n",phonebook[counter].FirstName,phonebook[counter].LastName,phonebook[counter].StreetAddress,phonebook[counter].Email,phonebook[counter].PhoneNumber);
counter++;
fclose(pWrite);
}
}
void DeleteEntry(phone*phonebook)
{ int oldp=counter;
int x = 0;
int i = 0;
char deleteFirstName[20];
char deleteLastName[20];
printf("\nFirst name:");
scanf("%s",deleteFirstName);
printf("\nLast name:");
scanf("%s",deleteLastName);
for(x=0;x<counter;x++)
{
if(strcmp(deleteFirstName,phonebook[x].FirstName) == 0)
{
if(strcmp(deleteLastName,phonebook[x].LastName) == 0)
{
for(i=x;i<counter-1;i++)
{
strcpy(phonebook[i].FirstName,phonebook[i+1].FirstName);
strcpy(phonebook[i].LastName,phonebook[i+1].LastName);
strcpy(phonebook[i].PhoneNumber,phonebook[i+1].PhoneNumber);
}
printf("Record deleted from the phonebook.\n\n");
--counter;
return;
}
}
}
printf("That contact was not found, please try again.");
}
void LoadEntry(phone*phonebook)
{
struct phone;
FILE *f;
f=fopen("phonebook_contacts.txt","r");
if(f==NULL)
{
printf("\nfile opening error in listing :");
exit(1);
}
while(fread(&f,sizeof(f),1,f)==1)
{
printf("\n\n\n YOUR RECORD IS\n\n ");
printf("%s%s%s%s%s",phonebook[counter].FirstName,phonebook[counter].LastName,phonebook[counter].StreetAddress,phonebook[counter].Email,phonebook[counter].PhoneNumber);
getch();
}
fclose(f);
}
void PrintDataEntry (phone*phonebook)
{ int oldp=counter;
int x = 0;
printf("\nPhonebook Entries:\n\n");
f=fopen("phonebook_contacts.txt","r");
if(f== NULL)
{
perror("The following error occurred:");
exit(EXIT_FAILURE);
}
else
{
for(x=0;x<counter;x++)
{
printf("%s%s%s%s%s",phonebook[counter].FirstName,phonebook[counter].LastName,phonebook[counter].StreetAddress,phonebook[counter].Email,phonebook[counter].PhoneNumber);
}
}
fclose(f);
}
void SortByDateofBirth(phone*phonebook)
{ int oldp=counter;
int i = 0;
int x = 0;
int swap;
int TempCounter = counter;
phone Temp;
do{
swap = 0;
for(i=1;i<TempCounter;i++)
{
if(strcmp(phonebook[i-1].DateofBirth,phonebook[i].DateofBirth)>0)
{
Temp = phonebook[i];
phonebook[i] = phonebook[i-1];
phonebook[i-1] = Temp;
strcpy(Temp.DateofBirth,phonebook[i].DateofBirth);
strcpy(Temp.LastName,phonebook[i].LastName);
strcpy(Temp.PhoneNumber,phonebook[i].PhoneNumber);
swap = 1;
}
}
TempCounter--;
}
while(swap);
printf("\nYour friends in Time Order by Date of Birth:\n\n");
for(x=0;x<counter;x++)
{
printf("\n(%d)\n",x+1);
printf("Name: %s %s\n",phonebook[x].DateofBirth,phonebook[x].LastName);
printf("Number:%s\n",phonebook[x].PhoneNumber);
}
}
void SortByLastName (phone*phonebook)
{ int oldp=counter;
int i = 0;
int x = 0;
int swap;
int TempCounter = counter;
phone Temp;
do{
swap = 0;
for(i=1;i<TempCounter;i++)
{
if(strcmp(phonebook[i-1].LastName,phonebook[i].LastName)>0)
{
Temp = phonebook[i];
phonebook[i] = phonebook[i-1];
phonebook[i-1] = Temp;
strcpy(Temp.FirstName,phonebook[i].FirstName);
strcpy(Temp.LastName,phonebook[i].LastName);
strcpy(Temp.PhoneNumber,phonebook[i].PhoneNumber);
swap = 1;
}
}
TempCounter--;
}
while(swap);
printf("\nYour friends in Alphabetical order by First Name:\n\n ");
for(x=0;x<counter;x++)
{
printf("\n(%d)\n",x+1);
printf("Name: %s %s\n",phonebook[x].FirstName,phonebook[x].LastName);
printf("Number:%s\n",phonebook[x].PhoneNumber);
}
}
void SearchForNumber (phone*phonebook)
{
int x=0,*i,j=0;
char TempLastName[20];
printf("\nplease type the Last name of the friend you wish to find his number.");
printf("\n\nLast Name:");
scanf("%s",TempLastName);
printf("\nsearch result:\n");
for(x=0;x<counter;x++)
{
if(!strcmp(TempLastName,phonebook[x].LastName))
{
i[j] = x;
j++;
}
}
for(j=0;j<counter;j++)
{
printf("%s,",phonebook[i[j]].FirstName);
printf("%s,",phonebook[i[j]].StreetAddress);
printf("%s,",phonebook[i[j]].Email);
printf("%s",phonebook[i[j]].PhoneNumber);
}
}
void SavetoFile (phone*phonebook)
{ int oldp=counter;
fprintf(pWrite,"%s,%s,%s,%s,%s\n",phonebook[counter].FirstName,phonebook[counter].LastName,phonebook[counter].StreetAddress,phonebook[counter].Email,phonebook[counter].PhoneNumber);
oldp = counter;
}
void Quit (phone*phonebook)
{ int oldp=counter;
fopen("phonebook_contacts.txt","r");
if(!(oldp == counter))
printf("The data entered isn't saved.\nAll of the changes will be discarded.");
else
exit(phonebook);}
退出函数不应包含任何文件名,也不应包含打印! 我无法理解如何将文件加载到结构数组中,因此如何在不泄漏太多内存大小的情况下完成此操作。
最佳答案
看一下这一行:
while(fread(&f,sizeof(f),1,f)==1)
为什么使用f
两次?你的编译器没有对你尖叫吗?
f
是一个 FILE*
- 它不是您可以存储数据的东西。
你是故意的吗
while(fread(&phone,sizeof(phone),1,f)==1)
^^^^^ ^^^^^
或pephaps
while(fread(&phonebook[counter],sizeof(*phonebook),1,f)==1)
但话又说回来,你永远不会增加计数器
....
关于c - C 中的电话簿程序将文件加载到结构数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59363627/
我正在学习 Java 并尝试制作一个简单的电话簿。对于这一部分,我尝试提示用户选择以下 3 个选项之一。 public class PhoneBook { public static void
我正在尝试将联系人插入到 Android 联系人数据库中。找了几个教程(没看懂),基本上是从Android SDK的“ContactManager”示例代码中复制方法。 我使用此方法添加新的原始联系人
我试图在诺基亚 5130c-2 XpressMusic 上访问手机的电话簿和 SIM phonenook。 该应用程序运行时没有错误,但它只返回手机电话簿中的号码。当我使用此代码列出可用的电话簿时 S
我在布置家庭作业时遇到了麻烦,我们应该制作电话簿吗?条件是它应该看起来像这样: Class Phonebook { private List _phoneList; public
感谢您的大力支持。我得承认 C++ 很有趣而且非常了不起,但有时它会让人头疼。我一直在努力解决这个家庭作业。它快完成了,但我在尝试弄清楚如何使用二进制文件时遇到了一些困难。 问题如下: You hav
class Phone_book: #Please note that this line of code has no indentation, all other lines has at lea
这个问题已经有答案了: PHP parse/syntax errors; and how to solve them (20 个回答) 已关闭 7 年前。 我在 上收到错误线。 错误: Parse
我正在尝试实现一个线程安全的电话簿对象。电话簿应该可以添加一个人,并根据姓名和电话号码查找一个人。从实现的角度来看,这只涉及两个哈希表,一个关联名称 -> Person,另一个关联 phone# ->
我想在我的 AS3 应用程序中获取 iPhone 和 android 电话簿中的所有联系人。这在 as3 中如何完成? -提前致谢 最佳答案 您将需要使用 native 扩展。 检查这个: https
我是一名优秀的程序员,十分优秀!