FILE structure - stdio.h


 
Thread Tools Search this Thread
Top Forums Programming FILE structure - stdio.h
# 1  
Old 12-17-2010
FILE structure - stdio.h

Hi All,

I am new to linux and Programming.
Inside the file stdio.h, there is a description about FILE structure. Which has many internal data members like _p, _r, _flags etc.

I have written a sample code to find out the contents of the FILE structure.
It opens a sample file ( FILE *fp ), does some read/write operations on it. and at the end prints
fp->_p, fp->_r etc. contents.

But everytime it prints 0, or NULL for these values. !!!

Any idea.?

Thanks,
Nikunj
# 2  
Old 12-17-2010
The contents of the FILE struct are implementaton defined. Meaning they can change any time the C maintainers or OS developers need to do that.

This is Solaris 9's idea of a FILE struct. I would dig thru you /usr/include file tree and find what your version looks like. Plus, consider chacking the internal file pointer and other struct members after you have done just a single read operation - like fgets. If you wait until EOF, you get NULL pointers.
Code:
struct __FILE_TAG       /* needs to be binary-compatible with old versions */
{
#ifdef _STDIO_REVERSE
        unsigned char   *_ptr;  /* next character from/to here in buffer */
        ssize_t         _cnt;   /* number of available characters in buffer */
#else
        ssize_t         _cnt;   /* number of available characters in buffer */
        unsigned char   *_ptr;  /* next character from/to here in buffer */
#endif
        unsigned char   *_base; /* the buffer */
        unsigned char   _flag;  /* the state of the stream */
        unsigned char   _file;  /* UNIX System file descriptor */
        unsigned        __orientation:2; /* the orientation of the stream */
        unsigned        __ionolock:1;   /* turn off implicit locking */
        unsigned        __seekable:1;   /* is file seekable? */
        unsigned        __filler:4;
};

# 3  
Old 12-19-2010
Thnx for replying.!
I understand that the contents of FILE structure are implementation defined. For a unix system, x86 processor. I wanted to know, How can we find contents of FILE structure ,( Which is defined in stdio.h ) for a particular file and has data members like _p, _r etc.

Suppose I create a simple file with FILE *fp. and perform some read/write operations on it.
Now when I print (fp->_p) , (fp->_r) etc. like members. I always get NULL/0 value. While the program compiles successfully. !!

Kindly answer. !
# 4  
Old 12-19-2010
Well, without seeing your code, how can we possibly advise you as to what is wrong except in general terms.
# 5  
Old 12-20-2010
Here's my sample code.


Code:
#include<stdio.h>
#include<string.h>

int main()
{
int *i, j;
size_t n;
const char *buf;
unsigned char *t;
FILE *fp;

fp = fopen("test.txt", "r+" );

buf = "This is Test String , This is Test String\n" ;

//j = strlen(buf);
//j = strlen(fp->_p);

n = fwrite(buf, 1, 10, fp);

if( fp->_p )
        printf(" Its not null");

//i = (int *)memcpy((void *)t, (const void *)buf, (size_t)(10)) ;

j = (int)fp->_r ;
t = fp->_p ;
printf("j = %d , t = %x \n", j, t);

return 0;
}

This will give error with gcc:
Code:
fwrite_test.c: In function ‘main':
fwrite_test.c:22: error: ‘FILE' has no member named ‘_p'
fwrite_test.c:27: error: ‘FILE' has no member named ‘_r'
fwrite_test.c:28: error: ‘FILE' has no member named ‘_p'

But as I said, the FILE structure defined in stdio.h has all these members. !! How do these members get values.?
# 6  
Old 12-20-2010
If they're all not defined, what's this NULL value thing you were talking about?

I suspect the struct you're seeing is not really FILE but _FILE or something like that. FILE itself will be an opaque type, because you're really not supposed to use anything inside. You'll need to typecast the pointer into the proper type to get at it.

How to do that, I'm not so sure, because I can't see your stdio.h from here. In my system, the structure isn't defined in stdio.h at all, but libio.h, and even then, doesn't resemble yours at all! Different versions of libc I guess. As they say, implementation-defined.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

C++ : is what the meaning of #include<stdio.h>?

Hello Guys, I'm new in programing line & just started from C++ programs but i don't know the meaning of #include<stdio.h> file....Guys tell me what's the working of this file in C++ program?? I'm confused... Title changed: Please use a descriptive subject text, and not C++?? (3 Replies)
Discussion started by: ggiwebsinfo
3 Replies

2. Programming

Ignoring the stdio.h file in a C file

I am facing a problem in the below given code. int main() { printf("\nHello Geeks\n\n") ; return 0 ; } In the above mentioned code i left including "#include ". And if i compile and execute this piece of code, the output is printed as expected. But "#include " being the most important... (7 Replies)
Discussion started by: Raj 89
7 Replies

3. Solaris

fatal error: stdio.h: No such file or directory

Trying to compile a C program recievin this hello.c:1:19: fatal error: stdio.h: No such file or directory gcc is installed on the system. echo $PATH /usr/bin:/usr/sbin:/usr/gcc/4.5/include/c++/4.5.2/tr1 root@Sol11swtb01:/media/NO NAME/Programming/C/Testing# cd... (2 Replies)
Discussion started by: Fingerz
2 Replies

4. Programming

stdio.h not found on Solaris 11

Hi friends, I hope u r doing well. I have just installed Solaris 11, and it seems that solaris 11 doesn't come with all the packages, one has to do everything manually. I download gcc from sunfreeware.com and installed it. After setting up the path variable, I tried to compile the hello world... (4 Replies)
Discussion started by: gabam
4 Replies

5. Red Hat

Copy certain file types recursively while maintaining file structure on destination?

Hi guys, I have just been bothered by a fairly small issue for some time now. I am trying to search (using find -name) for some .jpg files recursively. This is a Redhat environment with bash. I get this job done though I need to copy ALL of them and put them in a separate folder BUT I also... (1 Reply)
Discussion started by: rockf1bull
1 Replies

6. Programming

stdio.h vs unistd.h I/O

Hi guys. To work with physical files, sockets, pipes, ... which library is good? stdio or unistd stdio.h functions perform buffering and rationally should be better than unistd.h routines. but i am wondering why all UNIX programming books use unistd.h routines for almost all types of I/O... (7 Replies)
Discussion started by: majid.merkava
7 Replies

7. Programming

Atomic Read and Write with stdio

hi guys. can we use fread and fwrite with pipes to write data more than PIPE_BUF atomically since they lock FILE object until I/O operation finish? (1 Reply)
Discussion started by: majid.merkava
1 Replies

8. Programming

compare XML/flat file with UNIX file system structure

Before i start doing something, I wanted to know whether the approach to compare XML file with UNIX file system structure. I have a pre-configured file(contains a list of paths to executables) and i need to check against the UNIX directory structure. what are the various approches should i use ? I... (6 Replies)
Discussion started by: shafi2all
6 Replies

9. Programming

Search attributes in one structure using the values from another structure

Hello Groups I am trying to find out ways of comparing a value from a 'c' structure to a value in another 'C' structure. the 'C' structure can be a List or liked list as it contains lot many records. if we loop it in both the structures it is going to consume time. I am looking for a simple... (3 Replies)
Discussion started by: dhanamurthy
3 Replies

10. UNIX for Dummies Questions & Answers

Copying a Directory Structure to a new structure

Hi all Is it possible to copy a structure of a directory only. e.g. I have a file with the following entries that is a result of a find :- /dir1/dir2/file.dbf /dir1/dir2/dir3/file1.dbf /dir1/file.dbf I want to copy these to a directory and keep the structure however starting at a new dir... (8 Replies)
Discussion started by: jhansrod
8 Replies
Login or Register to Ask a Question