From owner-freebsd-questions@FreeBSD.ORG Tue Mar 31 03:08:58 2009 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 33D211065675 for ; Tue, 31 Mar 2009 03:08:58 +0000 (UTC) (envelope-from josh.carroll@gmail.com) Received: from wf-out-1314.google.com (wf-out-1314.google.com [209.85.200.175]) by mx1.freebsd.org (Postfix) with ESMTP id 076718FC17 for ; Tue, 31 Mar 2009 03:08:57 +0000 (UTC) (envelope-from josh.carroll@gmail.com) Received: by wf-out-1314.google.com with SMTP id 24so2536224wfg.7 for ; Mon, 30 Mar 2009 20:08:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:reply-to:in-reply-to :references:date:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=8p0CXFMD4PiTdUKVuIjpNALldD2oqD6GAEXWGeADw/o=; b=SZ2Htk2QP/QdE8xwJF+zNCIT5e/v4/mxSc1qhxur20vINdnd2SKhf1zek//H6Wml5s c7Zmp7sHruta6/dXD9p7GtfxS0EGq5CCF5dqtGRwHVQMq1C7p1XiAMLm0qIhIARrojK4 tV8CVLqB5uh4lNAi+3Pg29FhSxnsx15Yq6Oj0= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:reply-to:in-reply-to:references:date:message-id :subject:from:to:cc:content-type:content-transfer-encoding; b=xMZ78F8MOnym52GsEvJOCIinQz5HJ8l4HB0Brp6OmKNx1GFDvDjdVs+MclYtJaOo24 uKjb9NWrq8ZDsftMxiDDeazJd0y0pGql9TS6UPvnI8IijK3fCs/NxQtWqqisfXkEed85 nPEcPadhbxKaHfz8RYw/H7OHJRVuMVuSe7GyE= MIME-Version: 1.0 Received: by 10.142.44.11 with SMTP id r11mr2407315wfr.145.1238468937553; Mon, 30 Mar 2009 20:08:57 -0700 (PDT) In-Reply-To: <20090331025726.GA10888@thought.org> References: <20090331025726.GA10888@thought.org> Date: Mon, 30 Mar 2009 23:08:57 -0400 Message-ID: <8cb6106e0903302008j5ab06a97odbd32fb68c1a404d@mail.gmail.com> From: Josh Carroll To: Gary Kline Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Cc: FreeBSD Mailing List Subject: Re: Why?? (prog question) X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: josh.carroll@gmail.com List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 31 Mar 2009 03:08:58 -0000 On Mon, Mar 30, 2009 at 10:57 PM, Gary Kline wrote: > people, i've been under the weather for days and will probably be for a f= ew more. > new =A0and TEMPORARY meds dont like me, ugh. > > can anybody clue me in why the followin joinline program fails to catch i= f argc =3D=3D 1? > > > /* > =A0* simple prog to join all | very nearly all lines of a text file that > =A0* make up one paragraph into one LONG line. > =A0* > =A0* paragraphs are delimiated by a single \n break. > =A0*/ > > #include > #include > #include > > main(int argc, char argv[]) > { > =A0 char buf[65536]; > > =A0 if (argc =3D=3D 1) > =A0 { > =A0 =A0 =A0 =A0printf("Usage: %s < file > newfile\n", argv[0]); > =A0 =A0 =A0 =A0exit (-1); > =A0 } > =A0 while (fgets(buf, sizeof buf, stdin) ) > =A0 { > =A0 =A0 if (*buf =3D=3D '\n') > =A0 =A0 { > =A0 =A0 =A0 fprintf(stdout, "\n\n"); > =A0 =A0 } > =A0 =A0 else > =A0 =A0 { > =A0 =A0 =A0 buf[strlen(buf)-1] =3D ' '; > =A0 =A0 =A0 fputs(buf, stdout); > =A0 =A0 } > =A0 } > } main should be: int main(int argc, char **argv) or perhaps int main(int argc, char *argv[]) As is, you're defining int as char argv[] (e.g. char *) instead of char = **. What will likely happen is you'll get a segmentation fault when you try to run the program, since your printf format spec has %s, but you're passing it a char. In fact, if you compile it with -Wall, you'll see the two problems I've mentioned: t.c:13: warning: second argument of 'main' should be 'char **' t.c: In function 'main': t.c:20: warning: format '%s' expects type 'char *', but argument 2 has type 'int' Change char argv[] to char *argv[] or char **argv and it should work proper= ly. Note also that your main should have an int return type and should return a value. Regards, Josh