CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Is it possible to pass a stream to a function?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Darren Logan
Guest







Is it possible to pass a stream to a function?
PostPosted: Sun Feb 23, 2003 4:33 am     Reply with quote

Hello,

It is possible to pass a Stream identifier to a function?

It would save a lot of code space for me if this was possible.

Currently, I have to determine which serial port (i have 2) I need to send data out of thus (example):

void sendit(int whichport) {

if(whichport==1) {
fprintf(port1,"calling from port 1");
}

if(whichport==2) {
fprintf(port2,"calling from port 2");
}

}

what i'd really like to do is:

void sendit(int whichport) {

fprintf(whichport,"sending from port \%u",whichport);

}

What i've done doesn't work, im wondering whether a) I've done something wrong or b) You simply can't do it this way?

Answers on a postcard to "Save the ROM competition", FrustratesVille, UK

Cheers,
Darren
___________________________
This message was ported from CCS's old forum
Original Post ID: 12031
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

Re: Is it possible to pass a stream to a function?
PostPosted: Sun Feb 23, 2003 10:04 am     Reply with quote

You could you sprintf to put the data in a string and then write out to the port of choice

or

the printf command allows you to specify a function as the first parameter. The default is putc(), however, you could make your own function in which you specify the stream and then that function outputs accordingly.


:=Hello,
:=
:=It is possible to pass a Stream identifier to a function?
:=
:=It would save a lot of code space for me if this was possible.
:=
:=Currently, I have to determine which serial port (i have 2) I need to send data out of thus (example):
:=
:=void sendit(int whichport) {
:=
:=if(whichport==1) {
:=fprintf(port1,"calling from port 1");
:=}
:=
:=if(whichport==2) {
:=fprintf(port2,"calling from port 2");
:=}
:=
:=}
:=
:=what i'd really like to do is:
:=
:=void sendit(int whichport) {
:=
:=fprintf(whichport,"sending from port \%u",whichport);
:=
:=}
:=
:=What i've done doesn't work, im wondering whether a) I've done something wrong or b) You simply can't do it this way?
:=
:=Answers on a postcard to "Save the ROM competition", FrustratesVille, UK
:=
:=Cheers,
:=Darren
___________________________
This message was ported from CCS's old forum
Original Post ID: 12034
Darren Logan
Guest







Re: Is it possible to pass a stream to a function?
PostPosted: Sun Feb 23, 2003 11:04 am     Reply with quote

Mark,

Thanks. I like the sound of 2nd idea (printf) but admit to be a little under-skilled in this area.

Could you post typical code please ????

Thanks,
Darren
___________________________
This message was ported from CCS's old forum
Original Post ID: 12036
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

Re: Is it possible to pass a stream to a function?
PostPosted: Sun Feb 23, 2003 2:04 pm     Reply with quote

void myputc(char somedata)
{

if(whichport==1)
{
fputc(port1,somedata);
}
else
{
fputc(port2,somedata);
}
}

void main
{
whichport = 1;
printf(myputc,"sending from port \%u",whichport);
while(1);
}


whichport is a global in this case.


Regards,
Mark

:=Mark,
:=
:=Thanks. I like the sound of 2nd idea (printf) but admit to be a little under-skilled in this area.
:=
:=Could you post typical code please ????
:=
:=Thanks,
:=Darren
___________________________
This message was ported from CCS's old forum
Original Post ID: 12039
Darren Logan
Guest







Re: Is it possible to pass a stream to a function?
PostPosted: Mon Feb 24, 2003 6:59 am     Reply with quote

Thanks Marks, much appreciated. I'll give it a whirl.

This should save a fair bit of space as currently I've got duplicate fprintf's sending the same data only out different ports.

Cheers,
Darren
___________________________
This message was ported from CCS's old forum
Original Post ID: 12051
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group