Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Assign file to stdout problem (http://www.velocityreviews.com/forums/t739488-assign-file-to-stdout-problem.html)

Jason 12-04-2010 10:52 PM

Assign file to stdout problem
 
I'm trying to port a C source code from Solaris to Linux and I have a
problem:
In a code line there is a declaration: >> static FILE *outfp=stdout; <<
and GCC gives the error: >> initializer element is not constant <<
How can I do that in another way to compile fine?
Thanks.

Nick 12-04-2010 10:58 PM

Re: Assign file to stdout problem
 
Jason <nospam@nospam.invalid> writes:

> I'm trying to port a C source code from Solaris to Linux and I have a
> problem:
> In a code line there is a declaration: >> static FILE *outfp=stdout; <<
> and GCC gives the error: >> initializer element is not constant <<
> How can I do that in another way to compile fine?
> Thanks.


It's not clear whether this is a file-scope static or a function one,
but in either case, probably the easiest way to do it is:

static FILE *outfp = NULL;

and (almost immediately if it's declared in a function, somewhere fairly
early in a suitable function (main perhaps?) if it's file-scope) do:

if (outfp == NULL)
outfp = stdout;

If you use NULL as a magic value that you don't want to get reset to
stdout, you need to make sure the flow is such that this only gets
executed once.
--
Online waterways route planner | http://canalplan.eu
Plan trips, see photos, check facilities | http://canalplan.org.uk

Peter Nilsson 12-04-2010 11:03 PM

Re: Assign file to stdout problem
 
Jason <nos...@nospam.invalid> wrote:
> I'm trying to port a C source code from Solaris to Linux
> and I have a problem:
> In a code line there is a declaration: >> static FILE
> *outfp=stdout; <<
> and GCC gives the error: >> initializer element is not
> constant <<


That's because stdout is a macro and needn't be a compile
time constant.

> How can I do that in another way to compile fine?


You could intialise outfp in a function block.

static FILE *outfp;

int main(void)
{
outfp = stdout;
return 0;
}

Although there's probably an even better solution, like
decoupling your functions from 'globals' and instead take
parameters.

void foo(FILE *outfp) { fputs("Hello World\n", outfp); }

--
Peter

Seebs 12-05-2010 04:51 AM

Re: Assign file to stdout problem
 
On 2010-12-04, Jason <nospam@nospam.invalid> wrote:
> I'm


Posts listing the address as "nospam@nospam.invalid" from "aioe.org"
are presumptively trolls.

If you are not a troll, get a real Usenet provider and/or provide
a more plausible address.

-s
--
Copyright 2010, all wrongs reversed. Peter Seebach / usenet-nospam@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.

Keith Thompson 12-05-2010 08:19 AM

Re: Assign file to stdout problem
 
Seebs <usenet-nospam@seebs.net> writes:
> On 2010-12-04, Jason <nospam@nospam.invalid> wrote:
>> I'm

>
> Posts listing the address as "nospam@nospam.invalid" from "aioe.org"
> are presumptively trolls.


I'll take your word for it that a lot of trolls use aioe.org,
but that hardly implies that all, or even most, aioe.org users
are trolls. I used to use aioe.org myself. And plenty of posters
use fake e-mail addresses to avoid spam. At least Jason uses an
address that doesn't match an actual domain.

I saw nothing trollish in the content of the article.

[...]

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Seebs 12-05-2010 08:44 AM

Re: Assign file to stdout problem
 
On 2010-12-05, Keith Thompson <kst-u@mib.org> wrote:
> I'll take your word for it that a lot of trolls use aioe.org,
> but that hardly implies that all, or even most, aioe.org users
> are trolls. I used to use aioe.org myself. And plenty of posters
> use fake e-mail addresses to avoid spam.


Yes. But that *specific* address is very common in troll posts
to this group asking questions that are of the sort calculated to
get responses complaining about non-portable code.

There's been a bunch recently all with exactly that same address.

-s
--
Copyright 2010, all wrongs reversed. Peter Seebach / usenet-nospam@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.

luser- -droog 12-05-2010 08:54 AM

Re: Assign file to stdout problem
 
On Dec 5, 2:44*am, Seebs <usenet-nos...@seebs.net> wrote:
> On 2010-12-05, Keith Thompson <ks...@mib.org> wrote:
>
> > I'll take your word for it that a lot of trolls use aioe.org,
> > but that hardly implies that all, or even most, aioe.org users
> > are trolls. *I used to use aioe.org myself. *And plenty of posters
> > use fake e-mail addresses to avoid spam.

>
> Yes. *But that *specific* address is very common in troll posts
> to this group asking questions that are of the sort calculated to
> get responses complaining about non-portable code.
>
> There's been a bunch recently all with exactly that same address.
>


Besides which, this very issue was discussed not 2 months ago.
At the very least, one might reasonably recommend searching
before posting, and perhaps a skim through

www.catb.org/~esr/faqs/smart-questions.html



luser- -droog 12-06-2010 07:58 AM

Re: Assign file to stdout problem
 


The China Blue and the Gray wrote:
> <the latest in a series of succinct, code-ridden answers to silly questions>
> In article <idegnk$21o$1@speranza.aioe.org>, Jason <nospam@nospam.invalid>
> wrote:
> <the latest in a series of silly questions modelled after real questions from preceding months>
>


Are you writing a new FAQ?

Tim Rentsch 01-02-2011 08:07 AM

Re: Assign file to stdout problem
 
Seebs <usenet-nospam@seebs.net> writes:

> On 2010-12-04, Jason <nospam@nospam.invalid> wrote:
>> I'm

>
> Posts listing the address as "nospam@nospam.invalid" from "aioe.org"
> are presumptively trolls.
>
> If you are not a troll, get a real Usenet provider and/or provide
> a more plausible address.


The responded-to posting (which was snipped) did not deserve
this response. Get off your high horse.

Seebs 01-02-2011 06:03 PM

Re: Assign file to stdout problem
 
On 2011-01-02, Tim Rentsch <txr@alumni.caltech.edu> wrote:
> Seebs <usenet-nospam@seebs.net> writes:
>> On 2010-12-04, Jason <nospam@nospam.invalid> wrote:
>>> I'm


>> Posts listing the address as "nospam@nospam.invalid" from "aioe.org"
>> are presumptively trolls.


> The responded-to posting (which was snipped) did not deserve
> this response.


Yes it did. Whether or not the poster was a troll, his choice of an
invalid address, his choice of a Usenet provider, and his choice of question
happened to precisely match the pattern of a large number of recent spams.

If he wasn't a troll, he would benefit from knowing this. If he was,
there was no point in any other response.

Did he respond at any point to anything in the thread? I don't remember
him doing so, which would support my analysis. The troll using that pattern
generally didn't.

> Get off your high horse.


Haven't got one.

-s
--
Copyright 2010, all wrongs reversed. Peter Seebach / usenet-nospam@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.


All times are GMT. The time now is 01:59 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.