{"id":23224,"date":"2023-12-01T11:30:16","date_gmt":"2023-12-01T11:30:16","guid":{"rendered":"https:\/\/linuxways.net\/?p=23224"},"modified":"2024-03-25T02:10:29","modified_gmt":"2024-03-25T02:10:29","slug":"check-file-exists-c-language","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/scripting\/check-file-exists-c-language\/","title":{"rendered":"Check If File Exists in C Language"},"content":{"rendered":"<div id=\"wpbody\">In programming, there are cases where we need to verify the existence of a particular directory or file, either to use it or simply to verify its integrity, composition, or the permissions it has.In this Linux Ways article, you will learn how to check the existence of files with the access() function. We&#8217;ll show you its syntax and explain how it works, the calling method, and the type of arguments it uses.<\/p>\n<p>Then, we&#8217;ll look at this function through practical examples with code and images so you can better understand this function and use it quickly.<\/p>\n<h2><a id=\"post-23224-_inobkwurm9du\"><\/a>Access() Function to Check the Existence of Files<\/h2>\n<p>The access() function queries the existence of a file or directory and the associated read, write, and execute permissions. In the following, we will see the prototype of this function:<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;\"><div class=\"c codecolorer\"><span class=\"kw4\">int<\/span> access<span class=\"br0\">&#40;<\/span><span class=\"kw4\">const<\/span> <span class=\"kw4\">char<\/span> <span class=\"sy0\">*<\/span>pathname<span class=\"sy0\">,<\/span> <span class=\"kw4\">int<\/span> mode<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><\/div><\/div>\n<p>This function queries the existence and attributes of the file or directory that is specified by its absolute or relative path in the pathname argument. The path must be specified by a string or a pointer to it that contains the path, filename, and extension that you want to query.<\/p>\n<p>The type of query is specified in the mode input argument by a flag or the result of an OR operation between several of them. The following is a table with the available queries in the access() function:<\/p>\n<table>\n<thead>\n<tr>\n<th><strong>Flag<\/strong><\/th>\n<th><strong>Description<\/strong><\/th>\n<\/tr>\n<tr>\n<th><strong>F_OK<\/strong><\/th>\n<th>Check the existence of a file<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><strong>R_OK<\/strong><\/td>\n<td>Check if the file has read permissions<\/td>\n<\/tr>\n<tr>\n<td><strong>W_OK<\/strong><\/td>\n<td>Check if the file has write permissions<\/td>\n<\/tr>\n<tr>\n<td><strong>X_OK<\/strong><\/td>\n<td>Check if the file has executable permissions<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>When querying the F_OK, the access() function returns a result that is equal to 0 if the file exists, or -1 if it does not. Similarly, it returns 0 if the file has the requested write, read, or access permissions. Otherwise, it returns -1.<\/p>\n<p>This function is declared in the \u201cunistd.h\u201d header. So, to use the access() function, we need to include it in our code as follows:<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;\"><div class=\"c codecolorer\"><span class=\"co2\">#include &lt;unistd.h&gt;<\/span><\/div><\/div>\n<h2>How to Check the Existence of a File<\/h2>\n<p>In this example, we will see how to verify the existence of the \u201cexample.txt\u201d file in the \u201cDocuments\u201d directory. To do this, we create this file using the Linux file manager.<\/p>\n<p>In the code that we will use for this example, we will include the \u201cstdio.h\u201d and \u201cunistd.h\u201d headers and open a main() function of type void. In it, we declare the \u201ci&#8221; integer and the path pointer of type char to which we assign the string with the relative path of the file.<\/p>\n<p>After declaring the variables, we call the access() function and pass the path string as the first input argument and the F_OK flag as the second argument.<\/p>\n<p>The output argument is the \u201ci&#8221; integer which we then use with the returned value in an if-else condition to print a message that indicates whether or not the specified file exists. Let\u2019s see the code for this example:<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;\"><div class=\"c codecolorer\"><span class=\"co2\">#include &lt;stdio.h&gt;<\/span><br \/>\n<br \/>\n<span class=\"co2\">#include &lt;unistd.h&gt;<\/span><br \/>\n<br \/>\n<span class=\"kw4\">void<\/span> main <span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span><br \/>\n<span class=\"kw4\">char<\/span> <span class=\"sy0\">*<\/span>path <span class=\"sy0\">=<\/span><span class=\"st0\">&quot;\/home\/linuxways\/Documents\/example.txt&quot;<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw4\">int<\/span> i<span class=\"sy0\">;<\/span><br \/>\ni <span class=\"sy0\">=<\/span> access<span class=\"br0\">&#40;<\/span>path<span class=\"sy0\">,<\/span> F_OK<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw1\">if<\/span> <span class=\"br0\">&#40;<\/span>i <span class=\"sy0\">==<\/span> <span class=\"nu0\">0<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; <a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/printf.html\"><span class=\"kw3\">printf<\/span><\/a> <span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;The file %s exists<span class=\"es1\">\\n<\/span>&quot;<\/span> <span class=\"sy0\">,<\/span> path<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span> &nbsp; <br \/>\n<span class=\"kw1\">else<\/span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br \/>\n&nbsp; &nbsp; <a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/printf.html\"><span class=\"kw3\">printf<\/span><\/a> <span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;The file %s does not exists<span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"sy0\">,<\/span> path<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span> &nbsp; <br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/div>\n<p>In the following illustration, we will see the compilation and execution of this code. As can be seen in the figure, the access() function returns 0 which means that the \u201cexample.txt\u201d file exists in the \u201cDocuments\u201d directory.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"768\" class=\"wp-image-23226\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23224-1.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23224-1.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23224-1-300x225.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23224-1-768x576.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<p>Now, we specify the name of a non-existent file in the \u201cpath\u201d string, compile it, and run the code.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"768\" class=\"wp-image-23227\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23224-2.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23224-2.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23224-2-300x225.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23224-2-768x576.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<p>As we can see in the figure, the access() function returns a result that is equal to -1 when a non-existent file is specified in the string path.<\/p>\n<h2><a id=\"post-23224-_lu58t9tem2cf\"><\/a>How to Check the Permissions of a File<\/h2>\n<p>In this example, we&#8217;ll look at how we can check the permissions of a file using the access() function. To do this, we use the code from the previous example and replace the F_OK flag with the W_OK flag in the mode input argument. We also change the messages that this code displays to match the query. Let\u2019s see the following code for this example:<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;\"><div class=\"c codecolorer\"><span class=\"co2\">#include &lt;stdio.h&gt;<\/span><br \/>\n<br \/>\n<span class=\"co2\">#include &lt;unistd.h&gt;<\/span><br \/>\n<br \/>\n<span class=\"kw4\">void<\/span> main <span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span><br \/>\n<span class=\"kw4\">char<\/span> <span class=\"sy0\">*<\/span>path <span class=\"sy0\">=<\/span><span class=\"st0\">&quot;\/home\/linuxways\/Documents\/example.txt&quot;<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw4\">int<\/span> i<span class=\"sy0\">;<\/span><br \/>\ni <span class=\"sy0\">=<\/span> access<span class=\"br0\">&#40;<\/span>path<span class=\"sy0\">,<\/span> W_OK<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw1\">if<\/span> <span class=\"br0\">&#40;<\/span>i <span class=\"sy0\">==<\/span> <span class=\"nu0\">0<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; <a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/printf.html\"><span class=\"kw3\">printf<\/span><\/a> <span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;The file %s has write permissions<span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"sy0\">,<\/span> path<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span> &nbsp; <br \/>\n<span class=\"kw1\">else<\/span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br \/>\n&nbsp; &nbsp; &nbsp;<a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/printf.html\"><span class=\"kw3\">printf<\/span><\/a> <span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;The file %s does not have write permissions<span class=\"es1\">\\n<\/span>&quot;<\/span> <span class=\"sy0\">,<\/span> path<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span> &nbsp; <br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/div>\n<p>The following image shows the execution of this code. As we can see, the access() function has determined that the \u201cexample.txt\u201d file has write permissions.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"768\" class=\"wp-image-23228\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23224-3.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23224-3.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23224-3-300x225.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23224-3-768x576.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<p>Now, we go to the Linux manager and enter the properties of the file. In the properties, we go to the \u201cPermissions\u201d menu where we set up the read-only access. After that, we run the compilation again.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"768\" class=\"wp-image-23229\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23224-4.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23224-4.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23224-4-300x225.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23224-4-768x576.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<p>As we can see in the figure, the access() function in this execution has determined that the file has no write permissions.<\/p>\n<h2><a id=\"post-23224-_vpjp6l3a7ti0\"><\/a>How to Check the Existence of a Directory<\/h2>\n<p>In the same way that it queries the existence of a file, the access() function can also query the existence of a directory.<\/p>\n<p>The code for this is the same as in the previous example, except that instead of specifying the path of a file with its name and extension, we specify the directory path. Let\u2019s look at the code to check the existence of the \u201cDocuments\u201d directory:<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;\"><div class=\"c codecolorer\"><span class=\"co2\">#include &lt;stdio.h&gt;<\/span><br \/>\n<br \/>\n<span class=\"co2\">#include &lt;unistd.h&gt;<\/span><br \/>\n<br \/>\n<span class=\"kw4\">void<\/span> main <span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span><br \/>\n<span class=\"kw4\">char<\/span> <span class=\"sy0\">*<\/span>path <span class=\"sy0\">=<\/span><span class=\"st0\">&quot;\/home\/linuxways\/Documents&quot;<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw4\">int<\/span> i<span class=\"sy0\">;<\/span><br \/>\ni <span class=\"sy0\">=<\/span> access<span class=\"br0\">&#40;<\/span>path<span class=\"sy0\">,<\/span> F_OK<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw1\">if<\/span> <span class=\"br0\">&#40;<\/span>i <span class=\"sy0\">==<\/span> <span class=\"nu0\">0<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; <a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/printf.html\"><span class=\"kw3\">printf<\/span><\/a> <span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;The directory %s exists<span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"sy0\">,<\/span> path<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span> &nbsp; <br \/>\n<span class=\"kw1\">else<\/span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br \/>\n&nbsp; &nbsp; <a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/printf.html\"><span class=\"kw3\">printf<\/span><\/a> <span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;The directory %s does not exists<span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"sy0\">,<\/span> path<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span> &nbsp; <br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/div>\n<p>The following image shows the execution of this code which queries the existence of the \u201cDocuments\u201d directory:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"768\" class=\"wp-image-23230\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23224-5.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23224-5.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23224-5-300x225.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23224-5-768x576.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<h2><a id=\"post-23224-_6kqn6bjljd3i\"><\/a>Conclusion<\/h2>\n<p>In this Linux Ways article, we showed you how to check the existence of files and directories using the access() function. We looked at the syntax of this function, its calling method, and the ways to check the existence of files and directories as well as their read, write, and execute permissions. We then applied this function in practical examples where we can see this simple function in action by performing the queries of various types.\n<\/p><\/div>","protected":false},"excerpt":{"rendered":"<p>Guide on how to check the existence of files with the access() function, its syntax, the calling method, and the type of arguments it uses along with examples.<\/p>","protected":false},"author":110,"featured_media":23231,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[168],"tags":[],"class_list":["post-23224","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/23224","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/110"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=23224"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/23224\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/23231"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=23224"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=23224"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=23224"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}