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

P

philbo30

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!
 
G

Guest

philbo30 said:
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)?
 
S

santosh

philbo30 said:
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.
 
G

Guest

santosh said:
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?
 
S

santosh

Harald said:
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?
 
G

Guest

santosh said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top