SlideShare a Scribd company logo
4
Most read
11
Most read
12
Most read
File Handling &
Command Line Arguments
in C Programming
Seminar Presented By-
CS-681 MahendraYadav-1200113042
Praveen Kumar-12000113061
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
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
Steps in Processing a File
1. Create the stream via a pointer variable using
the FILE structure:
FILE *p;
2. Open the file, associating the stream name
with the file name.
3. Read or write the data.
4. Close the file.
3 of 14
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
File Open Modes
5 of 14
Advance File Open Modes
r+ - open for reading and writing, start at
beginning
w+ - open for reading and writing (overwrite
file)
a+ - open for reading and writing (append if
file exists)
6 of 14
File Handling Example
7 of 14
Output
9
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.
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
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 some
arguments, then press ENTER.
▪ Below is an example from the Unix command prompt.
$ ./myprog
$ ./myprog 5 23
10 of 14
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
Command Line Arguments Example
12 of 14
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.
14 of 14

More Related Content

PPTX
Java Data Types
PPTX
Introduction to php
PPTX
File system structure
PDF
Java programming lab manual
PPTX
File Management in C
PPTX
PPTX
Control statements in c
Java Data Types
Introduction to php
File system structure
Java programming lab manual
File Management in C
Control statements in c

What's hot (20)

PPT
History of c
PPTX
Pointers in C Programming
PPTX
Operating system critical section
PPTX
PPTX
Pointer in C++
PPSX
Break and continue
PPTX
Java package
PPSX
JDBC: java DataBase connectivity
PPT
PPTX
C Programming: Control Structure
PPTX
Operators in java
PDF
Constructor and Destructor
PPTX
Stream classes in C++
PPT
Java Streams
PDF
Object Oriented Programming Using C++ Practical File
PPTX
Java - Sockets
PDF
Introduction to c++ ppt
PPTX
Pointers in c++
PPTX
Constructor in java
History of c
Pointers in C Programming
Operating system critical section
Pointer in C++
Break and continue
Java package
JDBC: java DataBase connectivity
C Programming: Control Structure
Operators in java
Constructor and Destructor
Stream classes in C++
Java Streams
Object Oriented Programming Using C++ Practical File
Java - Sockets
Introduction to c++ ppt
Pointers in c++
Constructor in java
Ad

Viewers also liked (8)

PPT
Command line arguments.21
PPTX
Variables, Data Types, Operator & Expression in c in detail
PPTX
Managing console input and output
PDF
15 bitwise operators
PPTX
Overview of c language
PPS
Control statements
PPTX
Operator in c programming
PPTX
Storage class in C Language
Command line arguments.21
Variables, Data Types, Operator & Expression in c in detail
Managing console input and output
15 bitwise operators
Overview of c language
Control statements
Operator in c programming
Storage class in C Language
Ad

Similar to File Handling and Command Line Arguments in C (20)

PDF
Command Line Arguments in Computer Programming
PPTX
Lecturer notes on file handling in programming C
PPTX
Command line arguments
PDF
ppt5-190810161800 (1).pdf
PPT
PDF
File Handling in C Programming
PPT
File in c
PDF
Essential Techniques for Mastering Command Line Arguments in C From NareshIT.pdf
PPT
file.ppt
PPTX
C Programming Unit-5
DOCX
1 CMPS 12M Introduction to Data Structures Lab La.docx
PDF
COMMAND LINE ARGUMENT IN C++
PPTX
File Handling and Preprocessor Directives
PPT
File handling(some slides only)
PDF
File_Management_in_C
PPTX
23_1-Command line Arguments.pptx
PPT
Linux basics
PPT
02 fundamentals
PDF
C_Dayyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy 9.pdf
PPTX
pre processor and file handling in c language ppt
Command Line Arguments in Computer Programming
Lecturer notes on file handling in programming C
Command line arguments
ppt5-190810161800 (1).pdf
File Handling in C Programming
File in c
Essential Techniques for Mastering Command Line Arguments in C From NareshIT.pdf
file.ppt
C Programming Unit-5
1 CMPS 12M Introduction to Data Structures Lab La.docx
COMMAND LINE ARGUMENT IN C++
File Handling and Preprocessor Directives
File handling(some slides only)
File_Management_in_C
23_1-Command line Arguments.pptx
Linux basics
02 fundamentals
C_Dayyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy 9.pdf
pre processor and file handling in c language ppt

Recently uploaded (20)

PPT
Mechanical Engineering MATERIALS Selection
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PPTX
Lesson 3_Tessellation.pptx finite Mathematics
PDF
Structs to JSON How Go Powers REST APIs.pdf
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPTX
Sustainable Sites - Green Building Construction
PPTX
OOP with Java - Java Introduction (Basics)
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
composite construction of structures.pdf
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
DOCX
573137875-Attendance-Management-System-original
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
Internet of Things (IOT) - A guide to understanding
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PPTX
Construction Project Organization Group 2.pptx
Mechanical Engineering MATERIALS Selection
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
Lesson 3_Tessellation.pptx finite Mathematics
Structs to JSON How Go Powers REST APIs.pdf
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Sustainable Sites - Green Building Construction
OOP with Java - Java Introduction (Basics)
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
CYBER-CRIMES AND SECURITY A guide to understanding
composite construction of structures.pdf
Foundation to blockchain - A guide to Blockchain Tech
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
573137875-Attendance-Management-System-original
Operating System & Kernel Study Guide-1 - converted.pdf
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Internet of Things (IOT) - A guide to understanding
Embodied AI: Ushering in the Next Era of Intelligent Systems
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
Construction Project Organization Group 2.pptx

File Handling and Command Line Arguments in C

  • 1. File Handling & Command Line Arguments in C Programming Seminar Presented By- CS-681 MahendraYadav-1200113042 Praveen Kumar-12000113061
  • 2. 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
  • 3. 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
  • 4. Steps in Processing a File 1. Create the stream via a pointer variable using the FILE structure: FILE *p; 2. Open the file, associating the stream name with the file name. 3. Read or write the data. 4. Close the file. 3 of 14
  • 5. 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
  • 7. Advance File Open Modes r+ - open for reading and writing, start at beginning w+ - open for reading and writing (overwrite file) a+ - open for reading and writing (append if file exists) 6 of 14
  • 9. Output 9 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.
  • 10. 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
  • 11. 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 some arguments, then press ENTER. ▪ Below is an example from the Unix command prompt. $ ./myprog $ ./myprog 5 23 10 of 14
  • 12. 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
  • 13. Command Line Arguments Example 12 of 14
  • 14. 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.