Thread: Help pls

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

    Help pls

    how am i going to copy the hrworked.dat and save it into a new file called pay.dat

    Code:
    #include <stdio.h>
    #include<stdlib.h>
    
    void main()
    {
    	int employee_id;
    	int hrworked;
    	int client_id;
    	FILE *fpEmp;
    
    
    	fpEmp=fopen("Hrworked.dat","w");
    
    	printf("Employee Id:");
    	scanf("%d",&employee_id);
    	if(employee_id>0 && employee_id>1000)
    	{
    		printf("Invalid id\n\n");
    		printf("Please Key in again from the start");
    		main();
    	}
                    printf("\nHour Worked:");
    	scanf("%d",&hrworked);
    	if(hrworked>=1000)
    	{
    		printf("\nInvalid Working hour\n\n");
    		printf("Please key in from the start");
    		main();
    	}
                    printf("Client Id:");
    	scanf("%d",&client_id);
    	if(client_id>=6)
    	{
    		printf("Please key in only 1-5");
    		printf("Please key in from the start");
    		main();
    	}
    
    		
    			fprintf(fpEmp,"%d",employee_id);
    			fprintf(fpEmp,"|%d\n",hrworked);
    			fprintf(fpEmp,"|%d\n",client_id);
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    The simplest way to copy two files is to read a byte from one and write it to the other until there's nothing left.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void panic ( void )
    {
      fprintf ( stderr, "Something went wrong\n" );
      exit ( EXIT_FAILURE );
    }
    
    int main ( void )
    {
      int byte;
      FILE *in, *out;
    
      in = fopen ( "hrworked.dat", "r" );
      out = fopen ( "pay.dat", "w" );
    
      if ( in == NULL )
        panic();
      else if ( out == NULL ) {
        fclose ( in );
        panic();
      }
    
      while ( ( byte = fgetc ( in ) ) != EOF )
        fputc ( byte, out );
    
      fclose ( in );
      fclose ( out );
    
      return 0;
    }
    >void main()
    No, main returns int. Anything else is undefined, which is very bad.

    >fpEmp=fopen("Hrworked.dat","w");
    Did this call succeed? How do you know? Check the return value.

    >scanf("%d",&employee_id);
    Always check the return value of scanf as well, otherwise it's incredibly unsafe.

    >if(employee_id>0 && employee_id>1000)
    Somehow I get the feeling that the second test should be

    employee_id < 1000

    >main();
    Never, ever, ever, ever, ever, ever, ever call main recursively unless it's an IOCCC entry.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Dec 2002
    Posts
    6
    Thx's i try it now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic port scanner code .. pls help ???
    By intruder in forum C Programming
    Replies: 18
    Last Post: 03-13-2003, 08:47 AM
  2. i dont know what to do. pls. help!!!!
    By Unregistered in forum C++ Programming
    Replies: 14
    Last Post: 03-14-2002, 03:24 PM
  3. help me pls..... :(
    By mocha_frap024 in forum C++ Programming
    Replies: 2
    Last Post: 02-22-2002, 10:46 AM
  4. pls help me!!
    By hanseler in forum C++ Programming
    Replies: 1
    Last Post: 12-05-2001, 08:46 PM
  5. Pls Help Me In This Question!!!
    By Joanna in forum Windows Programming
    Replies: 1
    Last Post: 10-20-2001, 02:05 PM