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

 
 
Dave Vandervies
Guest
Posts: n/a
 
      06-06-2007
In article <(E-Mail Removed)>,
osmium <(E-Mail Removed)> wrote:
>"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.


This code can also be quite inefficient:
--------
int main(void)
{
return 0;
}
--------
But it won't be. And neither will code that uses getc to read characters
and look at them one at a time.

> The *code* says n function calls per line instead of one
>call per line.


No. The *code* says "get a character; if it's a newline increment a
counter, otherwise do nothing". Whether or not a syntactic construct
called a "function call" exists in the source code has no bearing on
what the generated code on the other side of an optimizing compiler does,
and anybody who cares about efficiency should already know that.


And if you're reading the characters from an I/O stream, pulling them
through the I/O system is going to be the bottleneck anyways, so how
efficiently you look at them isn't going to matter.


If you don't believe me, time it. With a huge file. If you can show
me numbers that prove you're right, I'd be interested in seeing them.



dave

--
Dave Vandervies http://www.velocityreviews.com/forums/(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
 
 
 
 
Tor Rustad
Guest
Posts: n/a
 
      06-06-2007
philbo30 wrote:

> 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;


FILE *fp;

> int linecounter = 0;
> int ch;
> fp = fopen("hooligans.txt","r");
> if (fp==NULL)
> {
> perror("fopen");
> return EXIT_FAILURE;


perror("hooligans.txt");
exit(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;


exit(EXIT_FAILURE);



--
Tor <torust [at] online [dot] no>

 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      06-07-2007
CBFalconer <(E-Mail Removed)> writes:
> Fred Kleinschmidt wrote:
> ... snip ...
>>
>> 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

>
> Then, in C terms, the final "line" is not a line.


Maybe.

C99 7.19.2p2:

A text stream is an ordered sequence of characters composed into
_lines_, each line consisting of zero or more characters plus a
terminating new-line character. Whether the last line requires a
terminating new-line character is implementation-defined.

--
Keith Thompson (The_Other_Keith) (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
 
 
 
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