24B15CS111 - SDF Lab - 1 - Assignment 14 And15
24B15CS111 - SDF Lab - 1 - Assignment 14 And15
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;}
int main() {
FILE *file;
char sentence[100];
if (file == NULL) {
return 1;
return 0;
Answer-#include <stdio.h>
int main() {
FILE *file;
char filename[50];
char ch;
int main() {
FILE *file;
int n, i;
char line[100];
return 0;
}
int main() {
FILE *file;
char filename[50], ch;
int lines = 0;
printf("There are %d lines in the file\n", lines + 1); // Count +1 for the last line
return 0;
}
int main() {
FILE *file1, *file2, *mergeFile;
char file1Name[50], file2Name[50], mergeFileName[50];
char ch;
fclose(file1);
fclose(file2);
fclose(mergeFile);