#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    4
    Rep Power
    0

    File handling in C


    Hi,

    I am new here and this is my first post,i am learning file handling in C and got a problem in executing the following code.it showed an error indicating that fd isn't define in FILE structure.

    Thanks and regards,
    Mustafiz


    Code:
    void main()
    {
    	FILE *fptr;
    	int count=1;
    	while(count<100)
    	{
    		fptr=fopen("new_file.txt","r");
    		if(fptr== NULL)
    		{
    			printf("cant open");
    			break;
    		}
    		else
    		{
    			printf("file no. : %d\t",count++);
    			printf("%d", fptr->fd);
    		}
    	}
    }
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,379
    Rep Power
    1871
    FILE is an opaque type, you're not supposed to know anything about what it contains. This is why all you have is a FILE* variable.

    If you want to get the underlying descriptor, then use fileno

    Oh, and main returns an int, not void.
    See here and here

    Good job on using code tags on your first post :)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    4
    Rep Power
    0
    FILE typedef structure includes some of the member variables as follows..

    Code:
    typedef struct{
         short level;
         unsigned flags;
         char fd;//this is the fd which i need to access,but i couldn't.
         unsigned char hold;
    //similar statements
    
    }FILE;
    in the meanwhile i am going through the link which you provided to know the fd,just wanted to mention from the above posted structure i didn't get the output.

    Thanks,
    Mustafiz
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    4
    Rep Power
    0
    I got how to know the fd.

    Thanks
    Mustafiz
  8. #5
  9. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,379
    Rep Power
    1871
    > FILE typedef structure includes some of the member variables as follows..
    Maybe so, but try compiling your code on another compiler, and you'll find different member variables there.

    That's the whole point of having a FILE* - it saves you from having to worry about the detail by providing you with an API centred around using FILE*.

    > I got how to know the fd.
    And I told you how to get the fd from a FILE* - what's your question?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    4
    Rep Power
    0
    I meant my doubt got cleared and i moved forward in writing,reading by creating new files too.

    Thanks for the help,will post if any doubts arises.:)

    mustafiz

IMN logo majestic logo threadwatch logo seochat tools logo