Structures and File Handling, Study notes of C programming

Topics covered are about file handling operations in C

Typology: Study notes

2020/2021

Uploaded on 12/22/2021

gurgeet-sandhu
gurgeet-sandhu 🇮🇳

3 documents

1 / 32

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20

Partial preview of the text

Download Structures and File Handling and more Study notes C programming in PDF only on Docsity!

Why use structure? In C, there are cases where we need to store multiple attributes of an entity. It is not necessary that an entity has all the information of one type only. It can have different attributes of different data types. For example, an entity Student may have its name (string), roll number (int), marks (float). To store such type of information regarding an entity student, we have the following approaches:

  • Construct individual arrays for storing names, roll numbers, and marks.
  • Use a special data structure to store the collection of different data types. What is Structure Structure in c is a user-defined data type that enables us to store the collection of different data types. Each element of a structure is called a member. Structures ca; simulate the use of classes and templates as it can store various information The ,struct keyword is used to define the structure. Let's see the syntax to define the structure in c. struct structure_name { data_type member1; data_type member2; . . data_type memeberN; };

Here, struct is the keyword; employee is the name of the structure; id, name, and salary are the members or fields of the structure. Let's understand it by the diagram given below:

Declaring structure variable We can declare a variable for the structure so that we can access the member of the structure easily. There are two ways to declare structure variable: By struct keyword within main() function By declaring a variable at the time of defining the structure. 1st way: Let's see the example to declare the structure variable by struct keyword. It should be declared within the main function. struct employee { int id; char name[50]; float salary; }; Now write given code inside the main() function. struct employee e1, e2; The variables e1 and e2 can be used to access the values stored in the structure. Here, e1 and e2 can be treated in the same way as the objects in C++ and Java.

C Structure example Let's see a simple example of structure in C language. #include<stdio.h> #include <string.h> struct employee { int id; char name[50]; }e1; //declaring e1 variable for structure int main( ) { //store first employee information e1.id=101; strcpy(e1.name, “David");//copying string into char array //printing first employee information printf( "employee 1 id : %d\n", e1.id); printf( "employee 1 name : %s\n", e1.name); return 0; } Output: employee 1 id : 101 employee 1 name : David

C Union Like structure, Union in c language is a user-defined data type that is used to store the different type of elements. At once, only one member of the union can occupy the memory. In other words, we can say that the size of the union in any instance is equal to the size of its largest element. Advantage of union over structure It occupies less memory because it occupies the size of the largest member only.

C Union example Let's see a simple example of union in C language. #include <stdio.h> #include <string.h> union employee { int id; char name[50]; }e1; //declaring e1 variable for union int main( ) { //store first employee information e1.id=101; strcpy(e1.name, "Sonoo Jaiswal");//copying string into char array //printing first employee information printf( "employee 1 id : %d\n", e1.id); printf( "employee 1 name : %s\n", e1.name); return 0; } Output: employee 1 id : 1869508435 employee 1 name : Sonoo Jaiswal

Comparison of Structure with Union

C Pointers The pointer in C language is a variable which stores the address of another variable. This variable can be of type int, char, array, function, or any other pointer. The size of the pointer depends on the architecture. However, in 32-bit architecture the size of a pointer is 2 byte. Consider the following example to define a pointer which stores the address of an integer. int n = 10; int* p = &n; // Variable p of type pointer is pointing to the address of the variable n of type integer. Declaring a pointer The pointer in c language can be declared using * (asterisk symbol). It is also known as indirection pointer used to dereference a pointer. int *a;//pointer to int char *c;//pointer to char

Pointer Example An example of using pointers to print the address and value is given below. As you can see in the above figure, pointer variable stores the address of number variable, i.e., fff4. The value of number variable is 50. But the address of pointer variable p is aaa3. By the help of * (indirection operator), we can print the value of pointer variable p.

Advantage of pointer

  1. Pointer reduces the code and improves the performance, it is used to retrieving strings, trees, etc. and used with arrays, structures, and functions.
  2. We can return multiple values from a function using the pointer.
  3. It makes you able to access any memory location in the computer's memory. Usage of pointer There are many applications of pointers in c language.
  4. Dynamic memory allocation In c language, we can dynamically allocate memory using malloc() and calloc() functions where the pointer is used.
  5. Arrays, Functions, and Structures Pointers in c language are widely used in arrays, functions, and structures. It reduces the code and improves the performance.

Pointer to array int arr[10]; int *p[10]=&arr; // Variable p of type pointer is pointing to the address of an integer array arr. #include <stdio.h> int main( ) { int *p,i; int val[7] = { 11, 22, 33, 44, 55, 66, 77 } ; p = &val[0]; for ( i = 0 ; i<7 ; i++ ) { printf("val[%d]: value is %d and address is %x\n", i, *p, p); p++; } return 0; } Output: val[0]: value is 11 and address is 60feec val[1]: value is 22 and address is 60fef val[2]: value is 33 and address is 60fef val[3]: value is 44 and address is 60fef val[4]: value is 55 and address is 60fefc val[5]: value is 66 and address is 60ff val[6]: value is 77 and address is 60ff

How to Create a File

Whenever you want to work with a file, the first step is to create a file. A file is nothing but space

in a memory where data is stored.

To create a file in a 'C' program following syntax is used,

FILE *fp; fp = fopen ("file_name", "mode");

In the above syntax, the file is a data structure which is defined in the standard library.

fopen is a standard function which is used to open a file.

• If the file is not present on the system, then it is created and then opened.

• If a file is already present on the system, then it is directly opened using this function.

fp is a file pointer which points to the type file.

Whenever you open or create a file, you have to specify what you are going to do with the file.

A file in 'C' programming can be created or opened for reading/writing purposes. A mode is

used to specify whether you want to open a file for any of the below-given purposes. Following

are the different types of modes in 'C' programming which can be used while working with a

file.

Different operations that can be performed on a file are: 1.Creation of a new file (fopen with attributes as “a” or “a+” or “w” or “w++”) 2.Opening an existing file (fopen) 3.Reading from file (fscanf or fgetc) 4.Writing to a file (fprintf or fputs) 5.Moving to a specific location in a file (fseek, rewind) 6.Closing a file (fclose)