Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | DFS <nospam@dfs.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Why is this outputting alternate blank lines? |
| Date | 2016-12-14 17:44 -0500 |
| Organization | A noiseless patient Spider |
| Message-ID | <o2shtc$108$1@dont-email.me> (permalink) |
| References | <o2qh45$9ae$1@dont-email.me> <o2r7cs$6l1$1@dont-email.me> <o2s0sb$tb$1@dont-email.me> <87vaum5km7.fsf@bsb.me.uk> |
On 12/14/2016 03:19 PM, Ben Bacarisse wrote:
> DFS <nospam@dfs.com> writes:
> <snip>
>> readdates.c
>> ================================================================
>> #include <stdio.h>
>> #include <stdlib.h>
>> #include <string.h>
>>
>> int main(int argc, char *argv[])
>> {
>>
>> int i=0,n=0,linewidth=13,linecount=0;
>> char line[linewidth];
>> char txtfile[strlen(argv[1])];
>
> These arrays are called variable length arrays because the size is not a
> compile time constant (despite linewidth looking very predictable).
> Nothing wrong with that but it's useful to know since this feature has
> been made optional in the latest C standard.
>
> The second one (txtfile) is not needed.
>
>> FILE *fp; char c;
>>
>> //open file
>> strcpy(txtfile,argv[1]);
>> fp=fopen(txtfile,"r");
>
> Just fp = fopen(argv[1], "r");
>
>> if(fp==NULL){
>> printf("Failed to open file\n");
>> return -1;
>> }
>
> You test fp, but not whether argv[1] exists. For simple programs, an
> easy option is to provide a default:
>
> FILE *fp = argc > 1 ? fopen(argv[1], "r") : stdin;
>
> (You still have to test fp.)
>
>> //count lines in file
>> c=getc(fp);
>> while (c!=EOF){
>> if(c=='\n'){linecount+=1;}
>> c=getc(fp);
>> }
>> fclose(fp);
>
> It's better just to use rewind(fp) here. That sets the reading position
> back to the start.
>
> Also, if a file contains "abc\ndef" (and nothing else) how many lines do
> you want to count?
>
>> printf("%d lines in file '%s'\n",linecount,txtfile);
>>
>> //copy data into array
>> char *datestr[linecount];
>
> This is likely to be the source of your trouble. Most systems limit the
> amount of space that this sort of object can occupy so that
> million-element arrays are likely to be a problem (look up "stack space"
> to find out more). Most space is usually available via malloc, so
> instead you can write
>
> char *datestr = malloc(linecount * sizeof *datestr);
>
>> fp = fopen(txtfile,"r");
>
> If you rewind, no need to reopen.
>
>> while(fgets(line,linewidth,fp)!=NULL) {
>> datestr[i]=malloc(linewidth);
>> strcpy(datestr[i],line);
>> i++;n++;
>> }
>
> There are some possible problems here though you may choose to disregard
> them for the moment. Currently, you'll count one line for the example I
> gave above, but fgets will read two lines (one with "abc\n" and the
> second with "def" and no newline). You will access datestr outside it's
> bound. Secondly, if there are long line, fgets will return them in two
> or more parts, again making you access datestr outside its bounds.
That's for another day, but thanks.
Today I just want to efficiently read into an array a list of date
strings (14-Dec-2016 format), and use mktime or some other function to
convert the strings to and from ints.
Using your recommendations:
//copy file data into array
char *datestr=malloc(linecount*sizeof(*datestr));
rewind(fp);
while(fgets(line,linewidth,fp)!=NULL) {
strcpy(datestr[i],line);
i++;n++;
}
Gives me a warning and a note:
readdates.c:46:10: warning: passing argument 1 of ‘strcpy’ makes pointer
from integer without a cast [-Wint-conversion]
strcpy(datestr[i],line);
^~~~~~~
In file included from readdates.c:3:0:
/usr/include/string.h:125:14: note: expected ‘char * restrict’ but
argument is of type ‘char’
extern char *strcpy (char *__restrict __dest, const char *__restrict
__src)
^~~~~~
>> fclose(fp);
>
> and if you do as I suggested above you probably want to write
>
> if (fp != stdin) fclose(fp);
>>
>> //print array
>> for (i=0;i<n;i++){printf("%s", datestr[i]);}
>> }
>> ================================================================
Thanks for the tips, Ben.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Why is this outputting alternate blank lines? DFS <nospam@dfs.com> - 2016-12-13 23:18 -0500
Re: Why is this outputting alternate blank lines? Melzzzzz <mel@zzzzz.com> - 2016-12-14 05:30 +0100
Re: Why is this outputting alternate blank lines? mark.bluemel@gmail.com - 2016-12-14 00:59 -0800
Re: Why is this outputting alternate blank lines? Richard Heathfield <rjh@cpax.org.uk> - 2016-12-14 10:38 +0000
Re: Why is this outputting alternate blank lines? DFS <nospam@dfs.com> - 2016-12-14 12:53 -0500
Re: Why is this outputting alternate blank lines? Richard Heathfield <rjh@cpax.org.uk> - 2016-12-14 18:05 +0000
Re: Why is this outputting alternate blank lines? Keith Thompson <kst-u@mib.org> - 2016-12-14 11:57 -0800
Re: Why is this outputting alternate blank lines? Richard Heathfield <rjh@cpax.org.uk> - 2016-12-14 20:05 +0000
Re: Why is this outputting alternate blank lines? "James R. Kuyper" <jameskuyper@verizon.net> - 2016-12-14 15:21 -0500
Re: Why is this outputting alternate blank lines? Keith Thompson <kst-u@mib.org> - 2016-12-14 12:28 -0800
Re: Why is this outputting alternate blank lines? supercat@casperkitty.com - 2016-12-14 13:09 -0800
Re: Why is this outputting alternate blank lines? Jean-Marc Bourguet <jm@bourguet.org> - 2016-12-14 22:53 +0100
Re: Why is this outputting alternate blank lines? <william@wilbur.25thandClement.com> - 2016-12-14 15:47 -0800
Re: Why is this outputting alternate blank lines? Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2016-12-14 15:41 -0700
Re: Why is this outputting alternate blank lines? Richard Heathfield <rjh@cpax.org.uk> - 2016-12-14 23:26 +0000
Re: Why is this outputting alternate blank lines? Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2016-12-14 17:33 -0700
Re: Why is this outputting alternate blank lines? Richard Heathfield <rjh@cpax.org.uk> - 2016-12-15 00:42 +0000
Re: Why is this outputting alternate blank lines? supercat@casperkitty.com - 2016-12-14 17:05 -0800
Re: Why is this outputting alternate blank lines? Richard Heathfield <rjh@cpax.org.uk> - 2016-12-15 08:21 +0000
Re: Why is this outputting alternate blank lines? supercat@casperkitty.com - 2016-12-15 09:15 -0800
Re: Why is this outputting alternate blank lines? Richard Heathfield <rjh@cpax.org.uk> - 2016-12-15 17:50 +0000
Re: Why is this outputting alternate blank lines? supercat@casperkitty.com - 2016-12-15 12:15 -0800
Re: Why is this outputting alternate blank lines? Richard Heathfield <rjh@cpax.org.uk> - 2016-12-15 20:37 +0000
Re: Why is this outputting alternate blank lines? raltbos@xs4all.nl (Richard Bos) - 2016-12-15 20:08 +0000
Re: Why is this outputting alternate blank lines? Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2016-12-14 20:40 -0700
Re: Why is this outputting alternate blank lines? Richard Heathfield <rjh@cpax.org.uk> - 2016-12-15 08:36 +0000
Re: Why is this outputting alternate blank lines? Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2016-12-15 17:43 -0700
Re: Why is this outputting alternate blank lines? Richard Heathfield <rjh@cpax.org.uk> - 2016-12-16 01:05 +0000
Re: Why is this outputting alternate blank lines? Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2016-12-15 18:34 -0700
Re: Why is this outputting alternate blank lines? <william@wilbur.25thandClement.com> - 2016-12-15 10:14 -0800
Re: Why is this outputting alternate blank lines? raltbos@xs4all.nl (Richard Bos) - 2016-12-15 20:15 +0000
Re: Why is this outputting alternate blank lines? Tim Rentsch <txr@alumni.caltech.edu> - 2016-12-15 01:51 -0800
Re: Why is this outputting alternate blank lines? asetofsymbols@gmail.com - 2016-12-15 06:09 -0800
Re: Why is this outputting alternate blank lines? Tim Rentsch <txr@alumni.caltech.edu> - 2016-12-15 17:47 -0800
Re: Why is this outputting alternate blank lines? Keith Thompson <kst-u@mib.org> - 2016-12-14 12:15 -0800
Re: Why is this outputting alternate blank lines? <william@wilbur.25thandClement.com> - 2016-12-15 10:24 -0800
Re: overcommit is the plague Noob <root@127.0.0.1> - 2016-12-17 01:17 +0100
Re: Why is this outputting alternate blank lines? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-14 20:19 +0000
Re: Why is this outputting alternate blank lines? DFS <nospam@dfs.com> - 2016-12-14 17:44 -0500
Re: Why is this outputting alternate blank lines? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-15 00:21 +0000
Re: Why is this outputting alternate blank lines? mark.bluemel@gmail.com - 2016-12-15 00:56 -0800
Re: Why is this outputting alternate blank lines? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-15 22:08 +0000
Re: Why is this outputting alternate blank lines? Geoff <geoff@invalid.invalid> - 2016-12-14 13:39 -0800
Re: Why is this outputting alternate blank lines? Ike Naar <ike@iceland.freeshell.org> - 2016-12-16 07:12 +0000
csiph-web