0% found this document useful (0 votes)
6 views6 pages

Unit 6

The document provides an overview of key concepts in C programming, including storage classes, file access modes, and the differences between sequential and random access. It also covers command line arguments, file handling functions, and attributes, along with examples of C programs for file operations. Additionally, it discusses the advantages of random access files and methods for seeking specific positions within them.

Uploaded by

raajeevmagesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views6 pages

Unit 6

The document provides an overview of key concepts in C programming, including storage classes, file access modes, and the differences between sequential and random access. It also covers command line arguments, file handling functions, and attributes, along with examples of C programs for file operations. Additionally, it discusses the advantages of random access files and methods for seeking specific positions within them.

Uploaded by

raajeevmagesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Sl.

N
Questions
o.

1. Define storage class.

storage class defines the lifetime, scope, and visibility of a variable or function. It determines how
and where the variable is stored, and how long it exists in memory. Storage classes are crucial for
understanding the behavior of variables, especially in terms of their lifetime (i.e., when they are
created and destroyed), visibility (i.e., where they can be accessed), and scope (i.e., the part of the
program in which they are valid).

There are four main storage classes in C:

1. auto
2. register
3. static
4. extern

2. Mention different types of file accessing.


1. Read Mode ®
2. Write Mode (w)
3. Append Mode (a)
4. Read/Write Mode (r+/w+)

3. Distinguish between Sequential access and Random access.


parameters Sequential access Random access.

Access speed Slower than random access It allows direct access to


since accessing a specific specific record in a file
record requires reading through
all the previous record
Access method File allows to access a record File access to specific record
in a sequential matter using index,number or key
Example Text file,log Database,spreadsheet

4. What is meant by command line argument.? Give an


Example.

Command Line Arguments are inputs given to a C program at the time of execution through the

terminal or command prompt. These arguments are passed to the main() function when the program

starts

int main(int argc, char *argv[])


5. List out the various file handling functions.
file handling refers to the process of working with files, such as opening, reading, writing, and
closing files. The C Standard Library provides a set of functions to perform these operations. These
functions are available in the <stdio.h> header file.
fopen()
fclose()
fgetc()
fputc()
fgets()
fputs()
ftell()
frewind()
fclose()

6. List out the major attributes and operations of a file.

Attributes Operations
File Name fopen()
File Descriptor / Pointer fclose()
File Mode fgetc(), fgets(), fread()
File Size fputc()

7. Write a program in C to display the last modification time of a file.


#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <time.h>

int main() {
struct stat fileStat;
char filename[100];

// Prompt user for the filename


printf("Enter the filename: ");
scanf("%s", filename);

// Get file status


if (stat(filename, &fileStat) == -1) {
perror("Error retrieving file information");
return 1;
}

// Convert the last modification time to human-readable format


printf("Last modification time of %s: %s", filename, ctime(&fileStat.st_mtime));
return 0;
}
8.
How do fputs() and fputc() differ from each other in terms of functionality?

9. Write a program in C to copy a file to another name.

Refer q.no 1 PART B

10. Find the average in a sequence of numbers in c while ignoring the negative numbers in sequence.
11.
What is a random access file?

Random Access, also known as Direct Access, allows data to be read from or written to any

position in the file without reading through the file sequentially. This type of file processing is

useful for scenarios where quick access to specific parts of the file is needed.
12.
What is the main advantage of using random access files?

Efficient Access to Large Files

Faster Data Retrieval

Non-Sequential Reading/Writing

Direct Data Modifications

13.
In which programming language is a common example of a random access file implemented?

A common example of random access file implementation is found in C programming, though it is


also supported in many other programming languages, including C++, Java, Python, and C#.
However, C is particularly notable for its simple and direct support for random access files using
standard library functions.

14.
What method is typically used to seek a specific position in a random access file?

fseek(FILE *fp, long offset, int origin): Moves the file pointer to a specified position.

origin can be:

❖ SEEK_SET (beginning of the file),


❖ SEEK_CUR (current position),
❖ SEEK_END (end of the file).

Example:
fseek(fp, 10, SEEK_SET); // Move to the 10th byte from the beginning

15.
How can you write data to a specific record in a random access file?

Open the File in a mode that allows reading and writing (e.g., r+b in C).
Calculate the Byte Offset: Determine the position of the record in the file. For fixed-length records,
you can calculate the offset as record_number * record_size.

Use fseek() (in C) or an equivalent to move the file pointer to the correct position.

Write the Data to the file at the current position.

Close the File once you are done.

You might also like