file handling and command line arguments in c

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

Upload: mahendra-yadav

Post on 29-Jan-2018

420 views

Category:

Engineering


0 download

TRANSCRIPT

Page 1: File Handling and Command Line Arguments in C

File Handling & Command Line Arguments in C Programming

Seminar Presented By-CS-681 Mahendra Yadav-1200113042

Praveen Kumar-12000113061

Page 2: File Handling and Command Line Arguments in C

Index

1.1 Introduction to File Handling

1.2 Steps in Processing a File

1.3 Basic File Operations

1.4 File Open Modes

1.5 Advance File Open Modes

1.6 File Handling Example and Output

2.1 Introduction to Command Line Arguments

2.2 How to Use Command Line Arguments

2.3 argc and argv

2.4 Command Line Arguments Example and Output 1 of 14

Page 3: File Handling and Command Line Arguments 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 and Command Line Arguments 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 and Command Line Arguments 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 and Command Line Arguments in C

File Open Modes

5 of 14

Page 7: File Handling and Command Line Arguments 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 and Command Line Arguments in C

File Handling Example

7 of 14

Page 9: File Handling and Command Line Arguments in C

Output

9

Here we are compiling the code by executing the command “gcccreate_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 and Command Line Arguments 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 and Command Line Arguments 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 and Command Line Arguments 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 and Command Line Arguments in C

Command Line Arguments Example

12 of 14

Page 14: File Handling and Command Line Arguments in C

Output

▪ $gcc prog.c

▪ $./a.out 10 20 30

▪ The Sum is 60.

14

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 and Command Line Arguments in C

14 of 14