Assign file to stdout problem

Discussion in 'C Programming' started by Jason, Dec 4, 2010.

  1. Jason

    Jason Guest

    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.
     
    Jason, Dec 4, 2010
    #1
    1. Advertising

  2. Jason

    Nick Guest

    Jason <> 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
     
    Nick, Dec 4, 2010
    #2
    1. Advertising

  3. Jason <> 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
     
    Peter Nilsson, Dec 4, 2010
    #3
  4. Jason

    Seebs Guest

    On 2010-12-04, Jason <> wrote:
    > I'm


    Posts listing the address as "" 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 /
    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.
     
    Seebs, Dec 5, 2010
    #4
  5. Seebs <> writes:
    > On 2010-12-04, Jason <> wrote:
    >> I'm

    >
    > Posts listing the address as "" 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) <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"
     
    Keith Thompson, Dec 5, 2010
    #5
  6. Jason

    Seebs Guest

    On 2010-12-05, Keith Thompson <> 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 /
    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.
     
    Seebs, Dec 5, 2010
    #6
  7. On Dec 5, 2:44 am, Seebs <> wrote:
    > On 2010-12-05, Keith Thompson <> 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, Dec 5, 2010
    #7
  8. The China Blue and the Gray wrote:
    > <the latest in a series of succinct, code-ridden answers to silly questions>
    > In article <idegnk$21o$>, Jason <>
    > wrote:
    > <the latest in a series of silly questions modelled after real questions from preceding months>
    >


    Are you writing a new FAQ?
     
    luser- -droog, Dec 6, 2010
    #8
  9. Jason

    Tim Rentsch Guest

    Seebs <> writes:

    > On 2010-12-04, Jason <> wrote:
    >> I'm

    >
    > Posts listing the address as "" 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.
     
    Tim Rentsch, Jan 2, 2011
    #9
  10. Jason

    Seebs Guest

    On 2011-01-02, Tim Rentsch <> wrote:
    > Seebs <> writes:
    >> On 2010-12-04, Jason <> wrote:
    >>> I'm


    >> Posts listing the address as "" 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 /
    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.
     
    Seebs, Jan 2, 2011
    #10
  11. Jason

    Tim Rentsch Guest

    Seebs <> writes:

    > On 2011-01-02, Tim Rentsch <> wrote:
    >> Seebs <> writes:
    >>> On 2010-12-04, Jason <> wrote:
    >>>> I'm

    >
    >>> Posts listing the address as "" 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.


    The question was perfectly reasonable and a quite legitimate query.
    It was not spam. As for the rest, unless the same question was
    posted many times in a short period, they are irrelevant. That
    didn't happen with this question.

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


    If he is a troll, he doesn't care about your comments. If he
    isn't a troll, they very well may drive him (and others) away
    from the newsgroup. The downside far outweighs the upside.

    > Did he respond at any point to anything in the thread? [snip]


    After your asshole response, why would he?

    >> Get off your high horse.

    >
    > Haven't got one.


    Your comments prove otherwise. If you want to play
    moderator, take it out on comp.lang.c.moderated.
     
    Tim Rentsch, Jan 4, 2011
    #11
  12. Jason

    Seebs Guest

    On 2011-01-04, Tim Rentsch <> wrote:
    > The question was perfectly reasonable and a quite legitimate query.
    > It was not spam. As for the rest, unless the same question was
    > posted many times in a short period, they are irrelevant. That
    > didn't happen with this question.


    *Substantively* identical includes, say, a dozen similar questions
    posted by the same troll.

    > If he is a troll, he doesn't care about your comments. If he
    > isn't a troll, they very well may drive him (and others) away
    > from the newsgroup. The downside far outweighs the upside.


    If he is a troll, being identified rather than getting nibbles
    makes it less fun. If he isn't, it's useful to him to know why
    people are ignoring him.

    >> Did he respond at any point to anything in the thread? [snip]


    > After your asshole response, why would he?


    I don't buy this "argument" at all. I was not being "an asshole".
    I was telling him factually that his posts contained things which
    would cause other participants to assume he was a troll. If he were
    not a troll, this would be information of value to him. If he were,
    he'd disappear until he could figure out a way to post without those
    tells.

    > Your comments prove otherwise.


    No, they don't.

    And *plonk*. No amount of technical knowledge you have makes me feel
    like putting up with you making up bullshit about my intent or internal
    state.

    -s
    --
    Copyright 2010, all wrongs reversed. Peter Seebach /
    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.
     
    Seebs, Jan 4, 2011
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Elad
    Replies:
    0
    Views:
    434
  2. Replies:
    1
    Views:
    284
  3. Edward Rutherford

    Assign file to stdout problem

    Edward Rutherford, Oct 16, 2011, in forum: C Programming
    Replies:
    1
    Views:
    360
    Paul N
    Oct 16, 2011
  4. Andreas S
    Replies:
    3
    Views:
    285
    Eric Hodel
    Dec 9, 2006
  5. Replies:
    2
    Views:
    379
    A. Sinan Unur
    Dec 7, 2005
Loading...

Share This Page