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

Functions in python slide share
Functions in python slide shareFunctions in python slide share
Functions in python slide shareDevashish Kumar
 
Looping statement in python
Looping statement in pythonLooping statement in python
Looping statement in pythonRaginiJain21
 
Basics of Object Oriented Programming in Python
Basics of Object Oriented Programming in PythonBasics of Object Oriented Programming in Python
Basics of Object Oriented Programming in PythonSujith Kumar
 
Python File Handling | File Operations in Python | Learn python programming |...
Python File Handling | File Operations in Python | Learn python programming |...Python File Handling | File Operations in Python | Learn python programming |...
Python File Handling | File Operations in Python | Learn python programming |...Edureka!
 
File handling in Python
File handling in PythonFile handling in Python
File handling in PythonMegha V
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator OverloadingNilesh Dalvi
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructorsNilesh Dalvi
 
Stream classes in C++
Stream classes in C++Stream classes in C++
Stream classes in C++Shyam Gupta
 
Memory allocation in c
Memory allocation in cMemory allocation in c
Memory allocation in cPrabhu Govind
 
Functions in python
Functions in pythonFunctions in python
Functions in pythoncolorsof
 

What's hot (20)

Functions in python slide share
Functions in python slide shareFunctions in python slide share
Functions in python slide share
 
File handling in c
File handling in cFile handling in c
File handling in c
 
Looping statement in python
Looping statement in pythonLooping statement in python
Looping statement in python
 
Basics of Object Oriented Programming in Python
Basics of Object Oriented Programming in PythonBasics of Object Oriented Programming in Python
Basics of Object Oriented Programming in Python
 
Python Functions
Python   FunctionsPython   Functions
Python Functions
 
Python File Handling | File Operations in Python | Learn python programming |...
Python File Handling | File Operations in Python | Learn python programming |...Python File Handling | File Operations in Python | Learn python programming |...
Python File Handling | File Operations in Python | Learn python programming |...
 
File handling in Python
File handling in PythonFile handling in Python
File handling in Python
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
Python programming : Files
Python programming : FilesPython programming : Files
Python programming : Files
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
Stream classes in C++
Stream classes in C++Stream classes in C++
Stream classes in C++
 
Loops in Python
Loops in PythonLoops in Python
Loops in Python
 
Python basics
Python basicsPython basics
Python basics
 
Python dictionary
Python   dictionaryPython   dictionary
Python dictionary
 
Memory allocation in c
Memory allocation in cMemory allocation in c
Memory allocation in c
 
Differences between c and c++
Differences between c and c++Differences between c and c++
Differences between c and c++
 
Functions in python
Functions in pythonFunctions in python
Functions in python
 
Strings in python
Strings in pythonStrings in python
Strings in python
 
Python-Inheritance.pptx
Python-Inheritance.pptxPython-Inheritance.pptx
Python-Inheritance.pptx
 
File in C language
File in C languageFile in C language
File in C language
 

Viewers also liked

Command line arguments.21
Command line arguments.21Command line arguments.21
Command line arguments.21myrajendra
 
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 detailgourav kottawar
 
Managing console input and output
Managing console input and outputManaging console input and output
Managing console input and outputgourav kottawar
 
Overview of c language
Overview of c languageOverview of c language
Overview of c languageshalini392
 
Operator in c programming
Operator in c programmingOperator in c programming
Operator in c programmingManoj Tyagi
 

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

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.pptxSKUP1
 
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.pptxLECO9
 
C_and_C++_notes.pdf
C_and_C++_notes.pdfC_and_C++_notes.pdf
C_and_C++_notes.pdfTigabu Yaya
 
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)Abdullah khawar
 
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/OWebStackAcademy
 
introduction of c langauge(I unit)
introduction of c langauge(I unit)introduction of c langauge(I unit)
introduction of c langauge(I unit)Prashant Sharma
 
Lecture01
Lecture01Lecture01
Lecture01Xafran
 
embeddedc-lecture1-160404055102.pptx
embeddedc-lecture1-160404055102.pptxembeddedc-lecture1-160404055102.pptx
embeddedc-lecture1-160404055102.pptxsangeetaSS
 
Preprocessor , IOSTREAM Library,IOMANIP Library
Preprocessor , IOSTREAM Library,IOMANIP LibraryPreprocessor , IOSTREAM Library,IOMANIP Library
Preprocessor , IOSTREAM Library,IOMANIP LibraryMeghaj Mallick
 
ppt5-190810161800 (1).pdf
ppt5-190810161800 (1).pdfppt5-190810161800 (1).pdf
ppt5-190810161800 (1).pdfMalligaarjunanN
 
C Programming UNIT 1.pptx
C Programming  UNIT 1.pptxC Programming  UNIT 1.pptx
C Programming UNIT 1.pptxMugilvannan11
 
LOSS_C11- Programming Linux 20221006.pdf
LOSS_C11- Programming Linux 20221006.pdfLOSS_C11- Programming Linux 20221006.pdf
LOSS_C11- Programming Linux 20221006.pdfThninh2
 
Unit 5 quesn b ans5
Unit 5 quesn b ans5Unit 5 quesn b ans5
Unit 5 quesn b ans5Sowri Rajan
 

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

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
 
JAVA_BASICS.ppt
JAVA_BASICS.pptJAVA_BASICS.ppt
JAVA_BASICS.ppt
 

Recently uploaded

VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxk795866
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2RajaP95
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEroselinkalist12
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .Satyam Kumar
 

Recently uploaded (20)

VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptx
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
Design and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdfDesign and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdf
 

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.