Project Name : " Employee Information System "
I am trying to add a new department name in "dept.txt" file.
using file Operations and funcctions defined in String Header file in C
Problem is in fnAddDepartment() function. When i run program in VC++ compiler, it says :
Debug Assertion Failed!
Program: F:\EIS\dept1\Debug\dept1.exe
File:fgets.c
Expression : str!=NULL
the text file " dept.txt" has following data stored:
"1000CCD
1001Human Resources
1002BCMD"
*****************************************************/
* Filename : FileFunctions.h
* Date : 24-Feb-2005
* Description : Declaration of functions and constants required to use the functions defined
***********************************************/
/* Pre-defined constants */
/* Constants to choose Dept file or Employee file */
#define DEP_FILE 1
#define EMP_FILE 2
/* Constants to define position in file */
#define BEGIN 0
#define CURRENT 1
/* Length of records */
#define EMP_RECLEN 50
#define DEPT_RECLEN 50
/* end of file and file related error codes */
#define ERROR_OPEN_FILE -2
#define ERROR_CLOSE_FILE -3
#define FILE_OPENED 3
#define FILE_CLOSED 4
#define END_OF_FILE -1
#define RECORD_READ_SUCCESS 4
#define RECORD_UPDATE_SUCCESS 5
FILE *fpDept;
FILE *fpEmp;
/* Function declarations */
int fnOpenFile ( int iFile );
int fnReadFile ( char acLine[], int iFile, int ipos);
void fnWriteFile ( char acLine[], int iFile);
int fnUpdateFile ( char acLine[], int iFile);
int fnCloseFile ( int iFile);
/******************************************************************************
* End of FileFunctions.h
******************************************************************************/
/* All the necessary header files included */
#include<stdio.h>
#include<stdlib.h>
#include<FileFunctions.h> /* a user defined header file */
#include<conio.h>
#include<string.h>
#include<windows.h>
/* Declare the prototypes of functions used */
void fnGotoxy(int icoord_x, int icoord_y);
void fnDisplayMainMenu();
void fnDisplayDepartmentMenu();
void fnAddDepartment();
void fnUpdateDepartmentName();
void fnPrintDepartmentDetails();
void fnGotoMainMenu();
int AutoGenerateDepartmentCode();
/* Defining Macros */
# define FIELD_SIZE 25
# define ARR_TEMP_SIZE 16
/******************************************************************************************
* Function name : main
* Description : It displays the Main Menu and all the functions and modules are
integrated in this function.
* Input Parameters :
* int agrc - Number of command line arguments
* char **argv - The command line arguments passed
* Returns - 0 on successful completion of program to operating system
********************************************************************************************/
int main (int argc, char **argv){
fnGotoxy(25,5);
printf(" Employee Information System ");
fnGotoxy(25,6);
printf(" ============================ ");
fnGotoxy(25,8);
printf(" Login Screen ");
fnGotoxy(25,9);
printf("=============");
fnDisplayMainMenu();
return 0;
}
/**********************************************************************************************
* Function name : fnGotoxy
* Description : It moves the cursor to the given coordinate position on screen.
*Input Parameters:
*int icoord_x : The given x coordiante. It is of integer datatype.
*int icoord_y : The given y coordinate . It is of integer datatype
*Returns : void
***********************************************************************************************/
void fnGotoxy(int icoord_x,int icoord_y)
{
static HANDLE hStdout = NULL;
COORD coord;
coord.X = icoord_x;
coord.Y = icoord_y;
if(!hStdout)
{
hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
}
SetConsoleCursorPosition(hStdout,coord);
}
void clrscr(void)
{
static HANDLE hStdout = NULL;
static CONSOLE_SCREEN_BUFFER_INFO csbi;
const COORD startCoords = {0,0};
DWORD dummy;
if(!hStdout)
{
hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(hStdout,&csbi);
}
FillConsoleOutputCharacter(hStdout,
' ',
csbi.dwSize.X * csbi.dwSize.Y,
startCoords,
&dummy);
fnGotoxy(0,0);
}
void fnDisplayMainMenu()
{
char cChoice;
system("cls");
fnGotoxy(25,5);
printf(" Employee Information System ");
fnGotoxy(25,6);
printf(" ============================ ");
fnGotoxy(29,8);
printf(" Main Menu");
fnGotoxy(29,9);
printf("=============");
fnGotoxy(25,12);
printf("1. Department Maintenance");
fnGotoxy(25,13);
printf("2. Employee Maintenance ");
fnGotoxy(25,14);
printf("3. Telephone Directory Maintenance ");
fnGotoxy(25,15);
printf("4. Reports ");
fnGotoxy(25,16);
printf("5. Exit ");
fnGotoxy(25,17);
printf("Enter Your Choice : ");
cChoice = getchar();
switch(cChoice)
{
case '1':
fnDisplayDepartmentMenu();
break;
case '2':
fnEmployeeMasterMaintenance();
break;
case '3':
fnTelephoneDirectoryMaintenance();
break;
case '4':
fnReports();
break;
case '5':
fnExit();
break;
default:
printf(" Please enter the correct choice : ");
}
}
/********************************************************************************************
* Function name : fnDisplayDepartmentMenu()
* Description : It displays the Department Menu.
*Input Parameters: None
*Returns : void
***********************************************************************************************/
void fnDisplayDepartmentMenu()
{
char cChoice1;
system("cls");
fnGotoxy(25,5);
printf("Employee Information System");
fnGotoxy(25,6);
printf("===========================");
fnGotoxy(25,8);
printf(" Department Maintenance Menu");
fnGotoxy(25,9);
printf(" ==========================");
fnGotoxy(25,12);
printf(" 1. Add Department");
fnGotoxy(25,13);
printf(" 2. Update Department Name ");
fnGotoxy(25,14);
printf(" 3. Print Department Details ");
fnGotoxy(25,15);
printf(" 4. Goto Main Menu ");
fnGotoxy(25,17);
printf(" Enter your Choice : ");
fflush(stdin);
cChoice1 = getchar();
switch(cChoice1)
{
case '1':
fnAddDepartment();
break;
case '2':
fnUpdateDepartmentName();
break;
case '3':
fnPrintDepartmentDetails();
break;
case '4':
fnGotoMainMenu();
break;
default:
printf("Please enter correct choice :");
}
}
/******************************************************************************
* Function: fnOpenFile
* Description: Opens a file specified by the parameter. Looks for the file
* in the current working directory.
* Input Parameters:
* int iFile - The file to be opened. Can have values
* DEP_FILE - Opens Department file (dept.txt)
* EMP_FILE - Opens Employee file (emp.txt)
* Returns : FILE_OPENED on success. ERROR_OPEN_FILE if an error occurs
* when opening a file
******************************************************************************/
int fnOpenFile ( int iFile )
{
char acFileName[20];
switch (iFile) {
case DEP_FILE:
strcpy (acFileName, "dept.txt");
fpDept = fopen(acFileName, "r+");
if (NULL == fpDept) {
return ERROR_OPEN_FILE;
}
break;
case EMP_FILE:
strcpy (acFileName, "emp.txt");
fpEmp = fopen(acFileName, "r+");
if (NULL == fpEmp) {
return ERROR_OPEN_FILE;
}
break;
default:
return FILE_OPENED;
break;
}
return 0;
}
/******************************************************************************
* Function: fnWriteFile
* Description: Adds a record (one line) from the array acLine[] into a file
* specified by parameter iFile
* Input Parameters:
* char acLine[] - character array whose contents have to be written
* int iFile - The file into which record has to be added. Can have values
* DEP_FILE - reads from Department file (dept.txt)
* EMP_FILE - reads from Employee file (emp.txt)
* Returns: None
******************************************************************************/
void fnWriteFile ( char acLine[], int iFile)
{
switch (iFile)
{
case DEP_FILE:
fseek(fpDept, 0L, SEEK_END);
fprintf (fpDept, "%s\n", acLine);
break;
case EMP_FILE:
fseek(fpEmp, 0L, SEEK_END);
fprintf (fpEmp, "%s\n", acLine);
break;
}
}
/******************************************************************************
* Function: fnCloseFile
* Description: Closes the file specified in the parameter.
* Input Parameters:
* int iFile - The file which has to be closed. Can have values
* DEP_FILE - reads from Department file (dept.txt)
* EMP_FILE - reads from Employee file (emp.txt)
* Returns: FILE_CLOSED on success. 1 on error
******************************************************************************/
int fnCloseFile ( int iFile)
{
switch (iFile)
{
case DEP_FILE:
if (fclose (fpDept)) {
return ERROR_CLOSE_FILE;
}
break;
case EMP_FILE:
if (fclose (fpEmp)) {
return ERROR_CLOSE_FILE;
}
break;
default:
return ERROR_CLOSE_FILE;
break;
}
return FILE_CLOSED;
}
int AutoGenerateDepartmentCode()
{
static int iIncrement=1002;
iIncrement++;
return iIncrement;
}
void fnAddDepartment()
{
int iCheck=0;
int iRet=0;
int iCount=0;
int iLength=0;
int Dept_Code=0;
char Dept_Name[16];
static char ArrTemp[ARR_TEMP_SIZE];
char acLine[25];
char aCompare[DEPT_RECLEN];
FILE *fpD;
system("cls");
fpD=fopen("dept.txt", "r");
fnGotoxy(25,5);
printf("Employee Information System");
fnGotoxy(25,6);
printf("===========================");
fnGotoxy(25,8);
printf(" Add a Department");
fnGotoxy(25,9);
printf(" ==========================");
fnGotoxy(20,11);
printf("Enter a new Department Name :");
fflush(stdout);
fgets(ArrTemp, ARR_TEMP_SIZE, stdin);
for(iLength=0; iLength<ARR_TEMP_SIZE; iLength++)
{
ArrTemp[iLength]=Dept_Name[iLength];
}
if(fpD=NULL)
{
fnGotoxy(20,15);
printf(" Cannot open file \"dept.txt\" ");
exit(0);
}
if(fgets(aCompare, DEPT_RECLEN, fpD)== NULL )
{
printf(" dept.txt file is empty");
}
else
{
if(strstr(aCompare, Dept_Name)==NULL)
{
for(iCount=0; iCount<ARR_TEMP_SIZE; iCount++)
{
if(Dept_Name[iCount]==32 || Dept_Name[iCount]==38 || Dept_Name[iCount]==45 ||
(Dept_Name[iCount] >= 65 && Dept_Name[iCount] <= 90) || (Dept_Name[iCount] >= 97 &&
Dept_Name[iCount] <=122))
{
iCheck = strlen(Dept_Name);
if(iCheck <3 || iCheck > 15)
{
fnGotoxy(20,15);
printf(" ERROR : Invalid Length of Department Name ");
fnGotoxy(20,17);
printf(" Please reenter department name ");
fnAddDepartment();
}
else
{
if(Dept_Name[0] >= 65 && Dept_Name[0] <= 90)
{
Dept_Code=AutoGenerateDepartmentCode();
fnGotoxy(25,13);
printf("Department Code : %d",Dept_Code);
fnOpenFile(DEP_FILE);
sprintf(acLine, "-20%s %4d", Dept_Name, Dept_Code);
fnGotoxy(25,15);
printf("%s",acLine);
fnWriteFile(acLine, DEP_FILE);
fnCloseFile(DEP_FILE);
fnGotoxy(25,17);
printf(" Department Name succesfully Added!! ");
fnGotoxy(25,19);
printf("Press any Key to go back to Department Maintenance Menu.. ");
getch();
fnDisplayDepartmentMenu();
}
else
{
fnGotoxy(25,15);
printf(" The first word of the entered department name should be a CAPITAL letter ");
fnGotoxy(25,17);
printf(" Please reenter the department name...");
fnAddDepartment();
}
}
}
else
{
fnGotoxy(25,17);
printf(" Invalid Department Name entered..");
fnGotoxy(25,18);
printf("Please reenter the department name");
fnAddDepartment();
}
}
}
else
{
fnGotoxy(20,15);
printf(" This Department is already in record ");
fnAddDepartment();
}
}
}