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="Ben Bacarisse, post: 4065095"] I think you can just test for EOF or a read error. See below. Since the function mallocs and frees the buffer, you could use a static array or an automatic (local) array. If a constant size it too restrictive, C99 has variably modified arrays. Of course, if you plan to grow the buffer in the future you will have to use malloc/realloc/free. I'd use these only for exit codes, not function return values. If the return is success/failure (with no distinction between failure types) I just use 1 and 0. You should test for feof or ferror here. When you find the string and break, this will read (or try to read) another line. If you abandon the EXIT_FAILURE/SUCCESS codes you can just write result = feof(fp) || ferror(fp); here. You could put the stop (well, the "carry on") condition in the loop since it is just a bunch of strstr calls. Some people don't like big conditions like that but they can be quite convenient: while (fgets(buff, 500+1, fp) && (!strstr(buff, cDefinition) || strstr(buff, s_case1) || strstr(buff, s_case2_1) || strstr(buff, s_case2_2) || strstr(buff, s_case3))) {} (I may have got that wrong -- check first!). Even if you don't do this, I would remove the apparent special case when you test for s_case1. Writing if (C1) if (C2 && C3) break; is the same as writing if (C1 && C2 && C3) break; [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C Programming
Need sharp criticism on my code.
Top