C programming session 09

1,694 views

Published on

Published in: Education, Technology, Business
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,694
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
73
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • Begin the session by explaining the objectives of the session.
  • Discuss the need for structures. Compare structure with an array.
  • Use this slide to test the student’s understanding on defining structures.
  • Discuss the advantages of using a label in defining a structure. Tell the students that a structure variable can be directly assigned to another structure variable of the same type. You need not to copy elements individually.
  • Use this slide to test the student’s understanding on defining structures.
  • Tell the students that structures can be passed to other functions. The structures can be passed either by value or by reference.
  • Use this slide to test the student’s understanding on passing structures to functions.
  • Use this slide to test the student’s understanding on array of structures.
  • Tell the students the way to access the elements of a structure, which is contained in another structure. Also, discuss the use of the typedef statement.
  • Use this slide to test the student’s understanding on typedef statement.
  • Use this slide to test the student’s understanding on the fwrite() function.
  • Tell the students that they can use the feof() function to check for the end of file. The feof () function does not report end-of-file unless we try to read past the last character of the file. Consider the following code. while (!feof (fp)) { fread (&buf, sizeof (struct buf) , 1 , fp) ; printf (…) ; /* print data from structure */ } Once the last record is read , the feof () function does not return a non-zero value. It senses the end-of-file only after the next read which fails. The buffer buf would still contain the contents of the last record which will be printed again. An alternative would be: while (1) { fread (&buf , sizeof (struct buf) , 1 , fp) ; if (! feof ()) printf (…) ; /* print data from structure */ }
  • Use this slide to test the student’s understanding on the fwrite() and fread() function.
  • Use this slide to test the student’s understanding on reading and writing structures in a file.
  • Use this and the next slide to summarize the session.
  • C programming session 09

    1. 1. <ul><li>In this session, you will learn to: </li></ul><ul><ul><li>Work with structures </li></ul></ul><ul><ul><li>Use structures in file handling </li></ul></ul>Objectives
    2. 2. <ul><li>Structures: </li></ul><ul><ul><li>Are collection of heterogeneous data types. </li></ul></ul><ul><ul><li>Are also known as records. </li></ul></ul><ul><ul><li>Are used to define new data types. </li></ul></ul><ul><ul><li>Are defined using the struct keyword. </li></ul></ul>Working with Structures
    3. 3. <ul><li>A structure is defined by using the struct keyword. </li></ul><ul><li>Consider the following example: </li></ul><ul><ul><li>struct { </li></ul></ul><ul><ul><li>char transno [4]; </li></ul></ul><ul><ul><li>int salesno; </li></ul></ul><ul><ul><li>int prodno; </li></ul></ul><ul><ul><li>int unit_sold; </li></ul></ul><ul><ul><li>float value_of_sale; </li></ul></ul><ul><ul><li>} salesrec; </li></ul></ul><ul><ul><li>All the variables in the record are treated as one data structure – salesrec . </li></ul></ul>Defining Structures
    4. 4. <ul><li>State whether True or False: </li></ul><ul><ul><li>The members of a structure must be of the same data type. </li></ul></ul><ul><li>a. Give the declaration for a structure called date with the following members. </li></ul><ul><ul><li>day (2 digits) month (2 digits) year (4 digits) </li></ul></ul><ul><li>b. Give appropriate statements to accept values into the members of the structure date and then print out the date as mm/dd/yyyy. </li></ul>Practice: 7.1
    5. 5. <ul><li>Solution: </li></ul><ul><ul><li>False </li></ul></ul><ul><ul><li>a. The structure declaration should be: </li></ul></ul><ul><ul><li>struct { </li></ul></ul><ul><ul><li>int day; </li></ul></ul><ul><ul><li>int month; </li></ul></ul><ul><ul><li> int year; </li></ul></ul><ul><ul><li> } date; </li></ul></ul><ul><ul><li>b. The statements could be: </li></ul></ul><ul><ul><li>scanf(“%d%d%d”, &date, &date.month, &date.year); </li></ul></ul><ul><ul><li>printf(“%d/%d/5d”, date.month, date.day, date.year); </li></ul></ul>Practice: 7.1 (Contd.)
    6. 6. <ul><li>Defining a label structures: </li></ul><ul><ul><li>Structure label may be declared as: </li></ul></ul><ul><ul><li>struct salesdata { </li></ul></ul><ul><ul><li>char transno [4]; </li></ul></ul><ul><ul><li>int salesno; </li></ul></ul><ul><ul><li>int prodno; </li></ul></ul><ul><ul><li>int unit_sold; </li></ul></ul><ul><ul><li>float value_of-sale; </li></ul></ul><ul><ul><li>}; </li></ul></ul><ul><ul><li>struct salesdata salesrec; </li></ul></ul><ul><ul><li>Here, salesdata is the label and salesrec is the data item. </li></ul></ul>Defining Structures (Contd.)
    7. 7. <ul><li>Given the following declarations: </li></ul><ul><li>struct date_type{ struct { </li></ul><ul><li>int day; int day; int month; int month; </li></ul><ul><li>int year; int year; </li></ul><ul><li> }; } date; </li></ul><ul><li>Declaration 1 Declaration 2 </li></ul><ul><li> Answer the following questions: </li></ul><ul><ul><li>Memory is allocated for the structure ( date_type/ date ). </li></ul></ul><ul><ul><li>Which of the following is/are the correct way(s) of referring to the variable day? </li></ul></ul><ul><ul><ul><li>a. day.date </li></ul></ul></ul><ul><ul><ul><li>b. date_type.day </li></ul></ul></ul>Practice: 7.2
    8. 8. <ul><ul><li>What change(s) should be made to the first declaration so that the structure date is created of type date_type ? </li></ul></ul><ul><ul><li>Is the following statement valid in case of the second declaration? If not, why? </li></ul></ul><ul><ul><li>struct date another_date; </li></ul></ul>Practice: 7.2 (Contd.)
    9. 9. <ul><li>Solution: </li></ul><ul><ul><li>date (date_type is only a structure type) </li></ul></ul><ul><ul><li>a. Invalid because the structure name precedes the variable name. </li></ul></ul><ul><ul><li>b. Invalid because date_type is not actually created in memory, it is only a label. </li></ul></ul><ul><ul><li>3. The following statement should be added after the struct declaration: </li></ul></ul><ul><ul><li>struct date_type date; </li></ul></ul><ul><ul><li>4. This is invalid because date is not a structure type but an actual structure in memory. </li></ul></ul>Practice: 7.2 (Contd.)
    10. 10. <ul><li>Passing Structures to Functions: </li></ul><ul><ul><li>Structures may be passed to functions either by value or by reference. </li></ul></ul><ul><ul><li>Usually methods pass the address of the structure. </li></ul></ul><ul><ul><li>The name of the variable being referenced is preceded by the symbol  . </li></ul></ul>Passing Structures to Functions
    11. 11. <ul><li>Consider the following code: </li></ul><ul><li>struct date_type { </li></ul><ul><li>int day; </li></ul><ul><li>int month; </li></ul><ul><li>int year; </li></ul><ul><li>}; struct date_type date, *ptr; </li></ul><ul><ul><li>How can the pointer variable ptr be assigned the address of the structure date ? </li></ul></ul><ul><ul><li>Is the following statement valid? </li></ul></ul><ul><ul><li>ptr = &date_type; </li></ul></ul><ul><ul><li>c. Give alternative ways of referring to: </li></ul></ul><ul><ul><li>i. &date.day </li></ul></ul><ul><ul><li>ii. date.month </li></ul></ul><ul><li>Given that ptr has been assigned the address of the structure date. </li></ul>Practice: 7.3
    12. 12. <ul><li>2. Consider the incomplete code of a program that is given in the following file. The code uses a function called printmonth() that displays the month name corresponding to any month number. The month number is accepted into the member month of the structure date . The blanks have to be filled in appropriately. </li></ul>Practice: 7.3 (Contd.)
    13. 13. <ul><li>Solution: </li></ul><ul><ul><li>a. By using the following statement: </li></ul></ul><ul><ul><li> ptr = &date ; </li></ul></ul><ul><ul><li>b. No. because date_type is not created in memory; it is only a label. </li></ul></ul><ul><ul><li>c. i. &(ptr-> date) </li></ul></ul><ul><ul><li> ii. ptr-> month </li></ul></ul><ul><ul><li>2. The statement to invoke printmonth() could be: </li></ul></ul><ul><ul><li>printmonth(&date); /*invoke printmonth() by passing structure */ </li></ul></ul><ul><ul><li>The missing lines in the code for printmonth() are: </li></ul></ul><ul><ul><li>printmonth(point) </li></ul></ul><ul><ul><li>struct date_type *point; </li></ul></ul><ul><ul><li>point is the parameter of the function since it is used within the function to access members of the structure date . </li></ul></ul>Practice: 7.3 (Contd.)
    14. 14. <ul><li>Arrays of structures can also be created. </li></ul><ul><li>It works in the same way as any other data type array. </li></ul><ul><li>Consider the following example: </li></ul><ul><ul><li>struct prod data{ </li></ul></ul><ul><ul><li>char prodname[8]; int no_of_sales; </li></ul></ul><ul><ul><li>float tot_sale; </li></ul></ul><ul><ul><li>}; </li></ul></ul><ul><ul><li>An array for the preceding structure can be declared as: </li></ul></ul><ul><ul><li>struct proddata prod_field[4]; </li></ul></ul><ul><ul><li>The elements of the structure can be accessed as: </li></ul></ul><ul><ul><li>prod_field [0].prodnam[0]; </li></ul></ul>Arrays of Structures
    15. 15. <ul><li>Declare a structure which will contain the following data for 3 employees: </li></ul><ul><li>Employee code (3 characters) </li></ul><ul><li>First name (20 characters) </li></ul><ul><li>Middle initial (1 character) </li></ul><ul><li>Last name (20 characters) </li></ul><ul><li>The employee codes to be stored in this structure are E01 , E02 , and E03 . </li></ul><ul><li>2. Write the code to input for all 3 employees, and print out the initials of each (e.g. Ashraf A Kumar would be printed as AAK) along with their codes. </li></ul>Practice: 7.4
    16. 16. <ul><li>Solution: </li></ul>Practice: 7.4 (Contd.)
    17. 17. <ul><li>A structure can be used as a valid data type within another structure. For example, if date has been defined as: </li></ul><ul><ul><li>struct date{ </li></ul></ul><ul><ul><ul><li>int dd; </li></ul></ul></ul><ul><ul><ul><li>int mm; </li></ul></ul></ul><ul><ul><ul><li>int yy; </li></ul></ul></ul><ul><ul><ul><li>}; </li></ul></ul></ul><ul><li>The date structure can be used in another structure as: </li></ul><ul><ul><li>struct trans_rec{ </li></ul></ul><ul><ul><li> char transno[4]; </li></ul></ul><ul><ul><li> char type; </li></ul></ul><ul><ul><li> float amount; </li></ul></ul><ul><ul><li> struct date tran_date; </li></ul></ul><ul><ul><li> }; </li></ul></ul>Working with Structures (Contd.)
    18. 18. <ul><li>What will the following declaration do? </li></ul><ul><ul><li>typedef char sentence[50]; </li></ul></ul><ul><ul><li>sentence complex[10]; </li></ul></ul>Practice: 7.5
    19. 19. <ul><li>Solution: </li></ul><ul><ul><li>1. The first statement defines sentence as a data type consisting of an array of 50 characters. The second statement declares complex as a two-dimensional array, (an array of ten arrays of 50 characters each). </li></ul></ul>Practice: 7.5 (Contd.)
    20. 20. <ul><li>To store data permanently, it needs to be stored in a file. </li></ul><ul><li>Mostly, the data, to be written in the file, is a logical group of information i.e. records. </li></ul><ul><li>These records can be stored in structure variables. Hence, you need to write structures onto a file. </li></ul>Using Structures in File Handling
    21. 21. <ul><li>The fwrite() function is used to write structures onto a file. </li></ul><ul><li>The f write() function has the following syntax: </li></ul><ul><li>fwrite (constant pointer, sizeof (datatype), 1, FILE pointer); </li></ul><ul><ul><li>The first parameter is a pointer to the data to be written. </li></ul></ul><ul><ul><li>The second parameter is the size of data to be written. </li></ul></ul><ul><ul><li>The third parameter is the number of objects or data to be written. </li></ul></ul><ul><ul><li>The fourth parameter is the pointer to file. </li></ul></ul>Writing Records onto a File Using Structures
    22. 22. <ul><li>Now that various assets of the transaction data entry program have been explained, these have to be consolidated and the entire program coded. The problem statement is repeated below. </li></ul><ul><li>A transaction data entry program called dataent , used at the familiar Alcatel Automatics Company, has to be coded. The transaction file stores the data on transactions made by the salesmen of the company. The records consist of the following fields. </li></ul><ul><ul><li>Transaction number </li></ul></ul><ul><ul><li>Salesman number </li></ul></ul><ul><ul><li>Product number (numbered 1 to 4) </li></ul></ul><ul><ul><li>Units sold </li></ul></ul><ul><ul><li>Value of sale </li></ul></ul><ul><ul><li>Value of sale is calculated in the program. </li></ul></ul>Practice: 7.6
    23. 23. <ul><li>The program should allow the user to indicate when he wants to stop data entry (i.e. it should keep accepting records until the user indicates that there are no more records). </li></ul><ul><li>After all records have been entered, a report on the total number of sales and the total sale value for each product is to be printed in the following format (for each product). </li></ul><ul><li> Product number : ___________________ </li></ul><ul><li> Product name : ___________________ </li></ul><ul><li> Total number of sales : ___________________ </li></ul><ul><li> Total sale value : ___________________ </li></ul><ul><li>Use the structures salesrec , salesdata , prodata , and prod_field defined earlier and code for the report printing within main() . Also use the code provided on page 7.2 and 7.3 in your solution. </li></ul>Practice: 7.6 (Contd.)
    24. 24. <ul><li>Solution: </li></ul>Practice: 7.6 (Contd.)
    25. 25. <ul><li>The fread() function is used to read data from a stream. </li></ul><ul><li>The fread() function has the following syntax: </li></ul><ul><li> fread (ptr, sizeof, 1, fp); </li></ul><ul><ul><li>The first parameter is a pointer to the variable where the data is to be fetched. </li></ul></ul><ul><ul><li>The second parameter is the size of data to be read. </li></ul></ul><ul><ul><li>The third parameter is the number of objects or data to be read. </li></ul></ul><ul><ul><li>The fourth parameter is the pointer to file. </li></ul></ul>Reading Records from Files Using Structures
    26. 26. <ul><li>Is the following statement to read the first 5 records of the file trans.dat valid? </li></ul><ul><li>fread (ptr, (sizeof(salesrec) *5), 1, fp); </li></ul><ul><li>If not state why and give the correct statement. No checks are to be done for an unsuccessful read. </li></ul><ul><li>2. Modify the above fread() statement to include an end-of-file check and also check whether the records have been read successfully. In case of end-of-file, display the message: </li></ul><ul><ul><li>End-of-file encountered </li></ul></ul><ul><li>and in case of other errors, display the message and exit: </li></ul><ul><ul><li>Unsuccessful read </li></ul></ul><ul><li>In case of a successful read, display the salesman number and transaction number of each record. Give all the structure declarations required. </li></ul>Practice: 7.7
    27. 27. <ul><li>Solution: </li></ul>Practice: 7.7 (Contd.)
    28. 28. <ul><li>Debug the following program called dat.c using the error listing provided in the following file. </li></ul>Practice: 7.8
    29. 29. <ul><li>Solution: </li></ul><ul><ul><li>The solution to this practice will be discussed in class. Work out your answer. </li></ul></ul>Practice: 7.8 (Contd.)
    30. 30. <ul><li>In this session, you learned that: </li></ul><ul><ul><li>Records can be defined in C by using structures. </li></ul></ul><ul><ul><li>Structure members can be of the same/different data type. </li></ul></ul><ul><ul><li>Memory is not reserved when a structure label is declared. A structure is created when it is declared as a struct of the same type as the structure label. </li></ul></ul><ul><ul><li>A member of a structure can be accessed as follows: </li></ul></ul><ul><ul><ul><li>structure-name.member-name </li></ul></ul></ul><ul><ul><li>A pointer to a structure can be used to pass a structure to a function. Using pointers, the structure members are accessed as follows: </li></ul></ul><ul><ul><ul><li>pointer-name->member-name </li></ul></ul></ul><ul><ul><li>Arrays of structures can be defined and initialized (if global or static). To access any member, an index has to be used after the structure name, as follows: </li></ul></ul><ul><ul><li>structure-name [index ].member-name </li></ul></ul>Summary
    31. 31. <ul><ul><li>The typedef statement can assign names to user-defined data types. These are treated the same way as data types provided by C. </li></ul></ul><ul><ul><li>The fread() function can read records from a file into a structure/array of structures. The format of the function is: </li></ul></ul><ul><ul><ul><li>fread (pointer, size of structure, number of objects to be read, file pointer); </li></ul></ul></ul><ul><ul><li>The fread() function returns the number of objects read from the file. It does not return any special value in case of end-of-file. The feof() function is used in conjunction with fread() to check for end-of-file. </li></ul></ul><ul><ul><li>The fwrite() function can write a structure array of structures onto a file. All numeric data is written in compressed form. Usually, fread() and fwrite() are used in conjunction. </li></ul></ul>Summary (Contd.)

    ×