Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C Programming
Need sharp criticism on my code.
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="grishin-mailing-lists, post: 4065071"] Hi guys, I'm trying to write really neat program. I can't decide how to write end-of-the loop better (as neatly as possible, huh). The loop stops in two cases: 1) EOF is met; 2) desired string is found; The first is a failure, the second is a good result. Well, I break the loop as soon as I met the string. But how to know whether all the fp has been looked through OR I met the string *without* adding additional flag, something like int found = 0; and bumping it if the string appears. I wouldn't like to add variables. I'd like more elegant solution. Another way is to return from the function right after the string has been got. It leads to code bloat -- I have to add another one free(buff). Any suggestions are appreciated. /* sets fp on the first true function definition */ int FindDef (char *cDefinition, FILE *fp) { int result = EXIT_SUCCESS; /* placed in stdlib.h */ char *s = NULL; char s_case1[] = "Далее"; /* ext var1 */ char s_case2_1[] = ";"; /* ext var2 */ char s_case2_2[] = "Возврат"; /* ext var3 */ char s_case3[] = "//"; /* ext var4 */ char *buff = NULL; if ( NULL != cDefinition && NULL != fp ) { fseek( fp, 0L, SEEK_SET ); buff = malloc( 500+1 ); /* dummy constant, but OKAY for now */ if ( NULL != buff ) { while ( fgets( buff, 500+1, fp ) ) { s = strstr( buff, cDefinition ); if ( NULL != s ) { if ( NULL == strstr( buff, s_case1 ) ) { if ( NULL == strstr( buff, s_case2_1 ) && NULL == strstr( buff, s_case2_2 ) && NULL == strstr( buff, s_case3 ) ) { break; } } } } if ( !fgets( buff, 1, fp ) ) { result = EXIT_FAILURE; } } else result = EXIT_FAILURE; } else result = EXIT_FAILURE; free(buff); return result; } [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C Programming
Need sharp criticism on my code.
Top