0% found this document useful (0 votes)
24 views7 pages

24B15CS111 - SDF Lab - 1 - Assignment 14 And15

The document outlines a Software Development Fundamentals Lab assignment focused on file handling in C programming. It includes objectives, theoretical explanations of text and binary files, and several programming tasks such as creating, reading, writing, and merging files. Each task is accompanied by sample code and expected outputs.
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)
24 views7 pages

24B15CS111 - SDF Lab - 1 - Assignment 14 And15

The document outlines a Software Development Fundamentals Lab assignment focused on file handling in C programming. It includes objectives, theoretical explanations of text and binary files, and several programming tasks such as creating, reading, writing, and merging files. Each task is accompanied by sample code and expected outputs.
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/ 7

Software Development Fundamentals Lab – I [24B15CS111]

Assignment Sheet-Week 14 and 15


(18th-23th Nov) and (25th – 30th Nov.)

Enroll.no.992401040060 priyesh pandey File


Handling
Objective: Opening a file fopen(), Reading a file fread(), Writing data into files fprintf(),
fscanf(), File modes, File copy programs, Text files and Binary Files

Theory: Data stored in variables and arrays is temporary — it’s lost when the program
terminates. C allows a program to read data from a file or write data to a file. Once the data is
saved in a file on computer disk, it will remain there after the program stops running. The
data can then be retrieved and used at a later time.
There are two types of files in C - text files and binary files.
A text file is processed as a sequence of characters. In a text file there is a special end-of-line
symbol that divides file into lines. In addition you can think that there is a special end-of-file
symbol that follows the last component in a file. A big advantage of text files is that it may be
opened and viewed in a text editor such as Notepad.
A binary file stores data that has not been translated into character form. Binary files
typically use the same bit patterns to represent data as those used to represent the data in the
computer's main memory. These files are called binary files because the only thing they have
in common is that they store the data as sequences of zeros and ones.
Steps to process file Input/Output in C :Declare file pointer.
1. Open the file.
2. Read / write data to the file.
3. Close the file, when the program is finished.

Program:
#include <stdio.h>
int main()
{ FILE *out_file; /* file pointer */
int number;
/* Open file in write mode */
out_file = fopen("sample.txt", "w");
if (out_file == NULL)
{
printf("Can't open file for writing.\n");
}
else
{
for (number = 1; number <= 10; number++)
{ /* Write data to file */
fprintf(out_file, "%d ", number);
} /* Close the file */
fclose(out_file);
}
return 0;}

1. Write a program in C to create and store information in a text file.


Sample input:
Input a sentence for the file : This is the content of the file test.txt.
Expected Output:
The file test.txt created successfully...!!

Answer- #include <stdio.h>

int main() {

FILE *file;

char sentence[100];

printf("Input a sentence for the file: ");

fgets(sentence, sizeof(sentence), stdin);

file = fopen("test.txt", "w"); // Open file in write mode

if (file == NULL) {

printf("Error: Unable to create file.\n");

return 1;

fprintf(file, "%s", sentence); // Write sentence to the file

fclose(file); // Close the file


printf("The file test.txt created successfully...!!\n");

return 0;

2. Write a program in C to read an existing file. Go to the editor


Test Data :
Input the file name to be opened : test.txt
Expected Output :

The content of the file test.txt is :


This is the content of the file test.txt.

Answer-#include <stdio.h>

int main() {
FILE *file;
char filename[50];
char ch;

printf("Input the file name to be opened: ");


scanf("%s", filename);

file = fopen(filename, "r"); // Open file in read mode


if (file == NULL) {
printf("Error: File does not exist.\n");
return 1;
}

printf("The content of the file %s is:\n", filename);


while ((ch = fgetc(file)) != EOF) { // Read file character by character
putchar(ch);
}
fclose(file); // Close the file
return 0;
}

3. Write a program in C to write multiple lines in a text file.


Test Data :
Input the number of lines to be written : 4
:: The lines are ::
test line 1
test line 2
test line 3
test line 4
Expected Output :

The content of the file test.txt is :


test line 1
test line 2
test line 3
test line 4

answer- #include <stdio.h>

int main() {
FILE *file;
int n, i;
char line[100];

printf("Input the number of lines to be written: ");


scanf("%d", &n);
getchar(); // To consume the newline after entering the number

file = fopen("test.txt", "w"); // Open file in write mode


if (file == NULL) {
printf("Error: Unable to create file.\n");
return 1;
}

printf(":: The lines are ::\n");


for (i = 0; i < n; i++) {
fgets(line, sizeof(line), stdin);
fputs(line, file); // Write line to the file
}

fclose(file); // Close the file


printf("The content of the file test.txt is:\n");

// Displaying written content


file = fopen("test.txt", "r");
while (fgets(line, sizeof(line), file)) {
printf("%s", line);
}
fclose(file);

return 0;
}

4. Write a program in C to find the number of lines in a text file.


Enter file name: abc.txt
There are 43 lines in the file

Answer- #include <stdio.h>

int main() {
FILE *file;
char filename[50], ch;
int lines = 0;

printf("Enter file name: ");


scanf("%s", filename);

file = fopen(filename, "r"); // Open file in read mode


if (file == NULL) {
printf("Error: File does not exist.\n");
return 1;
}

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


if (ch == '\n') {
lines++;
}
}
fclose(file); // Close the file

printf("There are %d lines in the file\n", lines + 1); // Count +1 for the last line
return 0;
}

5. Write a C Program to append the content of file at the end of another.


Enter name of first file a.txt
Enter name of second file b.txt
Enter name to store merged file merge.txt
Two files merged merge.txt successfully.

Answer- #include <stdio.h>

int main() {
FILE *file1, *file2, *mergeFile;
char file1Name[50], file2Name[50], mergeFileName[50];
char ch;

printf("Enter name of first file: ");


scanf("%s", file1Name);
printf("Enter name of second file: ");
scanf("%s", file2Name);
printf("Enter name to store merged file: ");
scanf("%s", mergeFileName);

file1 = fopen(file1Name, "r");


file2 = fopen(file2Name, "r");
mergeFile = fopen(mergeFileName, "w");

if (file1 == NULL || file2 == NULL || mergeFile == NULL) {


printf("Error: Unable to open files.\n");
return 1;
}

// Append first file to merged file


while ((ch = fgetc(file1)) != EOF) {
fputc(ch, mergeFile);
}

// Append second file to merged file


while ((ch = fgetc(file2)) != EOF) {
fputc(ch, mergeFile);
}

fclose(file1);
fclose(file2);
fclose(mergeFile);

printf("Two files merged into %s successfully.\n", mergeFileName);


return 0;

You might also like