Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Newbie Question: Code works when compiled as .exe but not as function...

Reply
Thread Tools

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

 
 
philbo30
Guest
Posts: n/a
 
      02-17-2007
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!

 
Reply With Quote
 
 
 
 
=?utf-8?B?SGFyYWxkIHZhbiBExLNr?=
Guest
Posts: n/a
 
      02-17-2007
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)?

 
Reply With Quote
 
 
 
 
santosh
Guest
Posts: n/a
 
      02-17-2007
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.

 
Reply With Quote
 
=?utf-8?B?SGFyYWxkIHZhbiBExLNr?=
Guest
Posts: n/a
 
      02-17-2007
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?

 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      02-17-2007

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 first attempts to close any file that is associated
with the specified
stream. Failure to close the file is ignored. The error and end-of-file
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?

 
Reply With Quote
 
=?utf-8?B?SGFyYWxkIHZhbiBExLNr?=
Guest
Posts: n/a
 
      02-17-2007
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 first attempts to close any file that is associated
> with the specified
> stream. Failure to close the file is ignored. The error and end-of-file
> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
If I create a page, then it's compiled upon first request, where cani find the compiled code?? lander ASP .Net 5 03-05-2008 04:34 PM
Page works in IDE but not Compiled (VS2005) =?Utf-8?B?RG91Z1Q=?= ASP .Net 3 09-24-2007 01:12 PM
g++ compiled C++ code called from gcc compiled C code Klaus Schneider C++ 1 12-02-2004 01:44 PM
[py2exe.i18n] English works, German works, but not French. What do I miss? F. GEIGER Python 3 08-06-2004 10:01 AM
After rebooting my PC works, works, works! Antivirus problem? Adriano Computer Information 1 12-15-2003 05:30 AM



Advertisments