SlideShare a Scribd company logo
1 of 15
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

What's hot

structure and union
structure and unionstructure and union
structure and union
student
 

What's hot (20)

structure and union
structure and unionstructure and union
structure and union
 
Structure in C
Structure in CStructure in C
Structure in C
 
Type conversion
Type  conversionType  conversion
Type conversion
 
Basics of C programming
Basics of C programmingBasics of C programming
Basics of C programming
 
Loops in C Programming Language
Loops in C Programming LanguageLoops in C Programming Language
Loops in C Programming Language
 
data types in C programming
data types in C programmingdata types in C programming
data types in C programming
 
File handling in c
File handling in cFile handling in c
File handling in c
 
Data types
Data typesData types
Data types
 
C++ Overview PPT
C++ Overview PPTC++ Overview PPT
C++ Overview PPT
 
Pointer in c
Pointer in cPointer in c
Pointer in c
 
User Defined Functions
User Defined FunctionsUser Defined Functions
User Defined Functions
 
Pointers C programming
Pointers  C programmingPointers  C programming
Pointers C programming
 
Files in c++
Files in c++Files in c++
Files in c++
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
Data types in C
Data types in CData types in C
Data types in C
 
Python Functions
Python   FunctionsPython   Functions
Python Functions
 
INLINE FUNCTION IN C++
INLINE FUNCTION IN C++INLINE FUNCTION IN C++
INLINE FUNCTION IN C++
 
Strings in C language
Strings in C languageStrings in C language
Strings in C language
 
File Handling in Python
File Handling in PythonFile Handling in Python
File Handling in Python
 
Managing I/O in c++
Managing I/O in c++Managing I/O in c++
Managing I/O in c++
 

Viewers also liked (9)

Command line arguments.21
Command line arguments.21Command line arguments.21
Command line arguments.21
 
Control statements in c
Control statements in cControl statements in c
Control statements in c
 
Variables, Data Types, Operator & Expression in c in detail
Variables, Data Types, Operator & Expression in c in detailVariables, Data Types, Operator & Expression in c in detail
Variables, Data Types, Operator & Expression in c in detail
 
Managing console input and output
Managing console input and outputManaging console input and output
Managing console input and output
 
15 bitwise operators
15 bitwise operators15 bitwise operators
15 bitwise operators
 
Overview of c language
Overview of c languageOverview of c language
Overview of c language
 
Control statements
Control statementsControl statements
Control statements
 
Operator in c programming
Operator in c programmingOperator in c programming
Operator in c programming
 
Storage class in C Language
Storage class in C LanguageStorage class in C Language
Storage class in C Language
 

Similar to File Handling and Command Line Arguments in C

LOSS_C11- Programming Linux 20221006.pdf
LOSS_C11- Programming Linux 20221006.pdfLOSS_C11- Programming Linux 20221006.pdf
LOSS_C11- Programming Linux 20221006.pdf
Thninh2
 
Unit 5 quesn b ans5
Unit 5 quesn b ans5Unit 5 quesn b ans5
Unit 5 quesn b ans5
Sowri Rajan
 

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

Unit_V_Files handling in c programming language.pptx
Unit_V_Files handling in c programming language.pptxUnit_V_Files handling in c programming language.pptx
Unit_V_Files handling in c programming language.pptx
 
Structures-2
Structures-2Structures-2
Structures-2
 
Lecture 1
Lecture 1Lecture 1
Lecture 1
 
C-Programming C LIBRARIES AND USER DEFINED LIBRARIES.pptx
C-Programming  C LIBRARIES AND USER DEFINED LIBRARIES.pptxC-Programming  C LIBRARIES AND USER DEFINED LIBRARIES.pptx
C-Programming C LIBRARIES AND USER DEFINED LIBRARIES.pptx
 
C-Programming C LIBRARIES AND USER DEFINED LIBRARIES.pptx
C-Programming  C LIBRARIES AND USER DEFINED LIBRARIES.pptxC-Programming  C LIBRARIES AND USER DEFINED LIBRARIES.pptx
C-Programming C LIBRARIES AND USER DEFINED LIBRARIES.pptx
 
C_and_C++_notes.pdf
C_and_C++_notes.pdfC_and_C++_notes.pdf
C_and_C++_notes.pdf
 
Embedded C - Lecture 1
Embedded C - Lecture 1Embedded C - Lecture 1
Embedded C - Lecture 1
 
Pf cs102 programming-8 [file handling] (1)
Pf cs102 programming-8 [file handling] (1)Pf cs102 programming-8 [file handling] (1)
Pf cs102 programming-8 [file handling] (1)
 
Core Java Programming Language (JSE) : Chapter XI - Console I/O and File I/O
Core Java Programming Language (JSE) : Chapter XI - Console I/O and File I/OCore Java Programming Language (JSE) : Chapter XI - Console I/O and File I/O
Core Java Programming Language (JSE) : Chapter XI - Console I/O and File I/O
 
introduction of c langauge(I unit)
introduction of c langauge(I unit)introduction of c langauge(I unit)
introduction of c langauge(I unit)
 
Lecture01
Lecture01Lecture01
Lecture01
 
embeddedc-lecture1-160404055102.pptx
embeddedc-lecture1-160404055102.pptxembeddedc-lecture1-160404055102.pptx
embeddedc-lecture1-160404055102.pptx
 
Preprocessor , IOSTREAM Library,IOMANIP Library
Preprocessor , IOSTREAM Library,IOMANIP LibraryPreprocessor , IOSTREAM Library,IOMANIP Library
Preprocessor , IOSTREAM Library,IOMANIP Library
 
ExtraFileIO.pptx
ExtraFileIO.pptxExtraFileIO.pptx
ExtraFileIO.pptx
 
ppt5-190810161800 (1).pdf
ppt5-190810161800 (1).pdfppt5-190810161800 (1).pdf
ppt5-190810161800 (1).pdf
 
Srgoc dotnet
Srgoc dotnetSrgoc dotnet
Srgoc dotnet
 
C++ Function
C++ FunctionC++ Function
C++ Function
 
C Programming UNIT 1.pptx
C Programming  UNIT 1.pptxC Programming  UNIT 1.pptx
C Programming UNIT 1.pptx
 
LOSS_C11- Programming Linux 20221006.pdf
LOSS_C11- Programming Linux 20221006.pdfLOSS_C11- Programming Linux 20221006.pdf
LOSS_C11- Programming Linux 20221006.pdf
 
Unit 5 quesn b ans5
Unit 5 quesn b ans5Unit 5 quesn b ans5
Unit 5 quesn b ans5
 

Recently uploaded

Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Christo Ananth
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
MsecMca
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
dollysharma2066
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Christo Ananth
 

Recently uploaded (20)

Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
Vivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design SpainVivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design Spain
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 

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.