LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 01-19-2010, 10:35 AM   #16
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682

look at setenv() for setting or creating an environment variable in a c program. There isn't a need to call a C program simply to set an environmental variable. You can assign a variable and export that variable in a bash script.

Last edited by jschiwal; 01-19-2010 at 11:55 PM.
 
Old 06-01-2010, 11:18 PM   #17
allanf
Member
 
Registered: Sep 2008
Location: MN
Distribution: Gentoo, Fedora, Suse, Slackware, Debian, CentOS
Posts: 100
Blog Entries: 1

Rep: Reputation: 20
Quote:
Originally Posted by carbonfiber View Post
What's your point? And why are you using fprintf?

Well this is an open question that can be taken at least two ways.


1) Why are you using a "fprintf" rather than an exit code?

Well an exit code can only contain the values of 0-255 for one thing
and since bash allows the assignment of stdout of a command, I choose the most general case which is to return a value in via stdout and hence a print. But please continue to read the next segment if this is the question that you were asking.



2) Why are you using a "fprintf" rather than a "printf"?


I always write C code where the "File Descriptor" (For C++ the "stream") is passed. This is because in Unix/Linux old-timers find code written that use the "File Descriptor/stream" is much more flexible than a function that passes in the file name.

For example you have a compressed file that is currently 2 Gigabytes of space. You know that it is just text. By passing the file descriptor, the file does not need to be de-compressed onto the hard drive and then read. The "popen" function can be used to get the field descriptor of the pipe that is de-compressing and read via the pipe. Remember that a pipe blocks the producer when the consumer is too slow, so it does not fill up the disk space. When a younger developer writes the function with a file name, the compressed file must be decompressed prior to reading it.

So I always used the fXXX i/o functions in my C code. I am not a lazy coder because the re-use of code well written can save much more time than using "printf" rather than "printf".



Does this help you understand why I used printf.
 
Old 06-02-2010, 05:54 PM   #18
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Quote:
Originally Posted by jschiwal View Post
look at setenv() for setting or creating an environment variable in a c program. There isn't a need to call a C program simply to set an environmental variable. You can assign a variable and export that variable in a bash script.
You can use this method to pass data to a child process, but not to a parent process. A copy of an environment variable that is created by a shell will be visible to a C program launched by the shell. Any modification to to the environment variable by the C program will not be seen by the parent shell, because the C program sees only a copy of the one owned by the parent shell.

The possibilities for communicating between a parent and child process are described very well in

Beej's Guide to Unix Interprocess Communication

--- rod.
 
  


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
Bash Script Passing variable to Function nutthick Programming 2 02-02-2005 05:15 AM
return value from shell script to c code? khucinx Programming 1 05-13-2004 03:43 PM
GNU wget return codes for shell script greenhornet Programming 3 05-09-2004 07:51 PM
How to change function parameter value and return back to the main shell program Bassam Linux - General 1 01-26-2004 10:02 AM
return key on shell script chupacabra Programming 2 10-22-2002 12:11 PM

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

All times are GMT -5. The time now is 10:06 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