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

C LANGUAGE File Handling

The document discusses file handling in C by declaring a FILE pointer and using functions like fopen(), fclose(), fprintf(), and fgets() to create, open, read from, and write to files. It covers opening files in different modes like read ('r'), write ('w'), and append ('a') and using functions accordingly to read/write file contents.

Uploaded by

MD COMPUTERS
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 views

C LANGUAGE File Handling

The document discusses file handling in C by declaring a FILE pointer and using functions like fopen(), fclose(), fprintf(), and fgets() to create, open, read from, and write to files. It covers opening files in different modes like read ('r'), write ('w'), and append ('a') and using functions accordingly to read/write file contents.

Uploaded by

MD COMPUTERS
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/ 3

File Handling

In C, you can create, open, read, and write to files by declaring


a pointer of type FILE, and use the fopen() function:
FILE *fptr
fptr = fopen(filename, mode);
FILE is basically a data type, and we need to create a pointer variable to
work with it (fptr). For now, this line is not important. It's just something
you need when working with files.
To actually open a file, use the fopen() function, which takes two
parameters:

Parameter Description

filename The name of the actual file you want to open (or create),
like filename.txt

mode A single character, which represents what you want to


do with the file (read, write or append):
w - Writes to a file
a - Appends new data to a file
r - Reads from a file

Create a File
To create a file, you can use the w mode inside the fopen() function.

The w mode is used to write to a file. However, if the file does not exist, it will
create one for you:

Example
FILE *fptr;

// Create a file
fptr = fopen("filename.txt", "w");

// Close the file


fclose(fptr);
Note: The file is created in the same directory as your other C files, if nothing
else is specified.

On our computer, it looks like this:

fptr = fopen("C:\directoryname\filename.txt", "w");

Closing the file


Did you notice the fclose() function in our example above?

This will close the file when we are done with it.

It is considered as good practice, because it makes sure that:

 Changes are saved properly


 Other programs can use the file (if you want)
 Clean up unnecessary memory space

Write To a File
Let's use the w mode from the previous chapter again, and write something to
the file we just created.

The w mode means that the file is opened for writing. To insert content to it,
you can use the fprintf() function and add the pointer variable (fptr in our
example) and some text:

Example
#include <stdio.h>
int main() {
FILE *fptr;
// Open a file in writing mode
fptr = fopen("filename.txt", "w");
// Write some text to the file
fprintf(fptr, "Some text");
// Close the file
fclose(fptr);
return 0;
}
Read Files
In the previous chapter, we wrote to a file using w and a modes inside
the fopen() function.

To read from a file, you can use the r mode:

Example
FILE *fptr;

// Open a file in read mode


fptr = fopen("filename.txt", "r");

This will make the filename.txt opened for reading.

Example
#include <stdio.h>
int main() {
FILE *fptr;
// Open a file in read mode
fptr = fopen("filename.txt", "r");
// Store the content of the file
char myString[100];
// Read the content and store it inside myString
fgets(myString, 100, fptr);
// Print file content
printf("%s", myString);
// Close the file
fclose(fptr);
return 0;
}

You might also like