File handling in C

15
File Handling & Command Line Arguments in C Programming Seminar Presented By- CS-681 Mahendra Yadav-1200113042 Praveen Kumar-12000113061

Transcript of File handling in C

Page 1: File handling in C

File Handling & Command Line Arguments in C ProgrammingSeminar Presented By-CS-681 Mahendra Yadav-1200113042

Praveen Kumar-12000113061

Page 2: File handling in C

Index1.1 Introduction to File Handling1.2 Steps in Processing a File1.3 Basic File Operations1.4 File Open Modes1.5 Advance File Open Modes 1.6 File Handling Example and Output2.1 Introduction to Command Line Arguments2.2 How to Use Command Line Arguments2.3 argc and argv2.4 Command Line Arguments Example and Output 1 of

14

Page 3: File handling in C

Introduction

▪ What is a File?• A file is a collection of related data that a computer treats as a single unit.• When a computer reads a file, it copies the file from the storage device to memory; when it writes to a file, it transfers data from memory to the storage device.• C uses a structure called FILE (defined in stdio.h) to store the attributes of a file.

2 of 14

Page 4: File handling in C

Steps in Processing a File

1.Create the stream via a pointer variable usingthe FILE structure:FILE *p;

2.Open the file, associating the stream namewith the file name.

3.Read or write the data.4.Close the file.

3 of 14

Page 5: File handling in C

Basic File Operations▪ fopen - open a file- specify how its opened (read/write)

and type (binary/text)▪ fclose - close an opened file▪ fread - read from a file▪ fwrite - write to a file▪ fseek / fsetpos - move a file pointer to somewhere in a

file.▪ ftell / fgetpos - tell you where the file pointer is located.

4 of 14

Page 6: File handling in C

File Open Modes

5 of 14

Page 7: File handling in C

Advance File Open Modes

r+ - open for reading and writing, start atbeginning

w+ - open for reading and writing (overwritefile)

a+ - open for reading and writing (append iffile exists)

6 of 14

Page 8: File handling in C

File Handling Example

7 of 14

Page 9: File handling in C

9

Output

Here we are compiling the code by executing the command “gcc create_a_file.c “.Then we are executing the code. Which creates a file named emp.rec.Lastly we are viewing the content of the file using the cat command.

Page 10: File handling in C

Introduction to Command Line Arguments

▪ So far, we have been defining the main() function to receive no arguments.▪ But the main( ) function can receive two

arguments.▪ This is how arguments can be passed in at

the command line. int main(int argc, char *argv[ ]) { … }

9 of 14

Page 11: File handling in C

How to Use Command Line Arguments

▪ From the command prompt, we can start running a program by typing its name and pressing ENTER.▪ To pass arguments, we type in the program’s

name followed by somearguments, then press ENTER.▪ Below is an example from the Unix command

prompt.

$ ./myprog$ ./myprog 5 23

10 of 14

Page 12: File handling in C

argc and argv

▪ The name of the variable argc stands for "argument count"; argc contains the number of arguments passed to the program.▪ When the user types in arguments, the user

separates each argument with a space.▪ The name of the variable argv stands for

"argument vector“. argv is an array of character strings.▪ argv[1] is the character string containing the first

argument, argv[2] the second, etc. 11 of 14

Page 13: File handling in C

Command Line Arguments Example

12 of 14

Page 14: File handling in C

14

Output

▪ $gcc prog.c▪ $./a.out 10 20 30▪ The Sum is 60.

Here we are compiling the program by executing the command “gcc prog.c”.Then we have given 3 inputs to the executable file “a.out”.Which is giving the output by adding the numbers.

Page 15: File handling in C

Thank You14 of

14