Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #99363

Re: Why is this outputting alternate blank lines?

From DFS <nospam@dfs.com>
Newsgroups comp.lang.c
Subject Re: Why is this outputting alternate blank lines?
Date 2016-12-14 12:53 -0500
Organization A noiseless patient Spider
Message-ID <o2s0sb$tb$1@dont-email.me> (permalink)
References <o2qh45$9ae$1@dont-email.me> <o2r7cs$6l1$1@dont-email.me>

Show all headers | View raw


On 12/14/2016 05:38 AM, Richard Heathfield wrote:
> On 14/12/16 04:18, DFS wrote:
>> Trying to read in a file of date string (in this case 10) and add them
>> to an array
>>
>> input
>> 22-May-1819
>> 01-Jan-1268
>> 03-May-1417
>> 01-Mar-1250
>> 05-Oct-1566
>> 21-Feb-1629
>> 23-Jun-1478
>> 18-Feb-1576
>> 07-Jan-1099
>> 21-Mar-1299
>>
>>
>> =====================================================
>> #include <stdio.h>
>> #include <stdlib.h>
>> #include <string.h>
>>
>> int main()
>> {
>>   int i=0,n=0;
>>   char *datestr[20];  //segfaults unless 2*input lines
>>   char line[12];
>
> Melzzzzz and Mark have already pointed out that this isn't big enough to
> read a complete line of data (including the newline character), and
> increasing it to 13 will solve one of your problems. But they both
> missed some points:
>
>>   FILE *fp = fopen("datestrings.txt","r");
>
> What if the file fails to open? You should test fp against NULL. If the
> file can't be opened, you can't read from it, and the attempt is likely
> to crash your program.
>
>>   while(fgets(line,sizeof(line),fp)!=NULL) {
>>     datestr[i]=malloc(sizeof(line));
>
> You should check that this succeeds before attempting to use the pointer:
>
> datestr[i] = malloc(sizeof line);
> if(datestr[i] != NULL)
> {
>
> and make an intelligent decision about what to do if the allocation
> fails. I can't help wondering why you are dynamically allocating the
> width of the string, the length of which you know, but not the number of
> items in the array, which is more prone to change.
>
>>     strcpy(datestr[i],line);
>
> Is a newline part of the data? Do you really want to store it?

No, but I don't know how NOT to store it.



>>     i++;n++;
>>   }
>>   fclose(fp);
>>   for (i=0;i<n;i++){
>>    printf("%s\n", datestr[i]);
>
> And now, let's look at what you're printing. What did you read into
> position 0 of the array? Answer: you tried to read "22-May-1819\n" but
> fgets wouldn't put the newline into the array, saving it for the next
> element. Now, you print a newline yourself --- %s\n rather than %s ---
> so we would expect to see in your output the date ("22-May-1819") plus a
> newline ("\n"). Now we come onto element 1 of the array, which just
> contains a newline on its own ("\n"). Now, you print the data, whatever
> it might be, plus a newline. Since the data is a newline too, you get
> two newlines: "\n\n".
>
> So your output so far is: "22-May-1819\n\n\n", and we'd therefore expect
> to see your dates separated by *two* blank lines:
>
> DATE 0 + first newline
> second newline
> third newline
> DATE 1 + first newline
> second newline
> third newline
>
> etc.
>
> So let's take a look at the data you posted:
>
>>   }
>> }
>> =====================================================
>>
>> output
>> 22-May-1819
>>
>>
>> 01-Jan-1268
>>
>>
>> 03-May-1417
>>
>>
>> 01-Mar-1250
>
> Prediction confirmed.
>
> Solution:
>
> (a) make your line buffer a touch bigger, so that there's room for the
> newline when each line is read; AND
> (b) EITHER get rid of that newline before you process your data, OR
> don't print an extra newline --- use %s rather than %s\n --- when you
> print the data. (Choose one solution or the other. If you choose both,
> all your data will appear on one line!)


Thanks.  That worked.

I updated the program to count the lines in the input file, then set the 
array size to the linecount.  Plus added a little error checking.

The new code reads and printfs up to 1 million lines just fine, but 
bombs somewhere between 1 million and 1.1 million.


$ ./builddates 1100000 datestrings.txt

$ ./readdates datestringsXXX.txt
Failed to open file

$ ./readdates datestrings.txt
1100000 lines in file 'datestrings.txt'
Segmentation fault (core dumped)


Any idea why it crashes above 1 million?


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])];
	FILE *fp; char c;

	//open file
	strcpy(txtfile,argv[1]);
	fp=fopen(txtfile,"r");
	if(fp==NULL){
	  printf("Failed to open file\n");
	  return -1;
	}


	//count lines in file
	c=getc(fp);
	while (c!=EOF){
	  if(c=='\n'){linecount+=1;}
	  c=getc(fp);
	}
	fclose(fp);
	printf("%d lines in file '%s'\n",linecount,txtfile);


	//copy data into array
	char *datestr[linecount];
	fp = fopen(txtfile,"r");
	while(fgets(line,linewidth,fp)!=NULL) {
	 datestr[i]=malloc(linewidth);
	 strcpy(datestr[i],line);
	 i++;n++;
	}
	fclose(fp);

	//print array
   for (i=0;i<n;i++){printf("%s", datestr[i]);}
}
================================================================

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


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