File&Command
File&Command
Example 1: Program to Create a File, Write in it, And Close the File
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()
{
Output
The file is now opened.
This is file handleing program
Data successfully read from file hello.c
The file is now closed.
#include <stdio.h>
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[] )
{
if(argc < 2)
{
printf("No argument passed through command line.\n");
}
else
{
printf("First argument is: %s\n", argv[1]);
}
}