{"id":3619,"date":"2024-03-06T11:38:03","date_gmt":"2024-03-06T11:38:03","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=3619"},"modified":"2024-03-06T11:38:03","modified_gmt":"2024-03-06T11:38:03","slug":"c-program-to-print-contents-of-file","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/c-program-to-print-contents-of-file\/","title":{"rendered":"C Program To Print Contents Of File"},"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-program-to-print-contents-of-file\">C Program To Print Contents Of File<\/a><\/li><li ><a href=\"#faq-c-program-to-print-contents-of-file\">FAQ- C Program To Print Contents Of File<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"c-program-to-print-contents-of-file\">C Program To Print Contents Of File<\/h2>\n\n\n\n<p>In this C program, we&#8217;re going to learn how to open a file and print out what&#8217;s inside it. It&#8217;s like peeking into a document using C! The program will demonstrate how C can easily handle files, making it a handy skill for dealing with information stored outside the program itself. By keeping things straightforward, this program is a great starting point for grasping the basics of working with files in C. Let&#8217;s look further and explore the contents of a file using the simplicity of C programming!<\/p>\n\n\n\n<p><a href=\"http:\/\/www.cplusplus.com\/reference\/cstdio\/fopen\/\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">fopen()<\/a>&nbsp;is basically functions  to open the file , whereas <a href=\"http:\/\/www.cplusplus.com\/reference\/cstdio\/fclose\/\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">fclose()<\/a>&nbsp;is used to close a file in C.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt; \n#include &lt;stdlib.h&gt; \/\/ For exit() \n  \nint main() \n{ \n    FILE *fptr; \n  \n    char filename&#91;100], c; \n  \n    printf(\"Enter the filename to open \\n\"); \n    scanf(\"%s\", filename); \n  \n    \/\/ Open file \n    fptr = fopen(filename, \"r\"); \n    if (fptr == NULL) \n    { \n        printf(\"Cannot open file \\n\"); \n        exit(0); \n    } \n  \n    \/\/ Read contents from file \n    c = fgetc(fptr); \n    while (c != EOF) \n    { \n        printf (\"%c\", c); \nc = fgetc(fptr); \n    } \n  \n    fclose(fptr); \n    return 0; \n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Enter the filename to open\na.txt\n\/*Contents of a.txt*\/<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"faq-c-program-to-print-contents-of-file\">FAQ- C Program To Print Contents Of File<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1700463581631\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1. How to display the contents of a file in C?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. We have to Open a file using the function fopen() and then store the reference of the file in a FILE pointer.<strong>\u00a0<\/strong>Hence, its possible to Read contents of the file using any of these functions such as  fgetc(), fgets(), fscanf(), or fread(). Later, close the file using the function fclose().<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1700463599333\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2. How to print something from a file in C?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. We create a <code>FILE<\/code> pointer variable named <code>file<\/code>.<br \/>Open the file &#8220;example.txt&#8221; in write mode (&#8220;w&#8221;) to write content using <code>fprintf<\/code>.<br \/>Close the file after writing.<br \/>Open the same file in read mode (&#8220;r&#8221;) to read content using <code>fscanf<\/code>.<br \/>Close the file after reading.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1700463603948\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3. How to use Fputs in C?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. fputs() is referred to as the standard C library which will write a string of characters to the file at the location indicated by the file pointer. Given below is the declaration of the fputs() function: int fputs (const char * str, FILE * stream); The fputs() function returns 0 if the string is written to the file successfully.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>C Program To Print Contents Of File In this C program, we&#8217;re going to learn how to open a file and print out what&#8217;s inside it. It&#8217;s like peeking into a document using C! The program will demonstrate how C can easily handle files, making it a handy skill for dealing with information stored outside &#8230; <a title=\"C Program To Print Contents Of File\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/c-program-to-print-contents-of-file\/\" aria-label=\"More on C Program To Print Contents Of File\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":3620,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[594],"class_list":["post-3619","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-programming","tag-c-program-to-print-contents-of-file","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\/3619","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=3619"}],"version-history":[{"count":6,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/3619\/revisions"}],"predecessor-version":[{"id":7950,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/3619\/revisions\/7950"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/3620"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=3619"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=3619"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=3619"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}