Professional Documents
Culture Documents
PROGRAM 1
PROBLEM STATEMENT
Write a program in C to read a file
THEORY
The standard library of C has many I/O functionalities. The functions used for reading a file are
very similar to writing files using keyboard. Standard functions like fopen are used to open a
file. Functions like fgetc are used to read the characters from the file.
PROGRAM LOGIC
1. Take the filename as input
2. Check if the file exists or not. If not, return an error message
3. If the file exists, open the file
4. Read all the characters till the end of the file
5. Output the read characters.
6. Close the file
CODE
#include <stdio.h>
#include <stdlib.h> // For exit()
int main()
{
FILE *fptr;
char filename[100], c;
// Open file
fptr = fopen(filename, "r");
if (fptr == NULL)
{
printf("Cannot open file \n");
exit(0);
}
Input file
Output terminal
2
PROGRAM 2
PROBLEM STATEMENT
Write a program to input some text and write them to a file
THEORY
…. … …
PROGRAM LOGIC
CODE
#include <stdio.h>
..
..
INPUT & OUTPUT