Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Ben Bacarisse Newsgroups: comp.lang.c Subject: Re: Whaddaya think? Date: Mon, 17 Jun 2024 00:17:18 +0100 Organization: A noiseless patient Spider Lines: 23 Message-ID: <87bk40wk8x.fsf@bsb.me.uk> References: <666ded36$0$958$882e4bbb@reader.netnews.com> <87sexdx3s1.fsf@bsb.me.uk> <666f0a3e$0$1412896$882e4bbb@reader.netnews.com> MIME-Version: 1.0 Content-Type: text/plain Injection-Date: Mon, 17 Jun 2024 01:17:19 +0200 (CEST) Injection-Info: dont-email.me; posting-host="f69b21fe46179def9aae9c77317b77e3"; logging-data="296506"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/5fC4FJHfwUikJWc5Pm8BgqsBgs9I2kGU=" User-Agent: Gnus/5.13 (Gnus v5.13) Cancel-Lock: sha1:pr5w6upAFz/FP1dgctHEtA7D8mc= sha1:gmAK3Zm9GKbgVKG86Iini8lp5rk= X-BSB-Auth: 1.e6277b489d4d6aa9c8d3.20240617001719BST.87bk40wk8x.fsf@bsb.me.uk DFS writes: > #include > #include > > int main(int argc, char *argv[]) { > > int N=0; > int *nums = malloc(2 * sizeof(int)); > > FILE* datafile = fopen(argv[1], "r"); > while(fscanf(datafile, "%d", &nums[N++]) == 1){ > nums = realloc(nums, (N+1) * sizeof(int)); > } > fclose (datafile); > > N--; This N-- is a bit "tricksy". Better to increment in the realloc (or the while body) so it only happens when an int has been read. -- Ben.