[go: up one dir, main page]

0% found this document useful (0 votes)
4 views4 pages

unit 5 qp

The document provides an overview of file processing in C, detailing types of file access (sequential and random), file functions, and basic operations such as opening, closing, and manipulating files. It includes explanations of command line arguments, file pointers, and the differences between text and binary files. Additionally, it contains programming exercises related to file handling, including reading, writing, and copying file contents.
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)
4 views4 pages

unit 5 qp

The document provides an overview of file processing in C, detailing types of file access (sequential and random), file functions, and basic operations such as opening, closing, and manipulating files. It includes explanations of command line arguments, file pointers, and the differences between text and binary files. Additionally, it contains programming exercises related to file handling, including reading, writing, and copying file contents.
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/ 4

4931_Grace College of Engineering,Thoothukudi

FILE PROCESSING
Files Types of file processing: Sequential access, Random access Sequential access file - Random access

file - Command line arguments.

UNIT-V / PART-A
1. What is a File?
A File is a collection of related information that is permanently stored on the disk and
allows us to access and alter the information whenever necessary.
2. How do you open a file?
The File is opened by the statement
- means file pointer, mode is the
file opening mode such as write, read, or append mode.
3. How do you close a file?
The file is closed by the statement fclose(file-pointer);
4. Mention any five file functions. (Jan 14)
fopen() :Creates a new file for use Opens a new existing file for use
fclose() : Closes a file which has been opened for use
fprintf() : Writes a set of data values to a file
fscanf() : Reads a set of data values from a file
fseek() : Sets the position to a desired point in the file
ftell() : Gives the current position in the file
rewind() : Sets the position to the beginning of the file.
5. What is the use of rewind () function?
This function is used to reset the pointer to the beginning of the file.
6. What do you mean by Command line arguments? (or) What does argv and argc
indicate in command-line argument? (May 18,19)
The main functions can have arguments passed which are called as command line
arguments. There are two command line arguments:Argument count denoted by argc
and Argument vector denoted by argv
The argc is an integer variable which denotes the number of parameters passed and argv
is pointer to array of character strings. The syntax is as follows:
main( int argc , char * argv[ ])
{

}
It can also be returned as
main(argc,argv)
int argc;
char * argv[];
{

}
7. What is sequential access file?
To reading or writing data records in sequential order, that is, one record after the other.
To read record 10, for example, you would first need to read records 1 through 9.
8. What is Random access file?
To the ability to access data at random. The opposite of random access is sequential access.
To go from point A to point Z in a sequential-access system, you must pass through all
intervening points. In a random-access system, you can jump directly to point Z.

CS3251_PIC
4931_Grace College of Engineering,Thoothukudi

9. Define what is the advantage of a random access file?


If the amount of data stored in a file is fairly large, the use of random access will allow
you to search through it quicker. If it had been a sequential access file, you would have
to go through one record at a time until you reach the target data. A random access file
lets you jump directly to the target address where data is located.
10. Define How do you search data in a data file using random access method?
Use the fseek() function to perform random access input/ouput on a file. After the file
was opened by the fopen() function, the fseek would require three parameters to work: a
file pointer to the file, the number of bytes to search, and the point of origin in the file.
11. Write a program in C language to create a data file.
#include <stdio.h>
void main()
{
FILE *fptr;
fptr = fopen("emp.rec", "w");
fclose(fptr);
}
12. What is the use of fseek() function?
This function is used for seeking the pointer position in the file at the specified byte.
Syntax: fseek( file pointer, displacement, pointer position);
Where
file pointer -It is the pointer which points to the file.
displacement -It is positive or negative. This is the number of bytes which are skipped
backward (if negative) or forward( if positive) from the current position. This is attached
with L because this is a long integer.
13. Write a program to copy contents of input.txt file to output.txt file.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main() {
FILE *fp1, *fp2;
char ch;
clrscr();
fp1 = fopen("Sample.txt", "r");
fp2 = fopen("Output.txt", "w");
while (1) {
ch = fgetc(fp1);
if (ch == EOF)
break;
else
putc(ch, fp2);
}
printf("File copied Successfully!");
fclose(fp1);
fclose(fp2);
}

CS3251_PIC
4931_Grace College of Engineering,Thoothukudi

14. Why ftell() method is used in C files?


This function returns the value of the current pointer position in the file. The value is
count from the beginning of the file.
Syntax: ftell(fptr);
Where fptr is a file pointer.
15. Compare Text file and Binary file.
Text file is human readable because everything is stored in terms of text. In binary
file everything is written in terms of 0 and 1, therefore binary file is not human
readable.
A newline(\n) character is converted into the carriage return-linefeed combination
before being written to the disk. In binary file, these conversions will not take place.
In text file, a special character, whose ASCII value is 26, is inserted after the last
character in the file to mark the end of file. There is no such special character present
in the binary mode files to mark the end of file.
In text file, the text and characters are stored one character per byte. For example, the
integer value 1245 will occupy 2 bytes in memory but it will occupy 5 bytes in text
file. In binary file, the integer value 1245 will occupy 2 bytes in memory as well as in
file.
16. Define File Pointer in C.
To access any file, we need to declare apointer to FILE structure and then associate it
with the particular file.
This pointer is referred to as file pointer.
Syntax to declare file pointer: FILE * fp;
17. List down functions for reading and writing data of a file.
Reading or writing characters using fgetc() and fputc() functions.
Reading or writing string using fgets() and fputs() functions.
Reading or writing integers using getw() and putw() functions.
Reading or writing formatted IO using fscanf() and fprintf() functions.
Reading or writing records using fread() and fwrite() functions.
18. What is purpose of library function feof()?
feof() function is a file handling function in C programming language which is used to
find the end of a file. Syntax: int feof(FILE *fp);
19. Write about the basic operations on files?
Naming a file
Opening a file
Reading data from file
Writing data into file
Closing a File
20. How can you restore a redirected standard streams?
C library functions named dup() and fdopen(), you can restore a standard stream such
as stdout to its original state. The dup() function duplicates a file handle. You can use
the dup() function to save the file handle corresponding to the stdout standard stream.
The fdopen() function opens a stream that has been duplicated with the dup() function.
21. Why files are needed? (May 19)
When a program is terminated, the entire data is lost. Storing in a file will preserve
your data even if the program terminates.
If you have to enter a large number of data, it will take a lot of time to enter them all.
However, if you have a file containing all the data, you can easily access the contents
of the file using a few commands in C.
You can easily move your data from one computer to another without any changes.

CS3251_PIC
4931_Grace College of Engineering,Thoothukudi

UNIT-V / PART-B
1. Write a program that will receive a filename and a line of text as command line
arguments and write the text to the file.
2. Write a program to copy the contents of one file into another.
3. Write a program that appends one file at the end of another.
4. Write a program that compares 2 files and returns 0 if they are equal and 1 if they are
not.
5. Explain in detail various functions for sequential file manipulations and operations.
6. Write a complete C program for reading an employee file containing { emp no,name,
salary, address}. Create an output file containing the name of those employees along
with their salary and address. (Jan 14)
7. a) Write the C program to find the number of lines in a file.
b) Write the C program to print contents in reverse order of a file.
8. Write the C program to compare contents of two files.
9. Explain the types of file processing with necessary examples. (May 18)
10. Write the C coding for finding the average of number stored in sequential access file.
(May 18)
11. Write the case study of sequential Access file is differing from Random Access
(May 18)
12. Write a C program to write all the members of an structures to a file fwrite( ). Read the
array from the file and display on the screen. (May 18)
13. Explain in detail various operations that can be done on the file giving suitable
examples. (May 19)
14. Explain in detail random access in files along with the functions used for the same in C.
Give suitable examples. (May 19)

CS3251_PIC

You might also like