Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Silly newbie question: Stop all processing on condition

Reply
Thread Tools

Silly newbie question: Stop all processing on condition

 
 
philbo30
Guest
Posts: n/a
 
      06-06-2007
Newbie here, just need a little general C direction:

I've a function that counts text lines in a file. If the file contains
less than 1000 lines of text, I know my input is garbage and I need
the app to completely stop all further processing. Currently, the
counting and evaluation works fine, but my app keeps on processing.
Here's the code I have so far:

int inputcheck()
{
file *fp;
int linecounter = 0;
int ch;
fp = fopen("hooligans.txt","r");
if (fp==NULL)
{
perror("fopen");
return EXIT_FAILURE;
}
while ((ch= getc(fp)) !=EOF)
{
if (ch == '\n') linecounter++;
}
fclose(fp);
if (linecounter < 1000)
{
printf("Garbage In, Don't Continue Processing\n"); //NEED TO STOP
ALL PROCESSING IF HERE
return -1;
}
else
return 0;
}
}

 
Reply With Quote
 
 
 
 
user923005
Guest
Posts: n/a
 
      06-06-2007
If you want to stop processing on the spot using the C language, then
use:
exit(EXIT_FAILURE);

you will need to include stdlib.h as this contains the prototype for
exit() and the macro definition for EXIT_FAILURE.

When you want to get a solution to a problem, the best way to do it is
to provide a small, compilable program that reproduces the problem.

Your code snippet is not in compilable shape.

The reason that it is a good idea is that often by simplification you
will solve the problem yourself and not even have to ask anyone.

 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      06-06-2007
CBFalconer <(E-Mail Removed)> writes:
[...]
> It is possible that EXIT_FAILURE on your machine evaluates to -1 or
> 0, when the treatment of the return value will depend on the order
> of tests.


It's unlikely that EXIT_FAILURE evaluates to 0, unless the system is
incapable of indicating failure.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
osmium
Guest
Posts: n/a
 
      06-06-2007
"philbo30" writes:

> Newbie here, just need a little general C direction:
>
> I've a function that counts text lines in a file. If the file contains
> less than 1000 lines of text, I know my input is garbage and I need
> the app to completely stop all further processing. Currently, the
> counting and evaluation works fine, but my app keeps on processing.
> Here's the code I have so far:
>
> int inputcheck()
> {
> file *fp;
> int linecounter = 0;
> int ch;
> fp = fopen("hooligans.txt","r");
> if (fp==NULL)
> {
> perror("fopen");
> return EXIT_FAILURE;
> }
> while ((ch= getc(fp)) !=EOF)
> {
> if (ch == '\n') linecounter++;
> }
> fclose(fp);
> if (linecounter < 1000)
> {
> printf("Garbage In, Don't Continue Processing\n"); //NEED TO STOP
> ALL PROCESSING IF HERE
> return -1;
> }
> else
> return 0;
> }
> }


Your code seems to count characters rather than lines. Did you mean,
perhaps, to use fgets instead of fgetc?


 
Reply With Quote
 
mark_bluemel@pobox.com
Guest
Posts: n/a
 
      06-06-2007
On 6 Jun, 14:35, "osmium" <(E-Mail Removed)> wrote:
> "philbo30" writes:

[snip]
> > while ((ch= getc(fp)) !=EOF)
> > {
> > if (ch == '\n') linecounter++;
> > }


> Your code seems to count characters rather than lines.


But it only counts characters which are newline characters, so he's
probably alright


 
Reply With Quote
 
osmium
Guest
Posts: n/a
 
      06-06-2007
<(E-Mail Removed)> wrote:

> On 6 Jun, 14:35, "osmium" <(E-Mail Removed)> wrote:
>> "philbo30" writes:

> [snip]
>> > while ((ch= getc(fp)) !=EOF)
>> > {
>> > if (ch == '\n') linecounter++;
>> > }

>
>> Your code seems to count characters rather than lines.

>
> But it only counts characters which are newline characters, so he's
> probably alright


Darn.


 
Reply With Quote
 
Fred Kleinschmidt
Guest
Posts: n/a
 
      06-06-2007

"osmium" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "philbo30" writes:
>
>> Newbie here, just need a little general C direction:
>>
>> I've a function that counts text lines in a file. If the file contains
>> less than 1000 lines of text, I know my input is garbage and I need
>> the app to completely stop all further processing. Currently, the
>> counting and evaluation works fine, but my app keeps on processing.
>> Here's the code I have so far:
>>
>> int inputcheck()
>> {
>> file *fp;
>> int linecounter = 0;
>> int ch;
>> fp = fopen("hooligans.txt","r");
>> if (fp==NULL)
>> {
>> perror("fopen");
>> return EXIT_FAILURE;
>> }
>> while ((ch= getc(fp)) !=EOF)
>> {
>> if (ch == '\n') linecounter++;
>> }
>> fclose(fp);
>> if (linecounter < 1000)
>> {
>> printf("Garbage In, Don't Continue Processing\n"); //NEED TO STOP
>> ALL PROCESSING IF HERE
>> return -1;
>> }
>> else
>> return 0;
>> }
>> }

>
> Your code seems to count characters rather than lines. Did you mean,
> perhaps, to use fgets instead of fgetc?


Seems to me that linecounter is only incremented when the character read
is \n, so it indeed does count lines, not characters. However, it will
miscount
by one if the final character is not a newline

I do not understand the OP's claim that this program "keeps on processing".
To the OP: What do you mean by that statement? How do you call
inputcheck? What do you do with its return value?
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Aero Stability and Controls Computing
..


 
Reply With Quote
 
osmium
Guest
Posts: n/a
 
      06-06-2007
"CBFalconer" writes:

> osmium wrote:
>> "philbo30" writes:
>>> Newbie here, just need a little general C direction:
>>>
>>> I've a function that counts text lines in a file. If the file
>>> contains less than 1000 lines of text, I know my input is
>>> garbage and I need the app to completely stop all further
>>> processing. Currently, the counting and evaluation works fine,
>>> but my app keeps on processing. Here's the code I have so far:
>>>

> ... snip and code slightly edited ...
>>>
>>> while ((ch = getc(fp)) != EOF) {
>>> if (ch == '\n') linecounter++;
>>> }

> ... snip ...
>>
>> Your code seems to count characters rather than lines. Did you
>> mean, perhaps, to use fgets instead of fgetc?

>
> You didn't read the condition controlling "linecounter++". That
> code can be quite efficient, and avoids the need for buffers of
> unknown length.


I have already said that I didn't look at the code closely enough. (Most
Americans knows what "darn" means) As far as efficiency, I would rate it as
about as bad as it can get. The code says that one function call is
replaced by something like 30-80 function calls. Some compilers may avoid
that by some machinations, but there are no assurances. (Guarantees are
something you get with Die Hard batteries at Sears.)

I would use a buffer of length 256, fgets, and hope for the best.


 
Reply With Quote
 
Dave Vandervies
Guest
Posts: n/a
 
      06-06-2007
In article <(E-Mail Removed)>,
osmium <(E-Mail Removed)> wrote:

>>> "philbo30" writes:


>> ... snip and code slightly edited ...
>>>>
>>>> while ((ch = getc(fp)) != EOF) {
>>>> if (ch == '\n') linecounter++;
>>>> }


> As far as efficiency, I would rate it as
>about as bad as it can get. The code says that one function call is
>replaced by something like 30-80 function calls. Some compilers may avoid
>that by some machinations, but there are no assurances. (Guarantees are
>something you get with Die Hard batteries at Sears.)


getc is specifically permitted to be a macro that can be expanded inline
with no function call for the general case, and any self-respecting
implementation that cares about execution speed[1] will implement it
that way.

So the code with getc will make one function call every time the stdio's
internal buffer needs to be re-filled (plus any calls needed by the
buffer-filling code), while the code with fgets will make one function
call per buffer fill (in fgets's internals), plus one call per block
(to fgets from your code), plus the code you need to check whether the
fgets actually read a '\n'.

In any case, you could run a horribly inefficient seven-buffer version
that checks for newlines with "atan(ch)==atan('\n')", compiled with a
non-optimizing compiler, and the extra overhead that gets introduced by
doing it that way will probably still be less than the time it takes to
pull a character through the I/O system.

Efficiency only matters when it matters, which is Not Very Often.


dave

[1] There may be self-respecting implementations that don't care about
execution speed; for example, an implementation that aggressively
checks for bad code would probably insert checks for side effects
in the expansion of the argument to getc and then call a function
that would do the same checking as fgetc.

--
Dave Vandervies (E-Mail Removed)
"Contact Your Systems Programmer" errors are why the best way to identify
one is that he's the geek muttering darkly to himself.
--Anthony de Boer in the scary devil monastery
 
Reply With Quote
 
osmium
Guest
Posts: n/a
 
      06-06-2007
"Dave Vandervies" writes:

> Efficiency only matters when it matters, which is Not Very Often.


But that is the subject here! Efficiency. CBFalconer said the code "can be
quite efficient", and I didn't disagree. The point is that it can also be
quite inefficient. The *code* says n function calls per line instead of one
call per line. I am well aware of the macro permission. Which is why I
said things the way I said them. Did you see the word "machinations"? Did
you see the thing about "assurances"?


 
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
Help with while condition OR condition Bill W. Ruby 13 05-09-2011 09:42 PM
When to stop - how do I work out when to stop processing Angus C Programming 5 07-18-2010 07:40 PM
GRIDVIEW: condition processing =?Utf-8?B?TWFya0F1cml0?= ASP .Net 1 02-07-2007 11:39 PM
Condition outside loop or separate loop for different condition? - Java 12 06-15-2005 08:50 AM
Backward traversal of sequence container: stop condition SpOiLeR C++ 13 03-04-2005 06:51 PM



Advertisments