【发布时间】:2015-07-10 14:09:06
【问题描述】:
所以我正在制作一个接收员工数据(id、姓名等)的程序。我已经从 sanfoundry 教程中获得了大部分内容,现在我只是想知道如何保存员工记录我在一个文本文件中创建。当然还有在开始运行程序时如何打开该文本文件。
我能够正常执行此操作,但我发现很难将其实现到教程中的代码中。
FILE *file = fopen("C:\\Users\\Me\\Desktop\\AdvProgrammingAssignment\\employees.txt", "r");
char c;
if(file == NULL) {
printf("Error, file not found");
}
do {
c = fgetc(file);
printf("%c", c);
}
while(c != EOF);
fclose(file);
这适用于读入(它来自我所做的另一个程序)但是现在我正在尝试添加教程的功能并让链表在退出时写入文本文件,我有点无能为力。
这是教程中的代码(不是全部)
void main()
{
struct emp_data *linkList;
char name[21], desig[51], addre[25], hd[15], empEmail[15], empSal[10];
char choice;
int eno;
linkList = NULL;
printf("\n Welcome to the employee database \n");
menu();
do
{
/* choose oeration to be performed */
choice = option();
switch(choice)
{
case '1':
printf("\n Enter the Employee Number : ");
scanf("%d", &eno);
printf("Enter the Employee name : ");
fflush(stdin);
gets(name);
printf("Enter the Employee Department : ");
gets(desig);
printf("Enter the Employee Address : ");
gets(addre);
printf("Enter the Employee Hire Date : ");
gets(hd);
printf("Enter the Employee Email : ");
gets(empEmail);
printf("Enter the Employee Salary : ");
gets(empSal);
linkList = insert(linkList, eno, name, desig, addre, hd, empEmail, empSal);
break;
case '2':
printf("\n\n Enter the employee number to be deleted: ");
scanf("%d", &eno);
linkList = deleteNode(linkList, eno);
break;
case '3':
if (linkList == NULL)
{
printf("\n Database empty.");
break;
}
display(linkList);
break;
case '4':
printf("\n\n Enter the employee number to be searched: ");
scanf("%d", &eno);
search(linkList, eno);
break;
case '5': break;
}
} while (choice != '5');
}
我应该创建一个新方法并在 main 的开头调用它吗?任何帮助将不胜感激。
【问题讨论】:
-
fgetc返回int(不是char) -
好的...关于如何解决我的问题有什么想法吗?
-
int fgetc( std::FILE* stream ).. [链接]更多信息en.cppreference.com/w/cpp/io/c/fgetc -
您的问题是读数不正确?或者你得到一个特定的异常/错误?
标签: c struct linked-list dynamic-memory-allocation