-3

My code:

#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;
    }
}
/********************************************************/

I follow the instructions from error. I changed gets and fopen to gets_s and fopen_s, but I got these errors:

  • IntelliSense: too few arguments in function call
  • IntelliSense: argument of type "char *" is incompatible with parameter of type "FILE **"
  • IntelliSense: a value of type "errno_t" cannot be assigned to an entity of type "FILE *" Error
  • error C2660:'fopen_s' : function does not take 2 arguments
Spikatrix
  • 20,225
  • 7
  • 37
  • 83
yoloXswag
  • 1
  • 1
  • 2
  • 1
    Error 1: `gets_s` takes two parameters, not one. – Spikatrix Apr 17 '15 at 06:42
  • so how can i fix this problem, in my lecture, we use gets and fopen to read file, but its not working – yoloXswag Apr 17 '15 at 07:00
  • 1
    see [fopen_s](https://msdn.microsoft.com/en-us/library/z5hh6ee9.aspx) and [gets_s](https://msdn.microsoft.com/en-us/library/5b5x9wc7.aspx) – BLUEPIXY Apr 17 '15 at 07:53
  • @yoloXswag Both gets() and fopen() work fine. You might be told(e.g. by the a warning from the compiler) that you shouldn't use gets()/fopen(), but they still actually works. You may change the code to fopen_s and gets_s, but you will still have the same problem as you had before. It is best if you explain, in clear detail, what you do, what happens, what doesn't work, and what you expected to happen. – nos Apr 17 '15 at 08:40
  • @nos: I beg to differ! `gets()` must be avoided. It should not even be taught, or better should be analyzed as a good example of an insecure API. Used correctly, `gets_s()` and `fgets()` fix the `gets` problem. The OP does have other problems in his code, but I suspect he is using a Microsoft compiler that just refuses to produce an executable that calls `gets`. – chqrlie Apr 17 '15 at 08:45
  • @nos like you said, i think gets and fopen suppose to works fine, and code suppose to open up a dat file, and i have fill up functions at bottom with i can read, write, search in side the dat file. but the problem is all the code seem mess up – yoloXswag Apr 17 '15 at 09:15

1 Answers1

0

gets_s() and fopen_s() take different arguments than gets and fopen. gets_s() takes the size of the receiving buffer:

gets_s(filename, sizeof filename);

and fopen_s() takes a pointer to the FILE*:

if (fopen_s(&fptr, filename, "r"))
    printf("Error, can't open the input file %s\n", filename);

fopen_s returns the error code. You can save that and get an appropriate error message using strerror() in case of failure.

Also note that your while(!feof(fptr)) loop will fail to correctly stop at the end of file, it will potentially duplicate the last set of values. See Why is “while ( !feof (file) )” always wrong? and fix the code this way:

    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;
    }

The way you link the list will actuallt reverse the order of nodes from the file contents, probably not what you intend to do.

Using scanf("%d", &choice); to get user choices is risky: if the user types a non digit, non space character, the program will go into an endless loop. At least check the return value of scanf, or get a full line from the user, test for end of file, and use sscanf() to parse the user choice.

If case of memory allocation failure, you only test one of the three potential failures, and just print a message. You should either return NULL and test that in the calling functions, or abort the program with exit() or abort().

Community
  • 1
  • 1
chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • another question from my codes, some code such as scanf, gets, strcpy. eg. the error says i have to add _s, however, from my lecture, he never told me this problem. and i would like to know why this happened (These were provide from my labs and i have to substitute function inside) – yoloXswag Apr 17 '15 at 09:12