I have a program that requires to read from a text file with a .cvs file extension.
The program tends to add, edit, delete records written in the file. But before anything, the program simply can't read the .cvs file to begin with. Anyone can guide/help me on this? Thanks.
This is my whole source code (Some functions are not yet finished):
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define MAX_STUD_NUM 9
#define MAX_STUD_LASTNAM 20
#define MAX_STUD_FIRSTNAM 20
#define MAX_STUD_MIDNAM 20
#define OK 0
#define CANCEL -2
#define TRUE 1
#define FALSE 0
#define ERROR -1
#define error01 1
#define error02 2
#define error03 3
#define error04 4
#define error05 5
#define error06 6
typedef struct StudInfoType
{
char stud_num[MAX_STUD_NUM + 1];
char stud_lastnam[MAX_STUD_LASTNAM + 1];
char stud_firstnam[MAX_STUD_FIRSTNAM + 1];;
char stud_midnam[MAX_STUD_MIDNAM + 1];;
} STUDENT;
struct StudInfoType * StudRec;
int StudRec_count = 0;
FILE * ptr;
int SearchFiles1();
int SearchRecord(const char * string);
void ViewStudRec();
void AddStudRec();
void EditStudRec();
void DelStudRec();
int SaveInfo();
int BlankCatch(const char * temp_buff);
int StudNumCatch(const char * str);
int NameCatch(const char * str);
void error(int errType);
int CountRecords();
int main()
{
char choice[10];
if(SearchFiles1() == ERROR)
{
printf("File not found. \n");
system("pause");
return ERROR;
}
fflush(stdin);
do
{
system("cls");
memset(choice, '\0', 10);
printf("\n-----Student Record-----\n");
printf("[1] Add Record(s)\n");
printf("[2] Edit Record(s)\n");
printf("[3] Delete Record(s)\n");
printf("[4] View Record(s)\n");
printf("[0] Exit\n\n");
printf("Menu: ");
gets(choice);
switch(choice[0])
{
case '1': AddStudRec(); break;
case '2': EditStudRec(); break;
case '3': DelStudRec(); break;
case '4': ViewStudRec(); break;
case '0': break;
default: error(error01);
getch();
}
}
while(choice[0] != '0');
free(StudRec);
return 1;
}
int CountRecords()
{
FILE *ptr;
char array[30];
int count_file = 0;
ptr = fopen("student.cvs","rt");
if ( ptr==NULL )
{
return ERROR;
}
while(!feof(ptr))
{
fscanf(ptr, "%[^\n]\n",array );
count_file++;
}
fclose(ptr);
return count_file;
}
int BlankCatch(const char * temp_buff)
{
if(strlen(temp_buff) != 0)
return FALSE;
else
return TRUE;
}
void error(int nErrorType)
{
switch(nErrorType)
{
case error01: printf("Invalid input.\n"); break;
case error02: printf("Code already exists.\n"); break;
case error03: printf("Code does not exist.\n"); break;
case error04: printf("Student Code must be in this format: XXXX-XXXX.\n"); break;
case error05: printf("Names must not exceed 20 characters in length.\n"); break;
case error06: printf("Invalid unit price.\n"); break;
}
}
int SearchFiles1()
{
int i = 0;
int nRet = 0;
nRet = CountRecords();
if ( nRet == ERROR )
{
return ERROR;
}
StudRec_count = nRet;
StudRec = (struct StudInfoType*) malloc(StudRec_count * sizeof(struct StudInfoType));
ptr = fopen("student.cvs","r");
char s_code[9];
char s_lname[20];
char s_fname[20];
char s_mname[20];
while ( fscanf( ptr, "%s %s %s%s" , s_code,s_lname,s_fname,s_mname) == 3 )
{
s_code[strlen(s_code)-1] = '\0';
strcpy(StudRec[i].stud_num, s_code);
s_lname[strlen(s_lname)-1] = '\0';
strcpy(StudRec[i].stud_lastnam, s_lname);
s_fname[strlen(s_fname)-1] = '\0';
strcpy(StudRec[i].stud_firstnam, s_fname);
s_mname[strlen(s_mname)-1] = '\0';
strcpy(StudRec[i].stud_midnam, s_mname);
i++;
}
fclose(ptr);
return OK;
}
int SearchRecord(const char * str)
{
int ctr = 0;
while(ctr < StudRec_count)
{
if(strcmp(str,StudRec[ctr].stud_num) == 0)
{
return ERROR;
break;
}
else
ctr++;
}
return OK;
}
int SaveInfo()
{
return ERROR;
}
void AddStudRec()
{
FILE * ptr;
ptr = fopen("student.cvs","a");
if(ptr == NULL)
{
perror("Error.");
system("pause");
exit(1);
}
int index = 0;
int check_name = 0;
int inPrice = 0;
char a_code[MAX_STUD_NUM + 1];
char a_lname[MAX_STUD_LASTNAM + 1];
char a_fname[MAX_STUD_FIRSTNAM + 1];
char a_mname[MAX_STUD_MIDNAM + 1];
index = 0;
do
{
again:
fflush(stdin);
system("cls");
printf("Enter student code: ");
gets(a_code);
index = BlankCatch(a_code);
if(index == FALSE)
{
index = 0;
index = StudNumCatch(a_code);
if(index == ERROR)
{
error(error04);
getch();
goto again;
}
else if(index == CANCEL)
{
fclose(ptr);
main();
break;
}
else
{
index = 0;
index = SearchRecord(a_code);
if(index == ERROR)
{
error(error02);
getch();
goto again;
}
else
{
check_name = 0;
enterlastname:
fflush(stdin);
printf("Enter student last name: ");
gets(a_lname);
check_name = BlankCatch(a_lname);
if(check_name == FALSE)
{
check_name = 0;
check_name = NameCatch(a_lname);
if(check_name == ERROR)
{
error(error05);
getch();
goto enterlastname;
}
else if(check_name == CANCEL)
{
fclose(ptr);
main();
break;
}
else
{
check_name = 0;
enterfirstname:
fflush(stdin);
printf("Enter student first name: ");
gets(a_fname);
check_name = BlankCatch(a_fname);
if(check_name == FALSE)
{
check_name = 0;
check_name = NameCatch(a_fname);
if(check_name == ERROR)
{
error(error05);
getch();
goto enterfirstname;
}
else if(check_name == CANCEL)
{
fclose(ptr);
main();
break;
}
else
{
printf("Item is added\n");
break;
}
}
else
{
error(error01);
getch();
goto enterfirstname;
}
}
}
else
{
error(error01);
getch();
goto enterlastname;
}
}
}
}
else
{
error(error01);
getch();
system("cls");
continue;
}
}
while(index != CANCEL || check_name == CANCEL);
fprintf(ptr,"%s, %s, %s", a_code, a_lname, a_fname);
fflush(stdin);
fclose(ptr);
system("pause");
}
void EditStudRec()
{
printf("not available..2\n");
system("pause");
}
void DelStudRec()
{
printf("not available..3\n");
system("pause");
}
void ViewStudRec()
{
int ctr, ctr2;
int space;
printf("Code");
printf(" Description ");
printf("Price");
printf("\n");
for(ctr = 1;ctr < StudRec_count;ctr++)
{
printf("%s",StudRec[ctr].stud_num);
printf(" %s",StudRec[ctr].stud_lastnam);
strlen(StudRec[ctr].stud_lastnam);
space =(16 - strlen(StudRec[ctr].stud_lastnam));
for(ctr2 = 0;ctr2<space+1;ctr2++)
{
printf(" ");
}
//printf("%.2f\n",StudRec[ctr].item_price);
}
system("pause");
}
int StudNumCatch(const char * str)
{
int c;
if(strcmp(str,"000") == 0)
return CANCEL;
else if(strlen(str)<MAX_STUD_NUM || strlen(str)>MAX_STUD_NUM)
return ERROR;
else
return OK;
while((c = *(str++)) != '\0')
{
if(c >= 58 && c<= 126)
return ERROR;
else
return OK;
}
}
int NameCatch(const char * str)
{
int c;
if(strcmp(str,"000") == 0)
return CANCEL;
else if(strlen(str) > MAX_STUD_LASTNAM)
return ERROR;
while((c = *(str++)) != '\0')
{
if(isdigit(c))
return ERROR;
else
return OK;
}
}
The text file (student.cvs) include the following record samples:
2009-1234, Asa, Gohan, Gogo
2009-4321, Basha, Bushum, jujog
2009-1999, Mekemi, Mekeke, Makeke
So there, I keep getting a FILE NOT FOUND return. Any help is much appreciated. Thanks again...