Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Program not giving any output (http://www.velocityreviews.com/forums/t737419-program-not-giving-any-output.html)

Maxx 11-07-2010 01:02 PM

Program not giving any output
 
Here i have a program which was supposed to do the following:

Example INPUT:::
Dolorian refused to release the
lyrics for the track 'A Part Of Darkness'
on 'When All The Laughter Has Gone',
and several parts of the lyrics on almost

Example OUTPUT:::::
Dolorian refused to release the
Lyrics for the track 'A Part Of Darkness'
On 'When All The Laughter Has Gone',
And several parts of the lyrics on almost

What this program does is basically checks the first letter of every
line and changes it to upper case.

Here is the program:::::


#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define BUFFER 5000

int main(int argc, char **argv)
{
FILE *fp;
int a[BUFFER];
int ch,*p=a,*c=a;
if((fp=fopen(argv[1],"a")) == NULL)
{
fprintf(stderr,"%s can't open file %s",argv[0],argv[1]);
exit(1);
}
while((ch=getc(fp))!=EOF)
{
*p++=ch;
}*p='\0';
for(;*c!='\0';c++)
{
if(*c==10 || *c==13)
{
++c;
if(islower(*c))
{
*c=toupper(*c);
}
}
}
while(*c++!='\0')
{
putc(*c,fp);
}
fclose(fp);
return 0;
}

It's get compiled with zero error/warning but when i run it against
any file it produces no output or change.The input remains the
same.please help i'm having tough time solving it.

Ben Bacarisse 11-07-2010 01:57 PM

Re: Program not giving any output
 
Maxx <grungeddd.maxx@gmail.com> writes:

> Here i have a program which was supposed to do the following:
>
> Example INPUT:::
> Dolorian refused to release the
> lyrics for the track 'A Part Of Darkness'
> on 'When All The Laughter Has Gone',
> and several parts of the lyrics on almost
>
> Example OUTPUT:::::
> Dolorian refused to release the
> Lyrics for the track 'A Part Of Darkness'
> On 'When All The Laughter Has Gone',
> And several parts of the lyrics on almost
>
> What this program does is basically checks the first letter of every
> line and changes it to upper case.
>
> Here is the program:::::
>
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <ctype.h>
> #define BUFFER 5000


There is no obvious need to store a large number of anything in this
program. It's a good exercise to re-think it so that the data you need
to store is unrelated to how long the input or input lines might be.

> int main(int argc, char **argv)
> {
> FILE *fp;
> int a[BUFFER];
> int ch,*p=a,*c=a;


These are not very helpful names.

> if((fp=fopen(argv[1],"a")) == NULL)


What do you think "a" does here? If you are not sure, look it up. I
could tell you, but you'll need to look up other stuff tomorrow, so find
some book or on-line reference now and be prepared.

But there are other points. What happens if argc is zero or one? Why
limit this program to files that can be named an opened? That might
have been part of the specification (it's not at all clear) but if not,
it would be better to make this program a filter: one that (at least
when no arguments are supplied) reads stdin and writes the modified data
to stdout.

> {
> fprintf(stderr,"%s can't open file %s",argv[0],argv[1]);
> exit(1);
> }
> while((ch=getc(fp))!=EOF)
> {
> *p++=ch;


Add more white space to your code! What happens when 'p' runs off the
end of 'a'?

> }*p='\0';
> for(;*c!='\0';c++)


It's possible that 'c' can run past the end of you data. Can you work
out how that can happen?

> {
> if(*c==10 || *c==13)


(1) C makes sure that system dependent line terminations are converted
into the single '\n' character (for text streams). (2) Why write a
non-portable and mysterious number rather than '\n'?

> {
> ++c;
> if(islower(*c))
> {
> *c=toupper(*c);
> }
> }
> }


Assuming that you don't run off the array, one thing you can be sure of
here is that *c is == 0 or the for loop would not have finished...

> while(*c++!='\0')


so this loop won't do anything at all.

> {
> putc(*c,fp);
> }
> fclose(fp);
> return 0;
> }
>
> It's get compiled with zero error/warning but when i run it against
> any file it produces no output or change.The input remains the
> same.please help i'm having tough time solving it.


--
Ben.

Eric Sosman 11-07-2010 02:32 PM

Re: Program not giving any output
 
On 11/7/2010 8:02 AM, Maxx wrote:
> Here i have a program which was supposed to do the following:
>
> Example INPUT:::
> Dolorian refused to release the
> lyrics for the track 'A Part Of Darkness'
> on 'When All The Laughter Has Gone',
> and several parts of the lyrics on almost
>
> Example OUTPUT:::::
> Dolorian refused to release the
> Lyrics for the track 'A Part Of Darkness'
> On 'When All The Laughter Has Gone',
> And several parts of the lyrics on almost
>
> What this program does is basically checks the first letter of every
> line and changes it to upper case.
>
> Here is the program:::::
>
>
> #include<stdio.h>
> #include<stdlib.h>
> #include<ctype.h>
> #define BUFFER 5000
>
> int main(int argc, char **argv)
> {
> FILE *fp;
> int a[BUFFER];
> int ch,*p=a,*c=a;
> if((fp=fopen(argv[1],"a")) == NULL)


Do you know what "a" means here? It means "open for appending."
You are opening a stream for *output* to the end of the file, not
for input of any kind. You probably mean "r+", which means "open
for reading and updating." A safer alternative might be to read
from one file and write another; "safer" because if your program
does something weird it won't have trashed the original input.

(Also, it would be a good idea to check that argv[1] is actually
present before trying to use it.)

> {
> fprintf(stderr,"%s can't open file %s",argv[0],argv[1]);
> exit(1);
> }
> while((ch=getc(fp))!=EOF)
> {
> *p++=ch;
> }*p='\0';
> for(;*c!='\0';c++)
> {
> if(*c==10 || *c==13)


Side-note: You probably mean '\n' and '\r' instead of
those mysterious magic numbers. Furthermore, you don't need
to worry about the '\r' at all: C's text I/O streams always
use a single '\n' to mark the end of a line, regardless of
what's actually recorded in a file.

> {
> ++c;
> if(islower(*c))
> {
> *c=toupper(*c);
> }
> }
> }


At this moment in your program, where does `c' point?
With that in mind, how many times will the body of the next
loop execute?

> while(*c++!='\0')
> {
> putc(*c,fp);
> }
> fclose(fp);
> return 0;
> }
>
> It's get compiled with zero error/warning but when i run it against
> any file it produces no output or change.The input remains the
> same.please help i'm having tough time solving it.


Side-note, this program is considerably more complicated (and
self-limited) than it needs to be to accomplish its stated task.
As you learn more, you'll see ways to simplify and improve it --
but right now, let's focus on the basics.

--
Eric Sosman
esosman@ieee-dot-org.invalid

Morris Keesan 11-07-2010 04:20 PM

Re: Program not giving any output
 
On Sun, 07 Nov 2010 08:02:02 -0500, Maxx <grungeddd.maxx@gmail.com> wrote:
....
> What this program does is basically checks the first letter of every
> line and changes it to upper case.

....
> if(*c==10 || *c==13)
> {
> ++c;
> if(islower(*c))
> {
> *c=toupper(*c);
> }
> }


Eric Sosman and and Ben Bacarisse have correctly diagnosed most of the
problems in your code, but there's another slightly subtle one.

What will your program do if the FIRST line of input begins with a
lower-case letter?
--
Morris Keesan -- mkeesan@post.harvard.edu

Chad 11-07-2010 07:00 PM

Re: Program not giving any output
 
On Nov 7, 6:32*am, Eric Sosman <esos...@ieee-dot-org.invalid> wrote:

> * * *Side-note, this program is considerably more complicated (and
> self-limited) than it needs to be to accomplish its stated task.
> As you learn more, you'll see ways to simplify and improve it --
> but right now, let's focus on the basics.
>


I don't think this is what the op was looking for, but like, here is
what I came up with...

[cdalten@localhost oakland]$ more dol
Dolorian refused to release the
lyrics for the track 'A Part Of Darkness'
on 'When All The Laughter Has Gone',
and several parts of the lyrics on almost
[cdalten@localhost oakland]$
[cdalten@localhost oakland]$ more lotoup.c
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

#define PATH "/home/cdalten/oakland/dol"

int main(void)
{
FILE *fp;
char buf[BUFSIZ];

if ((fp = fopen(PATH, "r")) == NULL) {
fprintf(stderr, "Can't open input file\n");
exit(EXIT_FAILURE);
}

while (fgets(buf, BUFSIZ, fp) != NULL) {
printf("%c%s",toupper(buf[0]), &buf[1]);
}

printf("\n");

fclose(fp);
exit(EXIT_SUCCESS);
}
[cdalten@localhost oakland]$ gcc -Wall -Wextra lotoup.c -o lotoup
[cdalten@localhost oakland]$ ./lotoup
Dolorian refused to release the
Lyrics for the track 'A Part Of Darkness'
On 'When All The Laughter Has Gone',
And several parts of the lyrics on almost
[cdalten@localhost oakland]$



Ben Bacarisse 11-07-2010 08:26 PM

Re: Program not giving any output
 
Chad <cdalten@gmail.com> writes:
<snip>
> #include <stdio.h>
> #include <stdlib.h>
> #include <ctype.h>
>
> #define PATH "/home/cdalten/oakland/dol"
>
> int main(void)
> {
> FILE *fp;
> char buf[BUFSIZ];


Why link the line length that you are prepared to accept with the size
of an internal IO buffer?

> if ((fp = fopen(PATH, "r")) == NULL) {
> fprintf(stderr, "Can't open input file\n");
> exit(EXIT_FAILURE);
> }


It seems a shame to insist that the data be in one named file. Even if
not required, isn't it easier to test if the program is more generous
about what it accepts?

> while (fgets(buf, BUFSIZ, fp) != NULL) {
> printf("%c%s",toupper(buf[0]), &buf[1]);
> }


But here's the key thing: by insisting on reading a line at a time, you
complicate the program and introduce a limit at the same time. Can you
think of a way to avoid an line length limit? Does it simplify the
program?

> printf("\n");


What's this for?

> fclose(fp);
> exit(EXIT_SUCCESS);
> }


<snip>
--
Ben.

Eric Sosman 11-07-2010 09:48 PM

Re: Program not giving any output
 
On 11/7/2010 2:00 PM, Chad wrote:
>
> I don't think this is what the op was looking for, but like, here is
> what I came up with...


In addition to Ben's comments, allow me to point out this
all-too-common howler:

> char buf[BUFSIZ];
>[...]
> printf("%c%s",toupper(buf[0]),&buf[1]);


Use `toupper( (unsigned char)buf[0] )' instead. I won't explain
why, because it's been explained over and over and over; peruse
the archives (or your C reference) for yourself.

--
Eric Sosman
esosman@ieee-dot-org.invalid

Chad 11-07-2010 09:53 PM

Re: Program not giving any output
 
On Nov 7, 12:26*pm, Ben Bacarisse <ben.use...@bsb.me.uk> wrote:

> But here's the key thing: by insisting on reading a line at a time, you
> complicate the program and introduce a limit at the same time. *Can you
> think of a way to avoid an line length limit? *Does it simplify the
> program?
>
> > * printf("\n");

>
> What's this for?
>


Here is how I avoided the line length limit...

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int main(int argc, char **argv)
{
FILE *fp;
int c;

if (argc != 2) {
fprintf(stderr, "Enter a file name.\n");
exit(EXIT_FAILURE);
}

if ((fp = fopen(argv[1], "r")) == NULL) {
fprintf(stderr, "Can't open %s file.\n", argv[1]);
exit(EXIT_FAILURE);
}

while ((c = getc(fp)) != EOF) {
if ((fputc(c, stdout)) == EOF) {
fprintf(stderr, "Output error.\n");
exit(EXIT_FAILURE);
}
if (c == '\n') {
if ((c = getc(fp)) == EOF) {
fprintf(stderr, "getc error.\n");
exit(EXIT_FAILURE);
}

if ((fputc((unsigned char)toupper(c), stdout)) == EOF) {
fprintf(stderr, "Output error.\n");
exit(EXIT_FAILURE);
}
}
}

/*If there isn't a newline after the last character in the file*/
printf("\n");

fclose(fp);
exit(EXIT_SUCCESS);
}

I'm not too sure if this approach simplifies the program.

Eric Sosman 11-07-2010 10:55 PM

Re: Program not giving any output
 
On 11/7/2010 4:53 PM, Chad wrote:
> On Nov 7, 12:26 pm, Ben Bacarisse<ben.use...@bsb.me.uk> wrote:
>
>> But here's the key thing: by insisting on reading a line at a time, you
>> complicate the program and introduce a limit at the same time. Can you
>> think of a way to avoid an line length limit? Does it simplify the
>> program?
>>
>>> printf("\n");

>>
>> What's this for?
>>

>
> Here is how I avoided the line length limit...
>
> #include<stdio.h>
> #include<stdlib.h>
> #include<ctype.h>
>
> int main(int argc, char **argv)
> {
> FILE *fp;
> int c;
>
> if (argc != 2) {
> fprintf(stderr, "Enter a file name.\n");
> exit(EXIT_FAILURE);
> }
>
> if ((fp = fopen(argv[1], "r")) == NULL) {
> fprintf(stderr, "Can't open %s file.\n", argv[1]);
> exit(EXIT_FAILURE);
> }
>
> while ((c = getc(fp)) != EOF) {
> if ((fputc(c, stdout)) == EOF) {
> fprintf(stderr, "Output error.\n");
> exit(EXIT_FAILURE);
> }
> if (c == '\n') {
> if ((c = getc(fp)) == EOF) {
> fprintf(stderr, "getc error.\n");
> exit(EXIT_FAILURE);
> }
>
> if ((fputc((unsigned char)toupper(c), stdout)) == EOF) {
> fprintf(stderr, "Output error.\n");
> exit(EXIT_FAILURE);
> }
> }
> }
>
> /*If there isn't a newline after the last character in the file*/
> printf("\n");
>
> fclose(fp);
> exit(EXIT_SUCCESS);
> }
>
> I'm not too sure if this approach simplifies the program.


Not much, but it introduces a new bug. And it's odd that
all the scrupulous error-checking of each fputc() (why not putc()
or putchar(), by the way?) is not matched by similar checking for
the printf() -- but then, the printf() seems to be extraneous
anyhow. And it's got a useless cast. And it's ugly.

If you haven't spotted the new bug yet, try this input:

oh what a tangled web we weave
when first we practice to write c!

that should be "ceave," but what the heck:
it wouldn't pass a spelling check.

--
Eric Sosman
esosman@ieee-dot-org.invalid

Chad 11-07-2010 11:27 PM

Re: Program not giving any output
 
On Nov 7, 2:55*pm, Eric Sosman <esos...@ieee-dot-org.invalid> wrote:
> * * *Not much, but it introduces a new bug. *And it's odd that
> all the scrupulous error-checking of each fputc() (why not putc()
> or putchar(), by the way?) is not matched by similar checking for
> the printf() -- but then, the printf() seems to be extraneous
> anyhow. *And it's got a useless cast. *And it's ugly.
>
> * * *If you haven't spotted the new bug yet, try this input:
>
> oh what a tangled web we weave
> when first we practice to write c!
>
> that should be "ceave," but what the heck:
> it wouldn't pass a spelling check.
>


You do realize that this is probably a case that the OP isn't ready
for yet don't you? I hate to break it to you, but there are some
posters here that don't consider every case. This is because there are
some posters that are rookies when it comes to structured programming.
With that, I'm about a half second from entering you into my kill
file.

Chad


All times are GMT. The time now is 04:27 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.