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

File Handling and Command Line Arguments in C

  • 1.
    File Handling & CommandLine Arguments in C Programming Seminar Presented By- CS-681 MahendraYadav-1200113042 Praveen Kumar-12000113061
  • 2.
    Index 1.1 Introduction toFile 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 isa 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 Processinga 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
  • 6.
  • 7.
    Advance File OpenModes 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
  • 8.
  • 9.
    Output 9 Here we arecompiling 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 CommandLine 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 UseCommand 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 ArgumentsExample 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.
  • 15.