Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| Path | csiph.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail |
|---|---|
| From | Ian Collins <ian-news@hotmail.com> |
| Newsgroups | comp.lang.c |
| Subject | Re: how to do this? |
| Date | Sun, 15 Nov 2015 10:02:38 +1300 |
| Lines | 50 |
| Message-ID | <daplreFi0ttU6@mid.individual.net> (permalink) |
| References | <n2849g$a6p$1@dont-email.me> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-Trace | individual.net yCDEafqtdOajM26I4ixQdAi/ZvF0pSxehMw+n4+T7p7M+1tcdf |
| Cancel-Lock | sha1:/6j4zPZfvD2+iP+glqhIPjf14uM= |
| User-Agent | Mozilla/5.0 (X11; SunOS i86pc; rv:17.0) Gecko/17.0 Thunderbird/17.0 |
| In-Reply-To | <n2849g$a6p$1@dont-email.me> |
| Xref | csiph.com comp.lang.c:76026 |
Show key headers only | View raw
bilsch01 wrote:
> I don't know how to write code to eliminate repetitiveness. The program
> below operates on four files, does several tasks for each. I want to
> learn a way to do the same thing with 1/4 of the code by having (a
> macro?) substitute 1, 2, 3, 4 in the code where needed when the program
> runs.
The obvious starting point is to create a function to open and read the
files with the necessary error checking. Say:
char*
load( const char* filename )
{
FILE* fp = fopen(filename, "r");
if( fp == NULL)
{
printf("\nerror opening file %s\n", filename );
return NULL;
}
const size_t buffSize = 2305;
char* buffer = malloc(buffSize);
if( (fread( buffer, buffSize-1, 1, fp )) != buffSize-1 )
{
printf("\nerror reading from file %s\n", filename );
free( buffer );
return NULL;
}
// What goes in the last character?
return buffer;
}
Then do your processing.
<snip>
>
> for (i=0;i<256;i++)
> {
> vec1 = malloc(9);
Why are you using dynamic allocation here? You never free what you
allocate.
--
Ian Collins
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
how to do this? bilsch01 <king621@comcast.net> - 2015-11-14 12:09 -0800
Re: how to do this? Rosario19 <Ros@invalid.invalid> - 2015-11-14 21:49 +0100
Re: how to do this? Ian Collins <ian-news@hotmail.com> - 2015-11-15 10:02 +1300
Re: how to do this? Ian Collins <ian-news@hotmail.com> - 2015-11-15 10:17 +1300
Re: how to do this? bilsch01 <king621@comcast.net> - 2015-11-14 15:16 -0800
Re: how to do this? James Kuyper <jameskuyper@verizon.net> - 2015-11-14 18:24 -0500
Re: how to do this? bilsch01 <king621@comcast.net> - 2015-11-14 15:30 -0800
Re: how to do this? Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2015-11-14 19:01 -0500
Re: how to do this? James Kuyper <jameskuyper@verizon.net> - 2015-11-14 19:07 -0500
Re: how to do this? Ian Collins <ian-news@hotmail.com> - 2015-11-15 11:26 +1300
Re: how to do this? bilsch01 <king621@comcast.net> - 2015-11-14 15:07 -0800
Re: how to do this? James Kuyper <jameskuyper@verizon.net> - 2015-11-14 18:17 -0500
Re: how to do this? bilsch01 <king621@comcast.net> - 2015-11-14 15:25 -0800
Re: how to do this? Ian Collins <ian-news@hotmail.com> - 2015-11-15 13:10 +1300
Re: how to do this? James Kuyper <jameskuyper@verizon.net> - 2015-11-14 19:21 -0500
Re: how to do this? Ian Collins <ian-news@hotmail.com> - 2015-11-15 13:24 +1300
Re: how to do this? James Kuyper <jameskuyper@verizon.net> - 2015-11-14 19:12 -0500
Re: how to do this? Ian Collins <ian-news@hotmail.com> - 2015-11-15 13:18 +1300
Re: how to do this? James Kuyper <jameskuyper@verizon.net> - 2015-11-14 19:23 -0500
Re: how to do this? bilsch01 <king621@comcast.net> - 2015-11-14 15:18 -0800
csiph-web