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

C-File Operations

The document provides an overview of file operations in C programming, including file pointers, file handling, and various functions for reading and writing data to files. It explains the use of modes like 'w+' for updating files and outlines the steps for reading and writing files using functions such as fopen(), fclose(), fprintf(), and fread(). Additionally, it includes example code snippets demonstrating how to read from and write to files.

Uploaded by

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

C-File Operations

The document provides an overview of file operations in C programming, including file pointers, file handling, and various functions for reading and writing data to files. It explains the use of modes like 'w+' for updating files and outlines the steps for reading and writing files using functions such as fopen(), fclose(), fprintf(), and fread(). Additionally, it includes example code snippets demonstrating how to read from and write to files.

Uploaded by

ba23018846
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

C-File Operations

What is FILE * fPtr?


The file (both text and binary) should be closed after reading/writing. Here, fptr
is a file pointer associated with the file to be closed.

What do you mean by file handling?

File Handling is the storing of data in a file using a program. In C programming


language, the programs store results, and other data of the program to a file
using file handling in C. Also, we can extract/fetch data from a file to work with
it in the program.

What is FILE * fp in C?

FILE *fp ; fp = fopen ("my file . txt", "a"); The function fopen returns a pointer
(referred as the file pointer) to the structure FILE which is defined in the stdio. h
header file . When you open a file make sure that the operation is successful.

What is a file pointer?

File pointer is a pointer which is used to handle and keep track on the files being
accessed. A new data type called “ FILE ” is used to declare file pointer . ...
fopen() function is used to open a file that returns a FILE pointer . Once file is
opened, file pointer can be used to perform I/O operations on the file .

What is the function of the mode W+?


Explanation: w+ is a mode used to open a text file for update (i. e., writing and
reading), discard previous contents if any.

1. Reading
C programming language supports four pre-defined functions to read contents from a file,
defined in stdio.h header file:

1. fgetc()– This function is used to read a single character from the file.
2. fgets()– This function is used to read strings from files.
3. fscanf()– This function is used to read formatted input from a file.
4. fread()– This function is used to read the block of raw bytes from files. This is used to
read binary files.

Steps To Read A File:

● Open a file using the function fopen() and store the reference of the file in a FILE
pointer.
● Read contents of the file using any of these functions fgetc(), fgets(), fscanf(), or
fread().
● File close the file using the function fclose().

Example.
Look at the one in your lab exercise first
Write a C program to read data from a text file and display the data on the screen. If file
does not exist, create a file.

Solution:
#include <stdio.h>
#include <stdlib.h> // For exit()
int main()
{
FILE *fp;
char fname1[50],ch;
printf("Enter filename to open for reading : ");
scanf("%s", fname1);
// Open one file for reading
fp = fopen(fname1, "r");

Comp211 1
if (fp == NULL)
{
fp = fopen(fname1, "w");
printf("\n%s file does not exist hence file created..", fname1);
//exit(0);
}
while((ch=fgetc(fp))!=EOF)
{
printf("%c", ch);
}
fclose(fp);
return 0;
}

Output:

In above output, we enter file name source.txt. If this file is present at same location where
your program is saved then it will display the content in that file.

2. Writing
When a programs output or some of the variables has to be saved to a storage location on file
system, the data has to be written to a file. In this tutorial, we shall learn how to write data to a
file using some of the builtin functions of C programming language.

The following functions are used to write data into the file. They are:

● fprintf()
● fputs()
● fputc()

Comp211 2
● fwrite()

fprintf()

The fprintf() is used to write formatted output to stream.It is used to write a set of characters into
a file.

fputs()

fputs() is used to write a line to a file.

fputc()

fputc() is used to write a character to the stream.

fwrite()

The fwrite() function is used to write data (can contain multiple characters and multiple lines) to
a file.

example1

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

int main() {
char sentence[1000];

// creating file pointer to work with files


FILE *fptr;

// opening file in writing mode


fptr = fopen("program.txt", "w");

// exiting program
if (fptr == NULL) {
printf("Error!");
exit(1);
}
printf("Enter a sentence:\n");
fgets(sentence, sizeof(sentence), stdin);

Comp211 3
fprintf(fptr, "%s", sentence);
fclose(fptr);
return 0;
}

Output

Enter a sentence: C Programming is fun

Here, a file named program.txt is created. The file will contain C programming is fun text.

In the program, the sentence entered by the user is stored in the sentence variable.

Then, a file named program.txt is opened in writing mode. If the file does not exist, it will be
created.

Finally, the string entered by the user will be written to this file using the fprintf()function and the
file is closed.

Example2
#include<stdio.h>
#include<conio.h>

void main()
{
FILE *fptr;
char name[20];
int age;
float salary;

/* open for writing */


fptr = fopen("emp.txt", "w");

if (fptr == NULL)
{
printf("File does not exist.\n");
return;
}
printf("Enter the name:\n");
scanf("%s", name);
fprintf(fptr, "Name = %s\n", name);

Comp211 4
printf("Enter the age:\n");
scanf("%d", &age);
fprintf(fptr, "Age = %d\n", age);

printf("Enter the salary:\n");


scanf("%f", &salary);
fprintf(fptr, "Salary = %.2f\n", salary);

fclose(fptr);
}

Comp211 5

You might also like