Thread: Programe error

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    6

    Programe error

    this is the my programme i can't seems to continue the programe
    after doing the 3 printf and the fprintf

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    #define N 50
    
    FILE *fpHrWorked;
    FILE *fpLatestEmp;
    FILE *fpPay;
    FILE *fpRead;
    
    struct data
    {
    	int EmpID;
    	char Name[30];
    	int Category;
    	float RatePerHr;
    	int HourWorked;
    }Employee[N];
    
    struct Hrworked
    {
    	int Emp_ID;
    	int HrWork;
    	int ClientID;
    }Worked[N];
    
    int main(void)
    {
    	int i=0;
    	int a=0;
    	int w=0;
    	int n=0;
    
    	fpHrWorked=fopen("HrWorked.dat","w");
    	fpLatestEmp=fopen("LatestEmp.dat","w");
    	fpPay=fopen("Pay.dat","w");
    	fpRead=fopen("temp.dat","r");
    
    		printf("\n\n\n");
    		printf("\t\tPlease Enter Employee ID(*No More Than 999*); ");
    		scanf("%d",&Worked[n].Emp_ID);
    		printf("\t\tPlease Enter Working Hour(*No More Than 999*); ");
    		scanf("%d",&Worked[n].HrWork);
    		printf("\t\tPlease Enter Client ID(*No More Than 9*); ");
    		scanf("%d",&Worked[n].ClientID);
    		n++;
    		fprintf(fpHrWorked,"%03d|%d|%d\n",Worked[i].Emp_ID,Worked[i].HrWork,Worked[i].ClientID);
    
    //error Start here onwards
    
    	system("rename EMP.dat Temp.dat");
    	n=0;
    	while(!feof(fpRead))
    	{
    		fscanf(fpRead,"%d|%30s|%d|%f|%d",&Employee[n].EmpID,Employee[n].Name,&Employee[n].Category,&Employee[n].RatePerHr,&Employee[n].HourWorked);
    		n++;
    	}
    	i=0;
    	while(!feof(fpHrWorked))
    	{
    		fscanf(fpHrWorked,"%d|%d|%d",&Worked[i].Emp_ID,&Worked[i].HrWork,&Worked[i].ClientID);
    		i++;
    	}
    	for(n=0;n<=i;n++)
    	{
    		for(a=0;a<=i;a++)
    		{
    			if(Employee[n].EmpID==Worked[a].Emp_ID)
    			{
    				Employee[n].HourWorked=Employee[n].HourWorked+Worked[i].HrWork;
    				fprintf(fpLatestEmp,"%d|%30s|%d|%f|%d",Employee[n].EmpID,Employee[n].Name,Employee[n].Category,Employee[n].RatePerHr,Employee[n].HourWorked);
    				fprintf(fpPay,"%d|%d|%s|%d|%d",Worked[i].ClientID,Employee[n].EmpID,Employee[n].Name,Employee[n].HourWorked,Employee[n].RatePerHr);
    				break;
    			}
    			else
    			{
    				fprintf(fpLatestEmp,"%d|%30s|%d|%f|%d",Employee[n].EmpID,Employee[n].Name,Employee[n].Category,Employee[n].RatePerHr,Employee[n].HourWorked);
    				fprintf(fpPay,"%d|%d|%s|%d|%d",Worked[i].ClientID,Employee[n].EmpID,Employee[n].Name,Employee[n].HourWorked,Employee[n].RatePerHr);
    			}
    		}
    	}
    	fclose(fpPay);
    	fclose(fpRead);
    	fclose(fpHrWorked);
    
    
    	system("delete temp.dat");
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2002
    Posts
    52
    Ok, here is the problem.

    You do know that C is case-sensitive. This means HeLLo is NOT the same as hello.

    First you have
    Code:
    fpRead=fopen("temp.dat","r");
    Then you use
    Code:
    system("rename EMP.dat Temp.dat");
    You need to change Temp.dat (notice the capital) to temp.dat.

    I noticed you don't use EMP.day there, you should make sure the file exists on your hard drive first. YOu can do this in the program by having something like this
    Code:
    FILE *oldemp;
    
    if((oldemp = fopen("EMP.dat", "r")) == NULL) {
        printf("Error, EMP.dat doesn't exist\n");
        return(1);
    }
    else
        fclose(oldtemp);
    Put the 'FILE *oldemp;' with all the other FILE* declarations. The if statement could go between where you pasted the comment and where you have system("rename ... ");, but can go anywhere inside the main body.

    Hope this has been some help
    - Daniel Wallace

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>i can't seems to continue the programe after doing the 3 printf and the fprintf
    Can you explain your question better please?

    >>fopen()
    Always check this function worked before trying to use file returned FILE*.

    >>You need to change Temp.dat (notice the capital) to temp.dat.
    Well, this depends on the OS, as the rename is done via system(). If you're on a windows platform, anything passed to system() will be case insensative, IIRC. Anyway, calling system to rename a file is not good, try using the rename() function that's in stdlib.h
    And on this point, the file you're renaming TO, has just been fopen()'d (ignoring case), so that maybe a problem.

    >>while(!feof(fpRead))
    This is not the way to control the loop. Use the return code from the reading function (fscanf() in this case).
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Dec 2002
    Posts
    6
    i keep getting error after performing the printf("please enter ur employeee id") and so on, i did have the file needed by creating it myself.so i dunno why i'm having error after peforming the 3 first printf and than the fprintf

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>i keep getting error
    My crystal ball doesn't see errors in other peoples code, only my own, so maybe you'd better tell us what the "error" actually is. That way I don't need to second guess you.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User
    Join Date
    Dec 2002
    Posts
    6
    okies now its say duplicate file name or file in use and i change abit of the coding already so it like this right now:

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    #define N 50
    
    FILE *fpHrWorked;
    FILE *fpLatestEmp;
    FILE *fpPay;
    FILE *fpRead;
    
    struct data
    {
    	int EmpID;
    	char Name[30];
    	int Category;
    	float RatePerHr;
    	int HourWorked;
    }Employee[N];
    
    struct Hrworked
    {
    	int Emp_ID;
    	int HrWork;
    	int ClientID;
    }Worked[N];
    
    int main(void)
    {
    	int i=0;
    	int a=0;
    	int w=0;
    	int n=0;
    
    	fpHrWorked=fopen("HrWorked.dat","w");
    	fpLatestEmp=fopen("LatestEmp.dat","w");
    	fpPay=fopen("Pay.dat","w");
    	fpRead=fopen("EMP.dat","r");
    
    	if((fpRead = fopen("EMP.dat", "r")) == NULL)
    	{
        printf("Error, EMP.dat doesn't exist\n");
        return(1);
    	}
    
    else
    {
        fclose(fpRead);
    
    
    		printf("\n\n\n");
    		printf("\t\tPlease Enter Employee ID(*No More Than 999*); ");
    		scanf("%d",&Worked[n].Emp_ID);
    		printf("\t\tPlease Enter Working Hour(*No More Than 999*); ");
    		scanf("%d",&Worked[n].HrWork);
    		printf("\t\tPlease Enter Client ID(*No More Than 9*); ");
    		scanf("%d",&Worked[n].ClientID);
    		n++;
    		fprintf(fpHrWorked,"%03d|%d|%d\n",Worked[i].Emp_ID,Worked[i].HrWork,Worked[i].ClientID);
    
    
    	system("rename EMP.dat Temp.dat");
    	n=0;
    	while(!feof(fpRead))
    	{
    		fscanf(fpRead,"%d|%30s|%d|%f|%d",&Employee[n].EmpID,Employee[n].Name,&Employee[n].Category,&Employee[n].RatePerHr,&Employee[n].HourWorked);
    		n++;
    	}
    	i=0;
    	while(!feof(fpHrWorked))
    	{
    		fscanf(fpHrWorked,"%d|%d|%d",&Worked[i].Emp_ID,&Worked[i].HrWork,&Worked[i].ClientID);
    		i++;
    	}
    	for(n=0;n<=i;n++)
    	{
    		for(a=0;a<=i;a++)
    		{
    			if(Employee[n].EmpID==Worked[a].Emp_ID)
    			{
    				Employee[n].HourWorked=Employee[n].HourWorked+Worked[i].HrWork;
    				fprintf(fpLatestEmp,"%d|%30s|%d|%f|%d",Employee[n].EmpID,Employee[n].Name,Employee[n].Category,Employee[n].RatePerHr,Employee[n].HourWorked);
    				fprintf(fpPay,"%d|%d|%s|%d|%d",Worked[i].ClientID,Employee[n].EmpID,Employee[n].Name,Employee[n].HourWorked,Employee[n].RatePerHr);
    				break;
    			}
    			else
    			{
    				fprintf(fpLatestEmp,"%d|%30s|%d|%f|%d",Employee[n].EmpID,Employee[n].Name,Employee[n].Category,Employee[n].RatePerHr,Employee[n].HourWorked);
    				fprintf(fpPay,"%d|%d|%s|%d|%d",Worked[i].ClientID,Employee[n].EmpID,Employee[n].Name,Employee[n].HourWorked,Employee[n].RatePerHr);
    			}
    		}
    	}
    }
    	fclose(fpPay);
    	fclose(fpRead);
    	fclose(fpHrWorked);
    
    
    	system("delete Temp.dat");
    	return 0;
    }

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    okies now its say duplicate file name or file in use and i change abit of the coding already so it like this right now:
    Asked, and answered:

    And on this point, the file you're renaming TO, has just been fopen()'d (ignoring case), so that maybe a problem.
    You should listen to Hammer.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Dec 2002
    Posts
    6
    okies sorry didn't understand so now i remove the system(rename Emp.dat Temp.dat) and when i run it, the program just hang & do nothing after this.example of output:

    Employee Id:1
    Hour Worked:1
    Client Id:1

    nothing happen after those 3 and i dun think the programme have ended yet its just stuck at some place
    Last edited by colincsf; 12-26-2002 at 10:36 PM.

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Did you check that the fopen()'s actually worked? Maybe you're looping on a bad file stream.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM