Thread: Printing words to console screen

  1. #16
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I knocked off a couple or so struct members, and changed the print out a bit, but you get the idea. Contents of the payroll.txt file, is at the bottom of the program.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    struct employee1 {
        int num; 
        char first_name[8];
        char last_name[8];
        int id_number; 
        int year;
        double pay_rate;
        double sthours; 
        double ot_hour;
        double double_hour;
        double gross_pay;
    };     
        
    
    
    int main() {
      int i, n ; 
      struct employee1 emp; 
      char junk[200];
      FILE *writeptr = fopen("payroll.txt", "rt");
      fgets(junk,200, writeptr);
    
    		//printf("Enter record number to be edited\n");
    		//scanf("%d", &employee.num);
    
    		//moving file pointer to position of record user requested
    //		fseek(writeptr, (employee.num-1) * sizeof(struct employee_data), SEEK_CUR);
    		
    		fscanf(writeptr, "%d %s %s %d %lf %d %lf %lf %lf %lf" , &emp.num, 
        emp.first_name, emp.last_name, &emp.id_number, &emp.pay_rate, &emp.year, 
        &emp.sthours, &emp.ot_hour, &emp.double_hour, &emp.gross_pay);
    		
    		printf("\n Number: %6d  First Name: %-15s  Last Name: %-15s \
        \n ID No: %-8d Pay Rate: %-10.2lf         Year: %-10d \
        \n ST Hours: %-.2lf  1.5 Hours: %-.2lf              2x hours: %-.2lf  Gross Pay: %-.2lf ", 
        emp.num, emp.first_name, emp.last_name, emp.id_number, emp.pay_rate, 
        emp.year, emp.sthours, emp.ot_hour, emp.double_hour, emp.gross_pay);
    
            
    
      printf("\n\n\t\t\t     press enter when ready");
    
      fclose(writeptr);
      getchar();
      return 0;
    }
    /*
    Contents of the payroll.txt file:
    
    number    First name     Last name      id no.      pay rate  year      sthours     1.5 hours   2x hours    
    
    Gross pay        
    1         p              w              1           1.00        1       1.00        1.00        1.00        
    
    4.50
    */

  2. #17
    Registered User
    Join Date
    Apr 2010
    Posts
    21
    Adak, Thank you very much

    just a few questions then i am good on my own i think
    1) why was string.h needed to be included in this example?
    2)in your example why was the variable junk and fgets used? i see that it is affects writeptr, does it allow the writeptr the capacity to print the strings to the screen?
    Last edited by Shadow20; 04-08-2010 at 06:48 AM.

  3. #18
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    It isn't necessary.

    fgets pulls off the header line, but needs a char array to hold it. "junk" seemed like a good name, since I knew I wouldn't be using it later (it was too wide for my editor to fit in one screen). It just gets rid of the entire header line. If you use fscanf(), it will pull off only enough letters until it hits the blank space between words. Then fscanf() stops. (and it doesn't restart itself or look for other data further on). You could have an "%s" for every word in the header, but there were many, and I didn't want to do that, at that point.

    Printing to the screen is independent of how you input the data. You don't have to use fgets(), at all. You just have to use "something" so the data can be reached. Both fgets() and fscanf() need the file pointer, but the file pointer has nothing to say about output to the screen (although the screen is a file stream in C, it is handled by it's own built-in file pointer).

  4. #19
    Registered User
    Join Date
    Apr 2010
    Posts
    21
    I think i got what you meant with that explanation. I finally got my program to work how i want it to atlast, it doesnt seem to happen very often
    You are the man!
    thanks for your help and time.

  5. #20
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Thanks for the kind words, and just stop by when you have problems with your code. I know I've learned a lot here.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. no. printing in words
    By rits in forum C Programming
    Replies: 2
    Last Post: 09-09-2009, 10:11 AM
  2. Faster way of printing to the screen
    By cacophonix in forum C Programming
    Replies: 16
    Last Post: 02-04-2009, 01:18 PM
  3. Beginners Contest #2 For those who wanted more!!
    By ILoveVectors in forum Contests Board
    Replies: 16
    Last Post: 08-12-2005, 12:03 AM
  4. question about printing on screen and not overwriting text...
    By revelation437 in forum C Programming
    Replies: 2
    Last Post: 12-14-2002, 02:48 PM
  5. Console Screen Resolution!!!!
    By Perica in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2002, 07:45 AM