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

C lecture 8.1

The document explains the concept of files in computer storage, highlighting their importance for data preservation and ease of access. It details the types of files (text and binary), file operations in C programming, and provides examples of file creation, writing, and reading functions. Additionally, it outlines various file modes and management functions available in C for handling files.

Uploaded by

devlina.karmakar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

C lecture 8.1

The document explains the concept of files in computer storage, highlighting their importance for data preservation and ease of access. It details the types of files (text and binary), file operations in C programming, and provides examples of file creation, writing, and reading functions. Additionally, it outlines various file modes and management functions available in C for handling files.

Uploaded by

devlina.karmakar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Introduction:

A file is a container in computer storage devices used for storing data.


Why files are needed?
When a program is terminated, the entire data is lost. Storing in a file will preserve your data even if the program terminates.
If we have to enter a large number of data, it will take a lot of time to enter them all.
However, if we have a file containing all the data, we can easily access the contents of the file using a few commands in C.
we can easily move your data from one computer to another without any changes.
Types of Files:
When dealing with files, there are two types of files :
• Text files
• Binary files
Text files:
Text files are the normal .txt files. We can easily create text files using any simple text editors such as Notepad.
When we open those files, you'll see all the contents within the file as plain text. We can easily edit or delete the contents.
They take minimum effort to maintain, are easily readable, and provide the least security and takes bigger storage space.
Binary files:
Binary files are mostly the .bin files in our computer.
Instead of storing data in plain text, they store it in the binary form (0's and 1's).
They can hold a higher amount of data, are not readable easily, and provides better security than text files.
File Operations:
In C, you can perform four major operations on files, either text or binary:
• Creating a new file
• Opening an existing file
• Closing a file
• Reading from and writing information to a file
File Creation:
A file is nothing but space in a memory where data is stored.
To create a file in a 'C' program following syntax is used,
FILE *fp;
fp = fopen ("file_name", "mode");
In the above syntax, the file is a data structure which is defined in the standard library.
• If the file is not present on the system, then it is created and then opened.
• If a file is already present on the system, then it is directly opened using this function.
fp is a file pointer which points to the type file.
A file in 'C' programming can be created or opened for reading/writing purposes. A mode is used to specify whether we
want to open a file for any of the below-given purposes.
Following are the most important file management functions available in 'C,'

function purpose
fopen () Creating a file or opening an existing file
fclose () Closing a file
fprintf () Writing a block of data to a file
fscanf () Reading a block data from a file
getc () Reads a single character from a file
putc () Writes a single character to a file
getw () Reads an integer from a file
putw () Writing an integer to a file
fseek () Sets the position of a file pointer to a specified
location
ftell () Returns the current position of a file pointer
rewind () Sets the file pointer at the beginning of a file
File Mode Description
r Open a file for reading. If a file is in reading mode, then no
data is deleted if a file is already present on a system.

w Open a file for writing. If a file is in writing mode, then a new


file is created if a file doesn't exist at all. If a file is already
present on a system, then all the data inside the file is
truncated, and it is opened for writing purposes.

a Open a file in append mode. If a file is in append mode, then


the file is opened. The content within the file doesn't change.

r+ open for reading and writing from beginning

w+ open for reading and writing, overwriting a file

a+ open for reading and writing, appending to file


File Creation & open:
#include <stdio.h>
int main()
{
FILE *fp;
fp = fopen (“computer.txt", "w");
}
File is created in the same folder where we have saved your code.
File closing:
FILE *fp;
fp = fopen (“computer.txt", “w");
fclose (fp);
The fclose function takes a file pointer as an argument. The file associated with the file pointer is then closed with the
help of fclose function. It returns 0 if close was successful and EOF (end of file) if there is an error has occurred while file
closing. After closing the file, the same file pointer can also be used with other files.
Writing to a file:
The stdio library offers the necessary functions to write to a file:
• fputc(char, file_pointer): It writes a character to the file pointed to by file_pointer.
• fputs(str, file_pointer): It writes a string to the file pointed to by file_pointer.
• fprintf(file_pointer, str, variable_lists): It prints a string to the file pointed to by file_pointer. The string can optionally include format specifiers and a list of variables
variable_lists.
fputc() function:
#include <stdio.h>
int main()
{
int i;
FILE * fptr;
char fn[50];
char str[] = “Computer Science Rocks\n";
fptr = fopen(“computer.txt", "w");
for (i = 0; str[i] != '\n'; i++)
{
fputc(str[i], fptr);
}
fclose(fptr);
return 0;
}
fputs () Function:
#include <stdio.h>
int main()
{
FILE * fp;
fp = fopen(“computer.txt", "w+");
fputs(“We are students of CST 2nd year,", fp);
fputs("We don't like exams\n", fp);
fputs(“We love our college\n", fp);
fclose(fp);
return (0);
}
fprintf()Function:
#include <stdio.h>
int main()
{
FILE *fptr;
fptr = fopen(“computer.txt", "w");
fprintf(fptr, "Learning C with my friends\n");
fclose(fptr);
return 0;
}
Reading data from a File:
There are three different functions dedicated to reading data from a file
• fgetc(file_pointer): It returns the next character from the file pointed to by the file pointer. When the end of the
file has been reached, the EOF is sent back.
• fgets(buffer, n, file_pointer): It reads n-1 characters from the file and stores the string in a buffer in which the
NULL character '\0' is appended as the last character.
• fscanf(file_pointer, conversion_specifiers, variable_adresses): It is used to parse and analyze data. It reads
characters from the file and assigns the input to a list of variable pointers variable_adresses using conversion
specifiers. Keep in mind that as with scanf, fscanf stops reading a string when space or newline is encountered.
#include <stdio.h>
int main()
{
FILE * file_pointer;
char buffer[30], c;
file_pointer = fopen("f=database.txt", "r");
printf("----read a line----\n");
fgets(buffer, 50, file_pointer);
printf("%s\n", buffer);
printf("----read and parse data----\n");
file_pointer = fopen(“database.txt", "r");
char str1[10], str2[2], str3[20], str4[2];
fscanf(file_pointer, "%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");
file_pointer = fopen(“database.txt", "r");
while ((c = getc(file_pointer)) != EOF)
printf("%c", c);
fclose(file_pointer);
return 0;
}

You might also like