2075 11

CSC115 ยท 2075 ยท 5 marks

Write short note on: a. Benefits of data files b. Graphics functions. [2.5+2.5]

Write short note on: a. Benefits of data files b. Graphics functions. [2.5+2.5]

C ProgrammingChapter 10: File Handling in CConcept of File

Quick answer

A file is a collection of related data placed on the disk. C supports the concept of files through which data can be stored on a disk or secondary storage device, and the stored data can be read whenever required.

Short Notes


a. Benefits of Data Files (2.5 marks)

The key benefits of using data files are:

1. Permanent Storage of Data

Data stored in files is saved on the disk (secondary storage). Unlike variables stored in RAM, file data is not lost when the program terminates. It can be retrieved at any time.

2. Handling Large Amounts of Data

Files allow programs to process large volumes of data that cannot be held entirely in memory at once. Data can be read or written in parts (blocks, strings, characters, etc.).

3. Data Reusability

Once data is written to a file, it can be read and reused by the same program or different programs multiple times without re-entering the data.

4. Data Sharing Between Programs

Multiple programs can access the same data file, enabling easy sharing and transfer of information between different applications.

5. Easy Data Modification

Files can be opened in various modes such as "r+" (read + write / update mode) or "a+" (append + read) to modify or add new data without destroying existing data.

6. Organized Data Management

Data can be stored in a structured and organized manner. C provides various file modes ("r", "w", "a", "rb", "wb", etc.) and I/O functions (fread(), fwrite(), fprintf(), fscanf(), etc.) to manage data efficiently.

7. Buffered I/O for Efficiency

C file handling uses buffers to temporarily hold data during read/write operations. When a file is closed using fclose(), all buffered data is flushed (written) to the file, ensuring no data is lost and improving I/O performance.


b. Graphics Functions (2.5 marks)

C provides a graphics library (graphics.h) that allows drawing shapes, lines, and images on the screen using pixel-based coordinates. The origin pixel (0, 0) is located at the top-left corner of the screen.

The important graphics functions are:

1. initgraph(&gd, &gm, path)

  • Purpose: Initializes the graphics system and puts the screen into graphics mode.
  • gd = graphics driver, gm = graphics mode, path = path to BGI driver files.
  • Example: initgraph(&gd, &gm, "c:\\tc\\bgi");

2. circle(x, y, radius)

  • Purpose: Draws a circle with center at (x, y) and the given radius.
  • Example: circle(300, 200, 150);

3. line(x1, y1, x2, y2)

  • Purpose: Draws a line from point (x1, y1) to point (x2, y2) using pixels placed near each other.
  • Coordinates are measured from the top-left corner of the screen.

4. getpixel(x, y)

  • Purpose: Gets the color of the pixel at position (x, y).
  • Returns an integer value corresponding to the color of that pixel.
  • The pixel is the smallest element on the graphics display screen.

5. setcolor(color)

  • Purpose: Sets the current drawing color for subsequent graphics operations.
  • Example: setcolor(WHITE);

6. closegraph()

  • Purpose: Closes the graphics mode and restores the screen to text mode, freeing resources used by the graphics system.

Example Program (from notes):

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
    int gd = DETECT, gm;
    initgraph(&gd, &gm, "c:\\tc\\bgi");
    circle(300, 200, 150);
    setcolor(WHITE);
    getch();
    closegraph();
}

Note: In C graphics, images are formed by placing pixels (dots) near each other on the screen. The pixel at position (0, 0) is always at the top-left corner of the screen.

Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.