0% found this document useful (0 votes)
19 views4 pages

Experiment No 11

Uploaded by

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

Experiment No 11

Uploaded by

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

Experiment no 11: File, Basic file operations: Create, Open, Read, Write, Delete.

Sample Problem: Implement a file operation that would write some strings in a file, and read
from it.
Required equipment and devices:

1. PC/Laptop
2. Code Blocks IDE
3. GNU/GCC

Sample Input: At created txt file: Student of BUBT (Fig-2)


Sample Output: Student of BUBT ( fig 3)
Solution:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main(){

// File Create
FILE *Jisan;
char buffer[30], c;

Jisan = fopen ("Jisan.txt", "r");


printf("--------read a line--------\n");
printf(buffer, 50, Jisan);
printf("%s\n", buffer);

printf("------Read and parse data------\n");


Jisan = fopen("Jisan.txt", "r");
char str1[10], str2[2], str3[20], str4[2];
fscanf(Jisan, "%s %s %s %s", str1, str2, str3, str4);

printf("Read String1 |%s|\n", str1);


printf("Read String2 |%s|\n", str2);
printf("Read String3 |%s|\n", str3);
printf("Read String4 |%s|\n", str4);

printf("Read the entire file\n");


Jisan = fopen("Jisan.txt", "r");
while ((c = getc(Jisan)) != EOF) printf("%c", c);

// write file
printf("Write the file\n");
Jisan = fopen("Jisan.txt", "w");
while ((c = getc(Jisan)) !=EOF) printf("%c", c);

fclose(Jisan);

return 0;
}

Input/Output Screenshot:
Fig-1

Fig-2

Fig-3
References:

1. Teach Yourself C by Herbert Schildt.


2. Online programming websites:
I. https://www.geeksforgeeks.org/

You might also like