Hi all,
This may be a stupid question, but I have never read in a file before in C so can't figure this one out. I have the following code:
Code:
#include <stdio.h>
#include <string.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(filename);
fptr= fopen(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;
}
and I also have a file named"student.dat" with the following data:
Chris Thomson 18344567 2320 95656777
Anna Smith 19233350 0085 99055681
John Kennedy 18345678 0333 97913467
I was unsure of what to do so added this to the project like I would any other c file. Then ran my code, if i type in "student" or "student.dat" (without the double quotes) my program immediately reads my error message saying it can not open the input file and terminates the program.
why can I not read in this file? Any help fixing up this error would be greatly appreciated! Thanks in advance!