{"id":770,"date":"2022-08-30T15:06:53","date_gmt":"2022-08-30T15:06:53","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/why-we-use-file-instead-of-file-for-i-o-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:06:53","modified_gmt":"2022-08-30T15:06:53","slug":"why-we-use-file-instead-of-file-for-i-o-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/why-we-use-file-instead-of-file-for-i-o-collection-of-common-programming-errors\/","title":{"rendered":"why we use FILE * instead of FILE for I\/O-Collection of common programming errors"},"content":{"rendered":"<p>Today I am learning things about Standard I\/O of C. When I opened the stdio.h file found that:<\/p>\n<pre><code>typedef struct _iobuf FILE;\n<\/code><\/pre>\n<p>and when check the defination of struct _iobuf found that:<\/p>\n<pre><code>struct _iobuf {\n    char *_ptr;\n    int   _cnt;\n    char *_base;\n    int   _flag;\n    int   _file;\n    int   _charbuf;\n    int   _bufsiz;\n    char *_tmpfname;\n};\n<\/code><\/pre>\n<p>To understand more, I have given descriptions about each don&#8217;t whether it is correct or not<\/p>\n<pre><code>struct _iobuf {\n    char *_ptr;      \/* next character position *\/\n    int   _cnt;      \/* characters left *\/\n    char *_base;     \/* location of buffer *\/\n    int   _flag;     \/* File status flags *\/\n    int   _file;\n    int   _charbuf;   \/*Data transfer buffer *\/\n    int   _bufsiz;    \/* Buffer size *\/\n    char *_tmpfname;  \/* Temporary file indicator *\/\n};\n<\/code><\/pre>\n<p>Now having two questions in my mind?<\/p>\n<p>Q1: Have I provided the correct Names and how structure help in I\/O and if I add or delete any thing what would happen? Does that would work accordingly? Does the sequence provided here matters?<\/p>\n<p>Q2: There is no pointer used here but why use FILE * for opening the File?<\/p>\n<ol>\n<li>\n<blockquote>\n<p>Have I provided the correct Names?<\/p>\n<\/blockquote>\n<p>These are all internal details that are specific to the Microsoft implementation, and AFAIK, undocumented.<\/p>\n<blockquote>\n<p>if I add or delete any thing what would happen?<\/p>\n<\/blockquote>\n<p>That would be really bad; you&#8217;d probably be causing <strong>undefined behaviour<\/strong>.<\/p>\n<blockquote>\n<p>There is no pointer used here but why use FILE * for opening the File?<\/p>\n<\/blockquote>\n<p>Because from the point-of-view of your application code, the implementation details don&#8217;t matter; <code>FILE *<\/code> is intended to be an <em>opaque pointer<\/em>.<\/p>\n<\/li>\n<li>\n<p>A1. Editing a standard header would result in undefined behaviour.<\/p>\n<p>A2. Structs are usually passed as pointers in C to avoid copying. Also, it&#8217;s meant to act as a handle or an opaque pointer.<\/p>\n<p>Bigger question is why would want to do anything you are asking about.<\/p>\n<\/li>\n<li>\n<p>Answers to all your questions:<\/p>\n<p>The Standard C Library by P.J. Plauger<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 22:41:29. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>Today I am learning things about Standard I\/O of C. When I opened the stdio.h file found that: typedef struct _iobuf FILE; and when check the defination of struct _iobuf found that: struct _iobuf { char *_ptr; int _cnt; char *_base; int _flag; int _file; int _charbuf; int _bufsiz; char *_tmpfname; }; To understand more, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-770","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/770","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=770"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/770\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=770"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=770"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=770"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}