{"id":3556,"date":"2024-03-06T11:33:17","date_gmt":"2024-03-06T11:33:17","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=3556"},"modified":"2024-03-06T11:33:17","modified_gmt":"2024-03-06T11:33:17","slug":"c-fopen-function-with-examples","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/c-fopen-function-with-examples\/","title":{"rendered":"C fopen() Function With Examples"},"content":{"rendered":"\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\" id=\"rank-math-toc\"><p>Table of Contents<\/p><nav><ul><li ><a href=\"#c-fopen-function-with-examples\">C fopen() Function With Examples<\/a><\/li><li ><a href=\"#table-lists-valid-mode-of-operation-values-in-c-with-their-meaning\">\u00a0Table lists valid mode_of_operation values in C with their meaning<\/a><ul><li ><a href=\"#return-value\">Return Value<\/a><\/li><li ><a href=\"#example-of-fopen\">Example of fopen()<\/a><\/li><\/ul><\/li><li ><a href=\"#faq-c-fopen-function-with-examples\">FAQ- C fopen() Function With Examples<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"c-fopen-function-with-examples\">C fopen() Function With Examples<\/h2>\n\n\n\n<p>The&nbsp;<strong>fopen() method<\/strong>&nbsp;in C is a library function that functions to open a file to perform various operations which include reading, writing, etc. along with various modes. If the file exists, then the fopen() function will open the particular file a new file is created.<\/p>\n\n\n\n<p>Syntax<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>FILE *fopen(const char *file_name, const char *mode_of_operation);\n<\/code><\/pre>\n\n\n\n<p>Parameters<\/p>\n\n\n\n<p>The method takes two parameters, both of character pointer type:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>file_name:<\/strong> This parameter, of C string type, holds the name of the file to be opened.<\/li>\n\n\n\n<li><strong>mode_of_operation:<\/strong> Also a C string type, this parameter indicates the mode of file access, specifying whether the file will be opened for reading, writing, or both.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"table-lists-valid-mode-of-operation-values-in-c-with-their-meaning\"><strong>&nbsp;Table lists valid mode_of_operation values in C with their meaning<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th><strong>Opening Modes<\/strong><\/th><th><strong>Description<\/strong><\/th><\/tr><\/thead><tbody><tr><td><strong>r<\/strong><\/td><td>This function will Search the file. It will Open the file for reading only.&nbsp;If the file is opened successfully fopen() loads it into memory and sets up a pointer that points to the first character in it. If the file cannot be opened fopen() returns NULL.<\/td><\/tr><tr><td><strong>w<\/strong><\/td><td>This function will Search the file. If the file exists already, its contents will be overwritten. If the file doesn\u2019t exist, a new file is made. Returns NULL, if unable to open the file.&nbsp;It creates a new file for writing only(no reading).<\/td><\/tr><tr><td><strong>a<\/strong><\/td><td>This function will Searches the file.&nbsp;Opens the file for both reading and writing.&nbsp;If opened successfully, fopen() loads it into memory and sets up a pointer that points to the first character in it. Returns NULL, if unable to open the file.<\/td><\/tr><tr><td><strong>r+<\/strong><\/td><td>This function will search the file.&nbsp;Opens the file for both reading and writing.&nbsp;If it is opened successfully, open f () loads it into memory and sets up a pointer that points to the first character in it. Returns NULL, if unable to open the file.<\/td><\/tr><tr><td><strong>w+<\/strong><\/td><td>This function will Search the file. If the file exists, its contents will be overwritten. If the file doesn\u2019t exist, a new file will be formed. Returns NULL, if unable to open the file.&nbsp;The difference between w and w+ is that we can also read the file made with the w+.<\/td><\/tr><tr><td><strong>a+<\/strong><\/td><td>it functions to Search files. If the file is opened successfully fopen( ) loads it into memory and sets up a pointer that points to the last character in it. If the file doesn\u2019t exist, a new file will be formed. Returns NULL, if unable to open the file.&nbsp;The file is opened for reading and appending(writing at the end of the file).<\/td><\/tr><tr><td><strong>rb<\/strong><\/td><td>It functions to Open the&nbsp;binary file in read mode.&nbsp;If the file does not exist, the open() function returns NULL.<\/td><\/tr><tr><td><strong>wb<\/strong><\/td><td>It functions to Open the&nbsp;binary file in write mode.&nbsp;As the pointer is set to the start of the file, the&nbsp;contents are overwritten.&nbsp;If the file does not exist, a&nbsp;new file is formed.<\/td><\/tr><tr><td><strong>ab<\/strong><\/td><td>It functions to Open the&nbsp;binary file in append mode.&nbsp;The file pointer is set&nbsp;after the last character in the file. A&nbsp;new file will be made &nbsp;if no file exists with the name.<\/td><\/tr><tr><td><strong>rb+<\/strong><\/td><td>It functions to Open the&nbsp;binary file in append mode.&nbsp;The file pointer is set&nbsp;after the last character in the file. A&nbsp;new file will be made if no file exists with the name.<\/td><\/tr><tr><td><strong>wb+<\/strong><\/td><td>Open the&nbsp;binary file in read and write mode.&nbsp;Contents are overwritten if the file exists. It will be created if the file does not exist.<\/td><\/tr><tr><td><strong>ab+<\/strong><\/td><td>Open the&nbsp;binary file in read and append mode.&nbsp;A file will be created if the file does not exist.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"return-value\">Return Value<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The function is used to return a pointer to FILE if the execution succeeds else NULL is returned.&nbsp;<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-of-fopen\">Example of fopen()<\/h3>\n\n\n\n<p>On running the following command, a new file will be created by the name \u201c<strong>demo_file.txt<\/strong>\u201d with the following content:&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\n\n\/\/ C program to illustrate fopen()\n \n#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n \nint main()\n{\n \n    \/\/ pointer demo to FILE\n    FILE* demo;\n \n    \/\/ Creates a file \"demo_file\"\n    \/\/ with file access as write-plus mode\n    demo = fopen(\"demo_file.txt\", \"w+\");\n \n    \/\/ adds content to the file\n    fprintf(demo, \"%s %s %s\", \"Welcome\", \"to\",\n            \"Skillvertex\");\n \n    \/\/ closes the file pointed by demo\n    fclose(demo);\n \n    return 0;\n}\n<\/code><\/pre>\n\n\n\n<p>According to the command , a new file will be produced by the name \u201c<strong>demo_file.txt<\/strong>\u201d with the following content:&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Welcome to Skillvertex\n<\/code><\/pre>\n\n\n\n<p><strong>To view the file, Run the following code, which will open the file and display its content.<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C program to illustrate fopen()\n \n#include &lt;stdio.h&gt;\n \nint main()\n{\n \n    \/\/ pointer demo to FILE\n    FILE* demo;\n    int display;\n \n    \/\/ Creates a file \"demo_file\"\n    \/\/ with file access as read mode\n    demo = fopen(\"demo_file.txt\", \"r\");\n \n    \/\/ loop to extract every characters\n    while (1) {\n        \/\/ reading file\n        display = fgetc(demo);\n \n        \/\/ end of file indicator\n        if (feof(demo))\n            break;\n\n        \/\/ displaying every characters\n        printf(\"%c\", display);\n    }\n \n    \/\/ closes the file pointed by demo\n    fclose(demo);\n \n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Welcome to Skillvertex<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"faq-c-fopen-function-with-examples\">FAQ- C fopen() Function With Examples<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1700043553540\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1. What does fopen () function do in C?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. The <code>fopen()<\/code> function is used to open a file identified by its filename and establish a connection to it through a stream. The mode variable, represented as a character string, defines the type of access that is requested for the file, such as reading, writing, or both.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1700043561203\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2. What is the return type of the fopen () function in C *?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. The <code>fopen()<\/code> function returns a pointer to a FILE structure type, enabling access to the opened file. This pointer is crucial for performing operations on the file, such as reading or writing. It&#8217;s important to note that when working with stream files of type record, you may need to cast the FILE pointer to an RFILE pointer to use record I\/O functions effectively.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1700043572102\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3. What is the difference between fopen and open in C?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. The <code>fopen()<\/code> function in C returns a <code>FILE*<\/code> (file pointer), which is used with functions like <code>fprintf()<\/code>, <code>fscanf()<\/code>, etc. On the other hand, the <code>open()<\/code> system call returns a file descriptor (an integer), which is often used with low-level functions like <code>read()<\/code> and <code>write()<\/code><\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>C fopen() Function With Examples The&nbsp;fopen() method&nbsp;in C is a library function that functions to open a file to perform various operations which include reading, writing, etc. along with various modes. If the file exists, then the fopen() function will open the particular file a new file is created. Syntax Parameters The method takes two &#8230; <a title=\"C fopen() Function With Examples\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/c-fopen-function-with-examples\/\" aria-label=\"More on C fopen() Function With Examples\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":5417,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[42,224],"tags":[572],"class_list":["post-3556","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-interview-prep","category-question-answer","tag-c-fopen-function-with-examples","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"_links":{"self":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/3556","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/comments?post=3556"}],"version-history":[{"count":7,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/3556\/revisions"}],"predecessor-version":[{"id":7944,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/3556\/revisions\/7944"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/5417"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=3556"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=3556"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=3556"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}