The fopen() function creates and correlates a stream with the file defined by the name of the file. The mode attribute is a sequence of bytes that indicates the sort of access to the file that has been provided. The positioning argument precedes additional keyword arguments in the mode attribute. Lowercase letters should be utilized for these positioning arguments. The keyword arguments might be specified in either upper or lower case. Quotation marks are used to separate them. There would only be a single indication of a keyword declared.

Any file accessed is frequently determined by the file title given to the fopen() function. There are a variety of file-naming guidelines that we will use to design an application. Customizable characters and readable characters are arranged into sections in text files. A newline character is being used at the end of every line. The system can introduce or transform command characters in an outcome textual stream.

Binary files are made up of a string of characters. The program will not transform control bits in input or output for these files. In this article, we’ll explore how to use the fopen() function to perform different operations in C.

Use Fopen() function:

The fopen() method usually generates a file pointer, which is used to open a defined file. We may utilize the file reference after we have accessed a file to enable the compiler to execute input and output operations on it.

#include

#include

int main()

{

    FILE *file_2 = NULL;


    file_2  = fopen (“file_2.txt”, “w x”);


    if(file_2 == NULL)


    {


        printf(“File does not createdn);


        exit(1);


    }


    else


    {


        printf(“File is createdn);


    }


    fclose(file_2);


    return 0;

}

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image1-34.png" data-lazy- height="309" src="data:image/svg xml,” width=”551″>

To start the code, we have to introduce two header files. For the declaration of input and output functions, we utilize header file. The second header file   contains methods for memory management, data type transformation, pseudo-random numeric formation, process monitoring, sorting and handling, math, and multibyte or extensive characters that are reliant and effective for C programmers.

Now in the next step, we apply the main() function and start the coding in the body of this function. We create a constructor for the file and set it to the ‘NULL’ value. Further, we declare the variable for creating the file by applying the fopen() function. This method creates a stream and accesses the file for whom the pathname seems to be the string referred to by name.

A string is referenced by the mode parameter. The file will be unlocked in the given mode ‘w x.’ This mode opens the specific file for reading and writing. In addition to this, if the mentioned file is present, this mode makes that file blank. Here “x,” is a new variant, and it is an exclusive create-and-open form. If the file is present or would not be made, accessing that file through exclusive format (“x”) fails.

Now we apply the if-else condition to check whether the defined file is created or not. If the mentioned file is equal to NULL, then the printf() function prints ‘File does not create’; otherwise printf() function prints ‘File is created.’ At the end of the code, we employ the fclose() function. The fclose() method terminates the file referred to with the file pointer ‘file_2’.

<img alt="" data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image3-31.png" height="35" src="data:image/svg xml,” width=”260″>

When we want to rewrite the defined file by using “w x” mode, the fopen() function gives NULL, avoiding the file from being overwritten. So we get this type of output.

Write the data in the file:

In C, the fopen() is a library function. It always opens the files and carries out different processes such as reading, writing, and switching between modes. Whereas if the file already exists, it is accessed; otherwise, a new file will be generated.

First of all we integrate two header files #include and #include . These are utilized for different purposes. For standard output and input, we utilize header file. This header file declares printf() and scanf() functions.

Now we are going to start the code to demonstrate the use of the fopen() function. We initialize the main() function. In the body of the function, we declare the pointer ‘file_1’ to FILE. In addition to this, we want to generate a file, so we apply the fopen() function. This function contains the parameters: name of file ‘file_1’ and the mode ‘w ’ to access that file. The mode ‘w ’ looks for a file. The data of the file will be updated when it has already existed.

However, a new file is generated only when the file cannot exist. Now the printf() function is utilized to insert the data ‘Information Technology’ to the file ‘file_1’. In the end to close the file pointed by ‘file_1’ the function fclose() is applied.

#include

#include

int main()

{

    FILE* file_1;


    file_1 = fopen(“file_1.txt”, “w “);


    fprintf(file_1, “%s %s %s”, “Information”,  “Technology”);


    fclose(file_1);


    return 0;

}

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image1-35.png" data-lazy- height="309" src="data:image/svg xml,” width=”551″>

When we run this code, a new file with the name “file_1” having the content ‘Information Technology’ will be generated.

Read the file:

Here the fopen() function is utilized to open an obtainable file. It is stated in the header file ‘’. Now, if we want to examine the file, we will utilize the subsequent program, which might access the file and show its contents.

#include

int main()

{

    FILE* file_1;


    int display;


    file_1 = fopen(“file_1.txt”, “r”);


    while (1) {


        display = fgetc(file_1);


        if (feof(file_1))


            break;


        printf(“%c”, display);


    }


    fclose(file_1);


    return 0;

}

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image5-30.png" data-lazy- height="366" src="data:image/svg xml,” width=”522″>

At the beginning of the program, we include a header file ‘’ for input and output purposes. To start the code, we have to declare the body of the main() function. Here we construct the pointer to FILE. Meanwhile, we initialize display variables having int data type. Now we make a new variable by the name of ‘file_1’.

Further, we define the fopen() function. Here we pass the file name ‘file_1’ having its access as read mode ‘r.’ By this mode, we retrieve the file. The file is then accessible for reading exclusively. If the file is successfully opened, the fopen() function imports this into the cache and creates a reference to the first word in the file.

The fopen() function then provides NULL if the file sometimes does not access. Further, we utilize a while loop to extract every word of the file. We read the file by using the fgetc() function. What this implies is that it might provide a value adequate for saving an unsigned character whenever it gets a normal word from the file. But, we can’t retrieve a letter value if we are at the end of any file; in this situation, the fgetc() function may provide “EOF,” which would illustrate that we have got the end of the file.

To display every word of the file, we apply the printf() function. In addition to this, we employ the fclose() function to close the file pointed by ‘file_1’.

<img alt="" data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image4-30.png" height="32" src="data:image/svg xml,” width=”186″>

After executing the above-mentioned program, we get the data ‘Information Technology’ of the file ‘file_1’.

Conclusion:

We see the methods of using the fopen() function to open the defined file. Then we specify the ‘mode’ parameter to this function. We have added three unique examples to elaborate on the concept of the fopen() function in the C programming language. So due to this, we write on that file or read the content of the file.

About the author

<img alt="" data-lazy-src="https://secure.gravatar.com/avatar/d014e3711df41253029f4d4199698df8?s=112&r=g" data-lazy- height="112" src="data:image/svg xml,” width=”112″>

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content