Thread: question about file

  1. #1
    Registered User
    Join Date
    Oct 2015
    Posts
    11

    question about file

    FILE pointer is struct data type which has been defined in standard library stdio.h. It has been defined in stdio.h as
    Code:
    typedef struct _iobuf
    {
        char*    _ptr;
        int    _cnt;
        char*    _base;
        int    _flag;
        int    _file;
        int    _charbuf;
        int    _bufsiz;
        char*    _tmpfname;
    } FILE;
    in code blocks;
    my question is that; every member as char *ptr or int _cnt and ..so on .what it means?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,078
    Quote Originally Posted by sweezy
    in code blocks;
    my question is that; every member as char *ptr or int _cnt and ..so on .what it means?
    Do you really need to know this? I mean, all you mentioned was the IDE: nothing about the compiler and standard library implementation, nothing about the OS. At the level that you seem to be operating, perhaps it would suffice to treat FILE as an abstraction and just use the standard library functionality.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by sweezy View Post

    my question is that; every member as char *ptr or int _cnt and ..so on .what it means?
    Could you clarify your question? There are char* members as well as int members in the struct you quoted. Naming the members beginning with a '_' is a naming convention sometimes used to mean "private member". It means that the user should not manipulate these members directly; rather the user should only use the associated stream functions (fopen, fclose, fread, etc.) to manipulate them.

  4. #4
    Registered User
    Join Date
    Oct 2015
    Posts
    11
    why i asked this question ?..u know i found this question about c programng interview and it was that :

    (9)What will be output of following program?
    Code:
    #include<stdio.h>
    int main(){
        char c;
        FILE *fp;
        
        fp=fopen("test.txt","r");
        printf("\n %x",fp->flags);
        fp=fopen("test.txt","rb+");
        printf("\n %x",fp->flags);
        fclose(fp);
        return 0;
    }
    Output: 5 47
    Explanation:
    In stdio.h there are flag status macros which are:
    NAME Meaning Value (In hexadecimal)_F_RDWR Read and write 0X0003
    _F_READ Read-only file 0X0001
    _F_WRIT Write-only file 0X0002
    _F_BUF Malloc'ed buffer data 0X0004
    _F_LBUF Line-buffered file 0X0008
    _F_ERR Error indicator 0X0010
    _F_EOF EOF indicator 0X0020
    _F_BIN Binary file indicator 0X0040
    _F_IN Data is incoming 0X0080
    _F_OUT Data is outgoing 0X00100
    _F_TERM File is a terminal 0X00200
    and i definitely didn't understand this and for that i asked what it means every member (flags and_ptr and ...so on).

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,078
    Quote Originally Posted by sweezy
    why i asked this question ?..u know i found this question about c programng interview and it was that :

    (9)What will be output of following program?
    The C standard does not guarantee that a FILE object has any member named flags, and even if an implementation is such that a FILE object does have a member named flags, since FILE is pretty much an opaque type, that member might not be accessible in the way given in the sample code. Therefore, a right approach to answer the interview question would be to point these out, and note that the program might not even compile. Likewise, the claim that "In stdio.h there are flag status macros which are" is very implementation specific: the standard does not guarantee any such macros.

    That said, if your interview is for a position where you are expected to work with something very implementation specific, then perhaps you would indeed be expected to know these implementation details, but if so the fact that you "definitely didn't understand this" means that you really are a poor candidate for the job.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File IO question
    By Digital Android in forum C Programming
    Replies: 1
    Last Post: 04-29-2014, 07:53 AM
  2. File Question
    By kolliash in forum C Programming
    Replies: 9
    Last Post: 06-10-2008, 10:14 AM
  3. Newbish Question file reading question....
    By kas2002 in forum C Programming
    Replies: 23
    Last Post: 05-17-2007, 12:06 PM
  4. File I/O question... please help!
    By sirSolarius in forum C++ Programming
    Replies: 11
    Last Post: 10-07-2003, 08:30 AM
  5. File question-Testing if file exists
    By fuh in forum C++ Programming
    Replies: 2
    Last Post: 01-28-2003, 07:11 PM

Tags for this Thread