0% found this document useful (0 votes)
11 views

File&Command

The document provides examples of file handling and command line arguments in C. The first example shows how to open a file, write data to it, and close it. The second example demonstrates opening a file, reading the contents, and closing it. The last section explains how to pass command line arguments to a C program's main function using argc and argv, with argv[0] being the program name and subsequent elements holding any arguments passed.

Uploaded by

gafos82267
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

File&Command

The document provides examples of file handling and command line arguments in C. The first example shows how to open a file, write data to it, and close it. The second example demonstrates opening a file, reading the contents, and closing it. The last section explains how to pass command line arguments to a C program's main function using argc and argv, with argv[0] being the program name and subsequent elements holding any arguments passed.

Uploaded by

gafos82267
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Examples of File Handing in C

Example 1: Program to Create a File, Write in it, And Close the File

// C program to Open a File,


// Write in it, And Close the File
#include <stdio.h>
#include <string.h>
int main()
{
// Declare the file pointer
FILE *filePointer;
// Get the data to be written in file
char dataToBeWritten[50] = {"This is file handleing program"};

// Open the existing file using fopen()


// in write mode using "w" attribute
filePointer = fopen("hello.c", "w");
// Check if this filePointer is null
// which maybe if the file does not exist
if (filePointer == NULL)
{
printf("hello.c file failed to open.");
}
else {
printf("The file is now opened.\n");
// Write the dataToBeWritten into the file
if (strlen(dataToBeWritten) > 0) {
// writing in the file using fputs()
fputs(dataToBeWritten, filePointer);
fputs("\n", filePointer);
}
// Closing the file using fclose()
fclose(filePointer);
printf("Data successfully written in file "
"hello.c\n");
printf("The file is now closed.");
}
return 0;
}

Output
The file is now opened.
Data successfully written in file hello.c
The file is now closed.
This program will create a file named hello.c in the same directory as
the source file which will contain the following text: “This is file
handleing program

Example 2: Program to Open a File, Read from it, And Close the File
// C program to Open a File,
// Read from it, And Close the File
#include <stdio.h>
#include <string.h>

Void main()
{

// Declare the file pointer


FILE *filePointer;

// Declare the variable for the data to be read from


// file
char dataToBeRead[50];

// Open the existing file using fopen()


// in read mode using "r" attribute
filePointer = fopen("hello.c", "r");

// Check if this filePointer is null


// which maybe if the file does not exist
if (filePointer == NULL) {
printf("hello.c file failed to open.");
}
else {

printf("The file is now opened.\n");

// Read the dataToBeRead from the file


// using fgets() method
while (fgets(dataToBeRead, 50, filePointer)
!= NULL)
{
// Print the dataToBeRead
printf("%s", dataToBeRead);
}

// Closing the file using fclose()


fclose(filePointer);

printf( "Data successfully read from hello.c\n");


printf("The file is now closed.");
}
return 0;
}

Output
The file is now opened.
This is file handleing program
Data successfully read from file hello.c
The file is now closed.

Command Line Arguments in C


The most important function of C is the main() function. It is mostly
defined with a return type of int and without parameters as shown
below:
int main() {
...
}
We can also give command-line arguments in C and C++. Command-
line arguments are the values given after the name of the program in the
command-line shell of Operating Systems. Command-line arguments
are handled by the main() function of a C/C++ program.
To pass command-line arguments, we typically define main() with two
arguments: the first argument is the number of command-line
arguments and the second is a list of command-line arguments.
Syntax:
int main(int argc, char *argv[]) { /* ... */ }
or
int main(int argc, char **argv) { /* ... */ }
Here,
 argc (ARGument Count) is an integer variable that stores the
number of command-line arguments passed by the user including the
name of the program. So if we pass a value to a program, the value
of argc would be 2 (one for argument and one for program name)
 The value of argc should be non-negative.
 argv (ARGument Vector) is an array of character pointers listing
all the arguments.
 If argc is greater than zero, the array elements from argv[0] to
argv[argc-1] will contain pointers to strings.
 argv[0] is the name of the program , After that till argv[argc-1] every
element is command -line arguments.
C

// program named mainreturn.c

#include <stdio.h>

// defining main with arguments

int main(int argc, char* argv[])

printf("You have entered %d arguments:\n", argc);

for (int i = 0; i < argc; i++)

printf("%s\n", argv[i]);

}
Example

Let's see the example of command line arguments where we are passing
one argument with file name.

#include <stdio.h>
void main(int argc, char *argv[] )
{

printf("Program name is: %s\n", argv[0]);

if(argc < 2)
{
printf("No argument passed through command line.\n");
}
else
{
printf("First argument is: %s\n", argv[1]);
}
}

Properties of Command Line Arguments:


1. They are passed to the main() function.
2. They are parameters/arguments supplied to the program when it is
invoked.
3. They are used to control programs from outside instead of hard
coding those values inside the code.
4. argv[argc] is a NULL pointer.
5. argv[0] holds the name of the program.
6. argv[1] points to the first command line argument and argv[argc-1]
points to the last argument.
Note: You pass all the command line arguments separated by a space,
but if the argument itself has a space, then you can pass such
arguments by putting them inside double quotes “” or single quotes ”.

You might also like