Newbie Question: Code works when compiled as .exe but not as function...

Discussion in 'C Programming' started by philbo30, Feb 17, 2007.

  1. philbo30

    philbo30 Guest

    Newbie here, working to mash together some code for a hobby project;
    the code is:

    int main()
    {
    char data[30];
    FILE *ptr;

    ptr=fopen("or.txt","r");
    fflush(stdout);
    freopen("parsed.txt", "w+", stdout);

    while (feof(ptr)==0)
    {
    fgets(data,30,ptr);
    if (data[1]=='7' && data[2]=='1') continue;
    if (data[0]=='B' && data[1]=='L') continue;
    if (data[0]=='E' && data[1]=='V') continue;
    if (data[0]=='P' && data[1]=='R') continue;
    if (data[0]=='B' && data[1]=='l') continue;
    if (data[5]=='T' && data[6]=='H') continue;
    if (data[0]=='P' && data[1]=='l') continue;
    if (data[3]=='6') continue;
    if (data[0]=='\n') continue;
    printf("%s",data);
    }
    fclose(ptr);
    return 0;
    }

    The goal is to open a file, remove those lines designated with
    "continue" and then write the results to a new file. This compiles and
    runs fine as an executable. However, when I attempt to call it as a
    function within a larger program, it isn't able to write the results
    to the new file, "parsed.txt", that file remains blank, and the
    program "hangs". Any ideas?

    In advance, thanks!
    philbo30, Feb 17, 2007
    #1
    1. Advertising

  2. philbo30 wrote:
    > Newbie here, working to mash together some code for a hobby project;
    > the code is:
    >
    > int main()
    > {
    > char data[30];
    > FILE *ptr;
    >
    > ptr=fopen("or.txt","r");
    > fflush(stdout);
    > freopen("parsed.txt", "w+", stdout);


    If you're using this as a function, it's generally not nice to
    disallow the use of standard output from any other part of the
    program. And at any rate, don't forget to check for errors here and in
    the earlier fopen.

    > while (feof(ptr)==0)
    > {
    > fgets(data,30,ptr);


    This won't do what you want.
    http://c-faq.com/stdio/feof.html

    It may or may not cause the problem you're having.

    > if (data[1]=='7' && data[2]=='1') continue;
    > if (data[0]=='B' && data[1]=='L') continue;
    > if (data[0]=='E' && data[1]=='V') continue;
    > if (data[0]=='P' && data[1]=='R') continue;
    > if (data[0]=='B' && data[1]=='l') continue;
    > if (data[5]=='T' && data[6]=='H') continue;
    > if (data[0]=='P' && data[1]=='l') continue;
    > if (data[3]=='6') continue;
    > if (data[0]=='\n') continue;
    > printf("%s",data);
    >
    > }
    > fclose(ptr);
    > return 0;
    > }
    >
    > The goal is to open a file, remove those lines designated with
    > "continue" and then write the results to a new file. This compiles and
    > runs fine as an executable. However, when I attempt to call it as a
    > function within a larger program, it isn't able to write the results
    > to the new file, "parsed.txt", that file remains blank, and the
    > program "hangs". Any ideas?


    Can you post a larger program that has this problem (but no larger
    than necessary to show the problem)?
    =?utf-8?B?SGFyYWxkIHZhbiBExLNr?=, Feb 17, 2007
    #2
    1. Advertising

  3. philbo30

    santosh Guest

    philbo30 wrote:
    > Newbie here, working to mash together some code for a hobby project;
    > the code is:
    >
    > int main()
    > {
    > char data[30];
    > FILE *ptr;
    >
    > ptr=fopen("or.txt","r");


    Never proceed without checking function calls for possible failures,
    particularly complex ones like fopen.

    > fflush(stdout);
    > freopen("parsed.txt", "w+", stdout);


    Why do you need to do this? Why not just open another file for output
    and associate with a fresh FILE pointer and leave stdout alone?

    If stdout has previously been used then freopen will fail. In anycase
    you must check the call for success before proceeding. freopen returns
    a null pointer upon failure.

    > while (feof(ptr)==0)


    Use feof only after fgets returns NULL. That is the way it works in C.
    You can use feof and ferror to find out the cause of failure of the
    standard I/O functions *after* a call to those functions have
    signalled failure, not before.

    > {
    > fgets(data,30,ptr);


    Again you're not checking fgets for possible failure.

    > if (data[1]=='7' && data[2]=='1') continue;
    > if (data[0]=='B' && data[1]=='L') continue;
    > if (data[0]=='E' && data[1]=='V') continue;
    > if (data[0]=='P' && data[1]=='R') continue;
    > if (data[0]=='B' && data[1]=='l') continue;
    > if (data[5]=='T' && data[6]=='H') continue;
    > if (data[0]=='P' && data[1]=='l') continue;
    > if (data[3]=='6') continue;
    > if (data[0]=='\n') continue;
    > printf("%s",data);
    > }
    > fclose(ptr);
    > return 0;
    > }
    >
    > The goal is to open a file, remove those lines designated with
    > "continue" and then write the results to a new file. This compiles and
    > runs fine as an executable. However, when I attempt to call it as a
    > function within a larger program, it isn't able to write the results
    > to the new file, "parsed.txt", that file remains blank, and the
    > program "hangs". Any ideas?
    >
    > In advance, thanks!


    I suspect that freopen is failing. Unless it matters, I suggest
    leaving stdin and stdout alone and opening a new stream.
    santosh, Feb 17, 2007
    #3
  4. santosh wrote:
    > philbo30 wrote:
    > > freopen("parsed.txt", "w+", stdout);

    >
    > Why do you need to do this? Why not just open another file for output
    > and associate with a fresh FILE pointer and leave stdout alone?
    >
    > If stdout has previously been used then freopen will fail.


    Why is that? freopen ignores the original stream after closing it, and
    closing a stream you've written to is not a problem. Am I missing
    something?
    =?utf-8?B?SGFyYWxkIHZhbiBExLNr?=, Feb 17, 2007
    #4
  5. philbo30

    santosh Guest

    Harald van Dijk wrote:
    > santosh wrote:
    > > philbo30 wrote:
    > > > freopen("parsed.txt", "w+", stdout);

    > >
    > > Why do you need to do this? Why not just open another file for output
    > > and associate with a fresh FILE pointer and leave stdout alone?
    > >
    > > If stdout has previously been used then freopen will fail.

    >
    > Why is that? freopen ignores the original stream after closing it, and
    > closing a stream you've written to is not a problem. Am I missing
    > something?


    Actually you're right. I was confused by the following paragraph from
    n1124:

    7.19.5.4

    4.
    The freopen function ï¬rst attempts to close any ï¬le that is associated
    with the speciï¬ed
    stream. Failure to close the ï¬le is ignored. The error and end-of-ï¬le
    indicators for the
    stream are cleared.

    The OP is changing the mode during the call to freopen in his code. Is
    that a case of implementation defined behaviour?
    santosh, Feb 17, 2007
    #5
  6. santosh wrote:
    > Harald van Dijk wrote:
    > > santosh wrote:
    > > > philbo30 wrote:
    > > > > freopen("parsed.txt", "w+", stdout);
    > > >
    > > > Why do you need to do this? Why not just open another file for output
    > > > and associate with a fresh FILE pointer and leave stdout alone?
    > > >
    > > > If stdout has previously been used then freopen will fail.

    > >
    > > Why is that? freopen ignores the original stream after closing it, and
    > > closing a stream you've written to is not a problem. Am I missing
    > > something?

    >
    > Actually you're right. I was confused by the following paragraph from
    > n1124:
    >
    > 7.19.5.4
    >
    > 4.
    > The freopen function ï¬rst attempts to close any ï¬le that is associated
    > with the speciï¬ed
    > stream. Failure to close the ï¬le is ignored. The error and end-of-ï¬le
    > indicators for the
    > stream are cleared.
    >
    > The OP is changing the mode during the call to freopen in his code. Is
    > that a case of implementation defined behaviour?


    p3:
    If filename is a null pointer, the freopen function attempts to change
    the mode of the stream to that specified by mode, as if the name of
    the file currently associated with the stream had been used. It is
    implementation-defined which changes of mode are permitted (if any),
    and under what circumstances.

    As far as I know, the original code is not implementation-defined
    because of this, as p3 applies only when filename is a null pointer,
    and there is no restriction on mode changes other than in p3.
    =?utf-8?B?SGFyYWxkIHZhbiBExLNr?=, Feb 17, 2007
    #6
    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. Klaus Schneider
    Replies:
    1
    Views:
    544
    Rolf Magnus
    Dec 2, 2004
  2. F. GEIGER
    Replies:
    3
    Views:
    769
    F. GEIGER
    Aug 6, 2004
  3. =?Utf-8?B?RG91Z1Q=?=

    Page works in IDE but not Compiled (VS2005)

    =?Utf-8?B?RG91Z1Q=?=, Sep 20, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    390
    =?Utf-8?B?RG91Z1Q=?=
    Sep 24, 2007
  4. Dave -Turner
    Replies:
    38
    Views:
    956
    Virtual_X
    Nov 1, 2007
  5. lander
    Replies:
    5
    Views:
    588
    bruce barker
    Mar 5, 2008
Loading...

Share This Page