Thread: creating staff system

  1. #1
    Registered User
    Join Date
    Jul 2017
    Posts
    30

    creating staff system

    Code:
    void displayStaff()
    {
        struct staff p;
        FILE *ifp;
        int count = 0;
        SYSTEMTIME t;
    
    
        GetLocalTime(&t);
    
    
        ifp = fopen("staff.dat", "rb");
    
    
        printf("\n\n Staff Details - as at %d-%d-%d  %02d:%02d\n\n", t.wDay, t.wMonth, t.wYear, t.wHour, t.wMinute);
        printf("Name\t\t ID\tPosition \t Salary\n");
        printf("============\t\t===========\t=======\n");
    
    
        while (fread(&p, sizeof(struct staff), 1, ifp) != 0) {
            printf("\t%-15d %-10s %-10s %-10s",p.id,p.name,p.position,p.salary);
            count++;
        }
        printf("\n\t< %d staff listed >\n\n", count);
        fclose(ifp);
    }

    always trigger break point at the while line....need hlp immediately!!

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,451
    Are you sure that your file contains at least 'sizeof(struct staff)' bytes? Because if fread returns 0 it means it either can't find enough bytes to read or something prevents you from reading from the file. Check 'feof()' to see if the first happened and 'ferror()' for the second.
    Devoted my life to programming...

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    35,952
    You'd better check you opened the file successfully as well, in case your break is because of a NULL assert check in fread.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jul 2017
    Posts
    30
    i change my code ...and wont trigger break point bt doesn't display the data i key in
    Code:
    void displayStaff() {
    
    
        FILE*fp1;
        fp1 = fopen("Record", "rb");
        printf("\n ID\t NAME\t POSITION\t SALARY\n");
        while (fread(&sta, sizeof(sta), 1, fp1))
            printf("%d\t\t%s\t%s\t%s", sta.id, sta.name, sta.position, sta.salary);
        fclose(fp1);
    }
    and i declare the id as int and other three as char

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,451
    Wait... How do you "key in" the data? Keep in mind, fread doesn't work like scanf. It doesn't parse the input for ints, floats, and other types. It just reads streams of bytes.
    Devoted my life to programming...

  6. #6
    Registered User
    Join Date
    Jul 2017
    Posts
    30
    i have have add staff function..in the display function i like the display the staff details i just add
    Code:
    void addStaff() {
    	char ans;
    	FILE*fp;
    	fp = fopen("Record", "ab");
    	
    	printf("Are you want to add new staff details?(Y/N)");
    	rewind(stdin);
    	scanf("%c", &ans);
    
    
    	while (toupper(ans) == 'Y')
    	{
    		printf("Staff Details\n");
    		printf("Staff ID:\n");
    		scanf("%d", &sta.id);
    		rewind(stdin);
    		printf("Staff name:\n");
    		scanf("%c", &sta.name);
    		rewind(stdin);
    		printf("Postion:\n");
    		scanf("%c", &sta.position);
    		rewind(stdin);
    		printf("Salary");
    		scanf("%c", &sta.salary);
    		rewind(stdin);
    
    
    		fwrite(&sta, sizeof(sta), 1, fp);
    		printf("New staff details store successfully\n");
    		printf("Are you want to add new staff details?(Y/N)");
    		rewind(stdin);
    		scanf("%c", &ans);
    	}
    	fclose(fp);
    }

  7. #7
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,451
    Are 'name', 'position' and 'salary' single characters or strings? Your functions seem to disagree about that...

    By the way, don't use 'rewind(stdin)', its behavior with non-file streams is implementation-defined. If you don't want to worry about stray newlines and such, use fgets together with sscanf.
    Devoted my life to programming...

  8. #8
    Registered User userxbw's Avatar
    Join Date
    Aug 2017
    Posts
    835
    just a small English grammar fix.
    Code:
     printf("New staff details store successfully\n"); 
    printf("Are you want to add new staff details?(Y/N)");
    
    //from present tense to past tense
     printf("New staff details stored successfully\n");
    
    //
     printf("Are you wanting to add new staff details?(Y/N)");
    
    // or 
    
    printf("Would you like to add more new staff details?(Y/N)");
    A computer is only as smart as the person using it.

  9. #9
    Registered User
    Join Date
    Jul 2017
    Posts
    30
    i already solve that..thank to you...i have another qusetion...
    Code:
    void deleteStaff() {
    	
    	char ans;
    	int id, found;
    	FILE*fpo, *temp1;
    
    
    		fpo = fopen("Report", "rb");
    		temp1 =("temp1", "wb");
    
    
    		printf("Enter the ID number you want to delete:");
    		scanf("%d", &id);
    
    
    		printf("Staff details:\n");
    		printf("ID\t NAME\n");
    		printf("============================");
    
    
    		while (fread(&sta, sizeof(sta), 1, fpo) !=0) {
    			if (id == sta.id) {
    				printf("%d\t\t%s\t%s\t%s]", sta.id, sta.name, sta.position, sta.salary);
    				found = 1;
    			}
    			else
    			{
    				fwrite(&sta, sizeof(sta), 1, temp1);
    				found = 0;
    			}
    		}
    
    
    		fclose(fpo);
    		fclose(temp1);
    		if(found == 1) {
    			printf("Are you sure to delete : ");
    			rewind(stdin);
    			scanf("%c", &ans);
    
    
    			if (toupper(ans) == 'Y')
    			{
    				system("delRecord");
    				system("rename temp1 Record");
    				printf("\nDeleted sucessfully....\n");
    			}
    			if (toupper(ans) != 'Y')
    				system("del temp1");
    		}
    }
    it will trigger the break point during the line while (fread(&sta, sizeof(sta), 1, fpo) !=0)
    i want to search the staff details and if founded, i want to delete it, but my code wont display the staff details.... can someone tell me where is the problem?

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    35,952
    > temp1 =("temp1", "wb");
    How does this open a file?

    > system("delRecord");
    Perhaps you need a space between "del" and "Record".

    > rewind(stdin);
    > scanf("%c", &ans);
    Stop using undefined actions like rewinding stdin.
    If you want to read the next non-whitespace character, use
    Code:
    scanf(" %c", &ans);  // note the leading space in the format string.
    Also, you really need to post your structure.
    > scanf("%c", &sta.salary);
    Because using %c to read in a salary makes no sense whatsoever.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    Jul 2017
    Posts
    30
    Code:
    void deleteStaff() {
    	
    	char ans;
    	int id, found;
    	FILE*fpo, *temp1;
    
    
    		fpo = fopen("Report", "rb");
    		temp1 =fopen("temp1", "wb");
    
    
    		printf("Enter the ID number you want to delete:");
    		scanf("%d", &id);
    
    
    		printf("Staff details:\n");
    		printf("ID\t NAME\n");
    		printf("============================");
    
    
    		while (fread(&sta, sizeof(sta), 1, fpo) !=0) {
    			if (id == sta.id) {
    				printf("%d\t\t%s\t%s\t%d", sta.id, sta.name, sta.position, sta.salary);
    				found = 1;
    			}
    			else
    			{
    				fwrite(&sta, sizeof(sta), 1, temp1);
    				found = 0;
    			}
    		}
    
    
    		fclose(fpo);
    		fclose(temp1);
    		if(found == 1) {
    			printf("Are you sure to delete : ");
    			
    			scanf(" %c", &ans);
    
    
    			if (toupper(ans) == 'Y')
    			{
    				system("del Record");
    				system("rename temp1 Record");
    				printf("\nDeleted sucessfully....\n");
    			}
    			if (toupper(ans) != 'Y')
    				system("del temp1");
    		}
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    void displayStaff() {
    
    
    	FILE*fp1;
    	fp1 = fopen("Record", "rb");
    	printf("\n ID\t NAME\t POSITION\t SALARY\n");
    	while (fread(&sta, sizeof(sta), 1, fp1))
    		printf("%d\t\t%s\t%s\t%d", sta.id, sta.name, sta.position, sta.salary);
    	fclose(fp1);
    }
    
    
    
    
    
    
    void searchStaff() {
    	int id, found;
    	struct staff sta;
    	FILE*fp2, *temp;
    
    
    	printf("Enter the ID number you want to search:\n ");
    	scanf("%d", &id);
    
    
    	fp2 = fopen("Record", "rb");
    	temp = fopen("temp", "wb");
    	printf("Staff Details\n");
    	printf("ID NUMBERS\t NAME\n");
    	printf("=========================");
    	while (fread(&sta, sizeof(sta), 1, fp2) != 0)
    	{
    		if (id == sta.id) {
    			printf("%d\t\t %s\n", sta.id, sta.name);
    			found = 1;
    
    
    		}
    		else {
    			fwrite(&sta, sizeof(sta), 1, temp);
    			found = 0;
    		}
    	}
    	fclose(fp2);
    	fclose(temp);
    	if (found == 1) 
    		system("Record");
    	}
    }
    whn i runthe display function, it wont displat the position ans name, gor delete function it trigger the break point when running the while line

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    35,952
    Ffs, post your struct!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,236
    Quote Originally Posted by Salem View Post
    Ffs, post your struct!
    It might be top secret

  14. #14
    Registered User
    Join Date
    Jul 2017
    Posts
    30
    Code:
    
    struct staff {
    	int id, password, salary;
    	char name[30], position[30];
    }sta;

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    35,952
    Are you still using %c to read the name and position in the first place? Post #6
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WSF-Staff
    By Elkvis in forum Tech Board
    Replies: 0
    Last Post: 06-19-2014, 09:17 AM
  2. technical staff
    By Kenan1975 in forum Windows Programming
    Replies: 1
    Last Post: 08-01-2010, 06:13 AM
  3. Creating a menu system
    By ICool in forum C Programming
    Replies: 9
    Last Post: 09-17-2007, 12:18 PM
  4. Looking for staff
    By Daniel Primed in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 01-19-2006, 07:17 PM
  5. Looking for an immortal staff and a builder or two
    By ~Kyo~ in forum Projects and Job Recruitment
    Replies: 1
    Last Post: 05-01-2005, 12:05 PM

Tags for this Thread