Thread: File I/O, fgets()? scanf()? I'm lost...

  1. #16
    Registered User
    Join Date
    Nov 2002
    Posts
    60
    Monster:
    my last_name array is 30 characters long, I assumed that would be enough to enter any kind of name!!

    Oh great, it's working!!! well, almost... :-/
    I get to enter and display the info, but obviously it's not being stored in the file, cause when I get out of the program and run it again, and I go to display it tells me "no match"!!
    Last edited by Lau; 11-22-2002 at 11:34 AM.

  2. #17
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Lau
    I get to enter and display the info, but obviously it's not being stored in the file, cause when I get out of the program and run it again, and I go to display it tells me "no match"!!
    Does the file have data in it after the first run of the program? IE can you open it in Notepad (or whatever)?

    >>Hammer: why don't I want to use scanf()?
    Because:
    - It only reads single words as strings. So, if you enter "Lau Is A C Coder", scanf() will only pick up the first word, being Lau. It does not read the complete line.
    - It has no bounds checking. If your input buffer is 20 characters long and the user enters a 30 character name, scanf() will blindly overwrite memory that extends past the end of array.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #18
    Registered User
    Join Date
    Nov 2002
    Posts
    60
    It does store in the file with the data I enter the first time. However, when I run the program a second time, it erases everything ... Oh, I use w+ when I open it, could that be the problem??

  4. #19
    Registered User
    Join Date
    Nov 2002
    Posts
    60
    You know, I changed the w+ mode to a r+, now I can see the content of my file stay in the file, but I can't access it anymore when I want to display an employee info, after I exit the program once !!!
    Even though that thing was due 2 days ago... I'm getting somewhere though with that thing!!

    I want to thank you for your help, Salem and Hammer

  5. #20
    Registered User
    Join Date
    Nov 2002
    Posts
    60
    I though that you couldn't use fseek() with text files???

  6. #21
    Registered User
    Join Date
    Nov 2002
    Posts
    7

    Smile an idea?

    Hi Lau...

  7. #22
    Registered User
    Join Date
    Nov 2002
    Posts
    491

    Re: an idea?

    Originally posted by Heidi
    Hi Lau...
    Who the hell is thi person and why are their posts always this worthless??

  8. #23
    Registered User
    Join Date
    Nov 2002
    Posts
    7

    try again!

    sorry hit the wrong key.....I agree with Hammer, do not use scanf. This function is not necessary in most cases. Take a look at this code....it is for a similiar program to your current project.
    Code:
    /*----------code---------------*/
    
    #include "stdafx.h"
    #include <stdio.h>
    #include <stdlib.h>
    #include "a:\CrtUtil.h"
    
    /* ---------------------------- */
    /*  DEFINE FUNCTION PROTOTYPES  */
    /* ---------------------------- */
    int HouseKeeping();
    void Process(void);
    void Heading(void);
    void EndJob(void);
    void Display(void);
    
    
    /* ------------------------------ */
    /*  DEFINE PAYROLL RECORD FORMAT  */
    /* ------------------------------ */
    
    typedef enum {Active, Inactive}Status;
    typedef enum {Hourly, Salaried, Exempt}Class;
    
    typedef struct
    
    { 
        Status  Status2;
        char  Soc_Sec[10];
        char  Last_Name[14];
        char  First_Name[12];
        char  Initial;
        char  Birth_Date[9];
        char  Sen_Date[9];
        int   Dept_Num;
        char  Sex;
        int   Citizen;
        int   Depend;
        float Pay;
        Class Employee_Class;
    
    } Payroll_Record_Type;
    
    /* ------------------------- */
    /*  DEFINE GLOBAL VARIABLES  */
    /* ------------------------- */
    
    FILE *Payroll_File;
    Payroll_Record_Type  Pay_Record;
    
    int Counter = 0;
    char Male;
    char Female;
    int Exempt_Counter = 0;
    int Salaried_Counter = 0;
    int Hourly_Counter = 0;
    
    
    /* ------------------------------ */
    /*  MAIN LINE PROCESSING ROUTINE  */
    /* ------------------------------ */
     void main()
    {
        HouseKeeping();
           while ( !feof(Payroll_File) )
               Process();
           EndJob();
    }
    
    
    /* ------------------------- */
    /*  HOUSEKEEPING SUBROUTINE  */
    /* ------------------------- */
    int HouseKeeping()
    {
    
    	textattr (0X1E);
    	clrscr();
    
    
        Payroll_File = fopen("Payroll.Dat", "rb");
        if (Payroll_File == NULL )
        {
            printf( "Cannot open payroll file...\n");
            exit( EXIT_FAILURE );
        }
        Heading();
        fread( &Pay_Record, sizeof(Pay_Record), 1, Payroll_File );
        Counter = 0;
    	return 0;
    }
    
    
    /* --------------------------- */
    /*  DETAIL PROCESSING ROUTINE  */
    /* --------------------------- */
    void Process()
    {
        if (Pay_Record.Status2 == Active )
        {
    		Display();
    	}
    		fread( &Pay_Record, sizeof(Pay_Record), 1, Payroll_File );
    }
       
    
    
    /* --------------------------- */
    /*  END OF JOB ROUTINE		   */
    /* --------------------------- */
    void EndJob()
    
    
    {
        printf("\nTOTAL NUMBER OF ACTIVE EMPLOYEES..: %d\n\n", Counter);
    	printf("EXEMPT:   %d      SALARIED:  %d  HOURLY:  %d\n", Exempt_Counter, Salaried_Counter, Hourly_Counter );
        fclose(Payroll_File );
    
    }
    
    
    
    
    /*----------------------------*/
    /*  HEADING ROUTINE           */
    /*----------------------------*/
    
    void Heading()
    {
    	printf("                             EMPLOYEE DISPLAY \n\n");
    	printf("SOC. SEC. DEPT  LAST NAME    FIRST NAME  I  SEX  DP  CIT  CLASS.   PAY      SEN. DATE\n\n");
    }
    
    
    
    
    
    /*----------------------------*/
    /*  DISPLAY ROUTINE           */
    /*----------------------------*/
    
    void Display()
    
    {
    
            printf("%.3s-%.2s-%4s ",
                Pay_Record.Soc_Sec,
                Pay_Record.Soc_Sec+3,
                Pay_Record.Soc_Sec+5 );
            
            printf("%.3d ", Pay_Record.Dept_Num);
            printf("%-13s ", Pay_Record.Last_Name);
            printf("%-11s ", Pay_Record.First_Name);
            printf("%c" , Pay_Record.Initial);
            printf("%3c", Pay_Record.Sex);
            printf("%4d", Pay_Record.Depend);
    		{
    
            if (Pay_Record.Citizen == 1)
                  printf( " Yes" );
            else 
                printf(" No ");
    		}   
            
    
    	switch (Pay_Record.Employee_Class)
    	{
    	case Hourly: printf("HRLY");
    		 Hourly_Counter++;
    		 break;
    
    	case Salaried: printf("SAL.");
    		 Salaried_Counter++;
    		 break;
    
    	case Exempt: printf("EXMT");
    		 Exempt_Counter++;
    		 break;
    
    	}
    
        printf("%9.2f", Pay_Record.Pay);
        printf("%4.2s/%.2s/%.4s",
                Pay_Record.Sen_Date + 4,
                Pay_Record.Sen_Date + 6,
                Pay_Record.Sen_Date);
    
            Counter = Counter + 1;
    
    
    
        }
    
        
    /*----------end oc code-------------*/
    Hope this sheds a little light!








    Code tags added by Hammer

  9. #24
    Registered User
    Join Date
    Nov 2002
    Posts
    7

    Wink reply to Orbitz

    Calmete Orbitz! A little slow tonight and "key-happy"! Hasta luego, amigo tonto!

  10. #25
    Registered User
    Join Date
    Nov 2002
    Posts
    491

    Re: reply to Orbitz

    Originally posted by Heidi
    Calmete Orbitz! A little slow tonight and "key-happy"! Hasta luego, amigo tonto!
    Well, just as long as you explain yourself i suppose don't have a problem with it


  11. #26
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Originally posted by Lau
    Monster:
    my last_name array is 30 characters long, I assumed that would be enough to enter any kind of name!!
    Murphy's Law

  12. #27
    Registered User
    Join Date
    Nov 2002
    Posts
    60
    I get it Monster

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  2. File I/O Question
    By Achy in forum C Programming
    Replies: 2
    Last Post: 11-18-2005, 12:09 AM
  3. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM