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

C Programming File Handling

Uploaded by

punkcybe770
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

C Programming File Handling

Uploaded by

punkcybe770
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

ST.

XAVIER’S COLLEGE
Maitighar, Kathmandu
(Affiliated to National Examinations Board)

C Programming File Handling I

Submitted by
Bibek Gautam
023NEB734
Grade 12 “G”

Submitted to:
Department of Computer
Science (Plus Two)
QUESTION I: Write a C program to write a character and display it using fgetc () and fputc ()
functions.
Code:
#include<stdio.h>
#include<stdlib.h>
int main () {
FILE *fptr;
fptr = fopen("sample.txt", "w");
if(fptr == NULL) {
printf("Error in opening File!!");
exit(1);
}
char ch;
printf("Enter a character: ");
scanf("%c", &ch);
fputc(fptr, ch);
fclose(fptr);
printf(“\n\nProgram Run By Bibek Gautam”);
return 0;

}
Output:

File:
QUESTION II: Write a C program to write a number and display it using getw() and putw() functions.

Code:
#include<stdio.h>
#include<stdlib.h>

int main() {
int num;
FILE *fptr;
fptr = fopen("sample.txt", "w+");
if(fptr == NULL) {
printf("Error in opening file!!");
}
printf("Enter a number: ");
scanf("%d", &num);
putw(num, fptr);
rewind(fptr);
printf("You entered %d", getw(fptr));
fclose(fptr);
printf("\n\nProgram Run By Bibek Gautam");
return 0;

}
Output:

File:

QUUESTION III: Write a C program to input a string and display it using fputs() and fgets() functions.
Code:
#include<stdio.h>
#include<stdlib.h>
int main() {
char str[20];
FILE *fptr;
fptr = fopen("sample.txt", "w+");
if(fptr == NULL) {
printf("Error in opening file!!");
}
printf("Enter a string: ");
scanf("%20[^\n]", str);
fputs(str, fptr);
rewind(fptr);
char returnedStr[20];
fgets(returnedStr, 20, fptr);
printf("You entered %s", returnedStr);
fclose(fptr);
printf("\n\nProgram Run By Bibek Gautam");
return 0;

}Output:

File:

QUUESTION IV: Write a program to copy the contents of one file to another.

Code:
#include<stdio.h>
#include<stdlib.h>

int main() {
FILE *fptr1, *fptr2;
printf("Enter the name of file of which content has to be copied: ");
char file1[20];
scanf("%s", file1);
fptr1 = fopen(file1, "r");

printf("Enter the name of file in which content has to be copied: ");


char file2[20];
scanf("%s", file2);
fptr2 = fopen(file2, "w");

int c;

while((c = fgetc(fptr1)) != EOF) {


fputc(c, fptr2);
}
printf("Content copied to %s.", file2);
printf("\n\nProgram Run By Bibek Gautam");
return 0;

}
Output:

File:

QUESTION V: Write a program to count the number of characters in a file you have created.
Code:
#include<stdio.h>
#include<stdlib.h>
int main() {
FILE *fptr;
fptr = fopen("sample.txt", "r");
if(fptr == NULL) {
printf("Error in opening file!!");
exit(1);
}

int ch, counter = 0;

while((ch = fgetc(fptr)) != EOF) {


counter++;
}
printf("Number of characters is %d.", counter);
printf("\n\nProgram Run By Bibek Gautam");
return 0;

}
Output:

File:

You might also like