LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-13-2005, 06:22 AM   #1
Thinking
Member
 
Registered: Oct 2003
Posts: 249

Rep: Reputation: 30
switching between descriptor and file pointer?


hiho@ll

i suddenly noticed that i can't do such code:

Code:
/*
 int descr for my problem it was a fifo O_RDONLY descriptor
*/
int test(int descr){
 char buff[1024]={0};
 FILE *fp=fdopen(descr,"r");
 fgets(buff,1023,fp);            // P1
 int len=read(descr,buff,1023);  // P2
 return len;
}
P2 will return 0
if i replace P2 with fread(buff,sizeof(char),1023,fp); it works

adding the line
descr=fileno(fp); between P1 and P2 doesn't work too (btw: descr == fileno(fp))

Q1: why can't i use the descriptor if i did a fdopen?
Q2: is it possible to use the descriptor after using fdopen? if yes, how?
Q2.2: is it possible to mix the usage of descriptor and file pointer?

thx@ll
 
Old 12-13-2005, 08:21 AM   #2
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
use fileno(descr)
 
Old 12-13-2005, 10:08 AM   #3
Thinking
Member
 
Registered: Oct 2003
Posts: 249

Original Poster
Rep: Reputation: 30
hmm
i didn't tested it but i'm not sure if it can work as you described
have a look at the filno prototype

int fileno(FILE *stream);
defined in stdio.h

it wants a FILE* and not an int

so fileno(descr) can't work, not?
 
Old 12-13-2005, 10:36 AM   #4
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
My bad - FILE * is a struct pointer.
fileno(fp) is what you should use in your code fragment above.

try man open
 
Old 12-13-2005, 11:07 AM   #5
Thinking
Member
 
Registered: Oct 2003
Posts: 249

Original Poster
Rep: Reputation: 30
i tried with fileno

i ask differently and more noobish ;-)

why does this code don't work?
Code:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

int main(int argc,char **argv){

 FILE *fp=fopen("readtest.cpp","r");
 int descr=fileno(fp);

 char buffer[100]={0};
 fread(buffer,sizeof(char),99,fp);
 printf("%s",buffer);

 memset(buffer,0,100);

 int i=read(descr,buffer,99);  // LX
 printf("\nREAD: %d %s\n",i,buffer);
 exit(0);
}
on line X (LX) read returns 0 and buffer isn't changed in any way
why?

thx@ll
 
Old 12-13-2005, 11:13 AM   #6
Thinking
Member
 
Registered: Oct 2003
Posts: 249

Original Poster
Rep: Reputation: 30
i just found this

http://www.delorie.com/gnu/docs/glibc/libc_242.html

but i'm not sure if i understand right

if i read the article i think that the only difference between FILE pointer and descriptor is their different "file position" handling
file pointer and descriptor use different offsets
right?
if yes, why read(); returns 0 in the above example and doesn't return the same as fread returns?

thx@ll
 
Old 12-13-2005, 06:42 PM   #7
primo
Member
 
Registered: Jun 2005
Posts: 542

Rep: Reputation: 34
Try using setbuf(fp, NULL) right after fopen() or fdopen(). You need to turn off any buffering and I guess that fread() is filling one buffer and making read() return 0 (EOF), in the case your file is large enough...
 
Old 12-14-2005, 12:46 AM   #8
Thinking
Member
 
Registered: Oct 2003
Posts: 249

Original Poster
Rep: Reputation: 30
@primo
damn, youre absolutly right
using this code
Code:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

int main(int argc,char **argv){

 FILE *fp=fopen("readtest.cpp","r");
 setbuf(fp,NULL);

 int descr=fileno(fp);

 char buffer[100]={0};
 fread(buffer,sizeof(char),99,fp);
 printf("%s",buffer);

 memset(buffer,0,100);

 int i=read(descr,buffer,99);
 printf("\nREAD: %d %s\n",i,buffer);
 exit(0);
}
it worked perfectly and i only added setbuf(fp,NULL);

just for understanding:
fread seems to buffer the input data
the problem was that my readtest.cpp was too small in size, so it was read completely by the fread function and the read thought it was at the end of the file
using a bigger file "read" continues reading where the buffer of fread ends (it's not necessaraliy the fread buffer parameter)

to mix both fread and read you have to set the file pointer fp to be unbuffered using as primo said
setbuf(fp,NULL);

thx
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Get the absolute path of file using file descriptor. appas Programming 7 01-19-2012 11:47 AM
apt-file returns nothing; 'bad file descriptor' overbored Debian 3 10-03-2004 09:13 PM
[c++] How can I get a file descriptor from a c++ i/o stream? chuanyung Programming 8 03-08-2004 11:23 PM
File descriptor lido Linux - Newbie 5 07-17-2003 11:58 AM
Strange pointer after switching X <-> tty Florio Linux - Software 5 12-16-2002 10:11 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:32 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration