using a filepointer in a diffrent program


 
Thread Tools Search this Thread
Top Forums Programming using a filepointer in a diffrent program
# 1  
Old 02-15-2006
using a filepointer in a diffrent program

Hi ALL :

consider the given 2 codes:

//p1.c
#include<stdio.h>
FILE * fp;
main()
{
pid_t pid;
fp=fopen("..........");
// now i am transfering the control to a new process(p2.c).
pid=fork();
if(pid==0)
.....
execl(" p2".....);
....
}

//p2.c
extern FILE *fp;
main()
{
//here i want to write to a file using the file pointer which i declared
// in p1.c
fprintf(fp,"%s",.....);
.....
.....
}
how can i write this...
(or)


Actually , i want a file to be opened in a process(probably in an infinite
loop),and using that file pointer i want to perform another task in a diffrent
process i.e acess that file pointer in a different process and do a job.

any help will be thanked....

Last edited by bankpro; 02-15-2006 at 06:45 AM..
# 2  
Old 02-15-2006
First off: execl will clobber EVERYTHING including the open file descriptors from the parent process.

Maybe if you explained exactly what you are trying to do - not in terms of code - we could tell you how to do it. Even if you could pass descriptors down to other programs using execl() -- which you cannot -- I do not see what you gain.
# 3  
Old 02-15-2006
Open file descriptors should, by default, remain open. They will be closed only if the FD_CLOEXEC flag was set, which is generally rare. Having file descriptors open across an exec() is how the shell implements stuff like:
program < inputfile > outputfile
# 4  
Old 02-15-2006
Yes, that was not correct - exec doesn't necessarily close file descriptors.

However I still don't understand what the OP is trying to achieve.
# 5  
Old 02-16-2006
fp

actually i want to import a file pointer or file descriptor to be used in another program .

In the eg (p1.c) ,i am passing a fp through execl function to p2.c,where i am employing it to write in to a file.
# 6  
Old 02-16-2006
Quote:
Originally Posted by bankpro
actually i want to import a file pointer or file descriptor to be used in another program .

In the eg (p1.c) ,i am passing a fp through execl function to p2.c,where i am employing it to write in to a file.
i know this sounds kinda layman-ish...
u can write fp to file and read from it in p2. u can decide the name of the file before executing the progs, like "test" or something...
# 7  
Old 02-16-2006
You can also use redirection to accomplish what you want.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to add field to diffrent file using shellscript? or awk

hi, would you help me? i have file total.csv "a","e23","f" "b,"34d","g" "c","45f","f" "d","45s","f" count.csv 3 i do this : paste -d',",' total.csv count.csv but the result like this: "a,"e23","f" 3 "b,"34d","g" (1 Reply)
Discussion started by: kivale
1 Replies

2. Shell Programming and Scripting

Copy-pasted code behaves diffrent

Heyas I'm currently attempting to apply the code of tui-select to tui-list. That is because tui-list simply made a 1 string list, while tui-select uses dynamicly up to 3 strings per line. Anyway, so i copy pasted the code, and just made the changes marked with red.... Know that both scripts... (2 Replies)
Discussion started by: sea
2 Replies

3. Shell Programming and Scripting

Sed diffrent replace by occurrence

I couldn't find the answer anywhere, so I hope you could help me. I need to change something like the following: something/bla/aaaa anything/bbb to: something --bla ----aaaa anything --bbb How do I do this? Is it possible with sed? I tried various patterns, but don't know how to... (5 Replies)
Discussion started by: Patwan
5 Replies

4. Solaris

Diffrent output in cron jobs

Hi, I have issue with cron. When i run script manually output is fine but when i add it to cron output file is not as same. both file attach some junk charecter comming in cron output. thanx Jignesh (5 Replies)
Discussion started by: jkmistry
5 Replies

5. Red Hat

samba fileshare across 2 diffrent workgroup

Hi im trying to configure samba on linux server so win client can access the shared files but my win client is on diffrent domain , we recently changed our domain name but not for certain servers... i guess the samba linux server is still part of our sub domain which is why i still can access... (4 Replies)
Discussion started by: halacil
4 Replies

6. Shell Programming and Scripting

Need a sheel script to compare no of file on diffrent servers

i have serverA and serverB and i have a folder with same name an location in both servers.... Now i want to compare files in folder(serverA) with folder (serverB) if there are files missing in folderB then copy those files from folder(serverA) to folder(serverB) (1 Reply)
Discussion started by: ashahzad
1 Replies

7. Shell Programming and Scripting

diffrent results between command line and scripted grep

When I type a command at the command line it supplies one result and the exact same command in a script egrep '^01|^02|^03|^04' file > fileout count = 29353 same count in the script yields a count of 23492 is there any reason this could be happening. (1 Reply)
Discussion started by: r1500
1 Replies

8. UNIX for Dummies Questions & Answers

reconstructing a record in a diffrent order

Can sed be used to take a existing record and reverse the order of defined character placement if there is no delimeters? existing record: 0123456789CO expected result: 9876543210CO if there were delimeters I could define the delimeter and each placement would have an id which I... (1 Reply)
Discussion started by: r1500
1 Replies

9. UNIX for Advanced & Expert Users

Diffrent IP range connectivity

Hi guys, We have different IP ranges in our LAN, for instance my system IP Add. is in 163.183.37.x range, in the meantime i wish to connect to our printer/plotter which is 163.183.4.x & our LAN in another location with 163.183.38.x. What shoul i do except defining pesudo IP Add's? as a... (1 Reply)
Discussion started by: nikk
1 Replies

10. UNIX for Dummies Questions & Answers

Diffrent versions of Unix/Linux

With so many diffrent flavors out there, how is a person soposed to know what to go with? I'm not asking for specific discussion here in this forum, but I would like to see various pages and articles dicsussing the pluses and minuses of various unix/linux OS's. I have been searching various... (1 Reply)
Discussion started by: MorderKartoffel
1 Replies
Login or Register to Ask a Question