我的代码:
#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define NAME_LEN 30
#define LINE_LEN 80
struct record {
char *firstname;
char *lastname;
long int idnumber;
int ccode;
long int phonenum;
struct record *next;
};
typedef struct record STUDENT;
STUDENT *header; /* pointer to the start of linked list */
char fname[NAME_LEN], lname[NAME_LEN];
char filename[LINE_LEN];
long int id, phone;
int course;
FILE *fptr;
STUDENT *makenode(long int stud_id, int c_code, long int phone, char *fname, char *lname);
void insert_node();
void delete_node();
void search_node();
void print_list();
void output_list();
int main()
{
int choice;
STUDENT *p, *q;
q = NULL;
printf(" Enter the input file name: ");
gets_s(filename);
fptr = fopen_s(filename, "r");
if (fptr == NULL)
printf("Error, can't open the input file %s \n", filename);
else
{
while (!feof(fptr))
{
fscanf(fptr, " %s %s %ld %d %ld", fname, lname, &id, &course, &phone);
/* printf("%s %s %ld %d %ld \n",fname, lname,id,course,phone);*/
p = makenode(id, course, phone, fname, lname);
p->next = q;
q = p;
}
printf("Created the Linked List\n");
header = q;
fclose(fptr);
do {
printf("\n\n MENU \n\n");
printf("1. Insert\n");
printf("2. Delete\n");
printf("3. Search\n");
printf("4. List\n");
printf("5. Save\n");
printf("6. Quit\n");
printf("\n");
printf("Enter your choice: "); /*Prompt user*/
scanf("%d", &choice);
switch (choice)
{
case 1:
insert_node();
break;
case 2:
delete_node();
break;
case 3:
search_node();
break;
case 4:
print_list();
break;
case 5:
output_list();
break;
}
} while (choice != 6);
return 0;
}
}
/***************************************************************/
STUDENT *makenode(long int stud_id, int c_id, long int phone, char *fname, char *lname)
{
STUDENT *ptr;
ptr = (STUDENT *)malloc(sizeof(STUDENT));
if (ptr != NULL)
{
ptr->next = NULL;
ptr->firstname = (char *)malloc(strlen(fname) + 1);
strcpy(ptr->firstname, fname);
ptr->lastname = (char *)malloc(strlen(lname) + 1);
strcpy(ptr->lastname, lname);
ptr->idnumber = stud_id;
ptr->ccode = c_id;
ptr->phonenum = phone;
return(ptr);
}
else
{
printf("Memory not allocated\n");
}
}
/************************************************************/
void insert_node()
{
}
/************************************************************/
void delete_node()
{
}
/***********************************************************/
void search_node()
{
}
/**********************************************************/
void output_list()
{
}
/********************************************************/
void print_list()
{
STUDENT *p1;
p1 = header;
while (p1 != NULL)
{
printf("%s %s %ld %d %ld\n", p1->firstname,
p1->lastname, p1->idnumber, p1->ccode, p1->phonenum);
p1 = p1->next;
}
}
/********************************************************/
我遵循错误指示。我将
gets
和fopen
更改为gets_s
和fopen_s
,但是出现以下错误:IntelliSense:函数调用中的参数太少
IntelliSense:类型为“ char *”的参数与
类型为“ FILE **”的参数
IntelliSense:无法将类型“ errno_t”的值分配给
类型为“ FILE *”的实体错误
错误C2660:'fopen_s':函数未使用2个参数
最佳答案
gets_s()
和fopen_s()
的参数不同于gets
和fopen
。 gets_s()
采用接收缓冲区的大小:
gets_s(filename, sizeof filename);
而
fopen_s()
使用指向FILE*
的指针:if (fopen_s(&fptr, filename, "r"))
printf("Error, can't open the input file %s\n", filename);
fopen_s
返回错误代码。您可以保存该消息,并在出现故障时使用strerror()
获得相应的错误消息。还要注意,您的
while(!feof(fptr))
循环将无法正确停止在文件末尾,它可能会复制最后一组值。请参见Why is “while ( !feof (file) )” always wrong?并通过以下方式修复代码: while (fscanf(fptr, " %s %s %ld %d %ld", fname, lname, &id, &course, &phone) == 5)) {
/* printf("%s %s %ld %d %ld\n",fname, lname,id,course,phone); */
p = makenode(id, course, phone, fname, lname);
p->next = q;
q = p;
}
链接列表的方式实际上将颠倒文件内容中节点的顺序,可能不是您要执行的操作。
使用
scanf("%d", &choice);
获取用户选择是有风险的:如果用户键入非数字,非空格字符,则程序将陷入无限循环。至少检查scanf
的返回值,或从用户那里获取整行,测试文件结尾,然后使用sscanf()
解析用户选择。如果出现内存分配失败的情况,则仅测试三种潜在故障之一,然后仅显示一条消息。您应该返回
NULL
并在调用函数中进行测试,或者使用exit()
或abort()
中止该程序。关于c - gets和fopen不起作用,甚至更改为gets_s和fopen_s,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29692006/