I'm facing a run-time error

M

Maxx

Lately i'm having a problem running this program:

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


int main(int argc, char *argv[])
{
FILE *fp;
int ch;
if((fp=fopen(argv[1],"r")) == NULL)
fprintf(stderr,"Error can't open file: %s",*argv[1]);

while((ch=getc(fp)) !=EOF)
{
if(isalpha(ch) && islower(ch)) {
ch=toupper(ch);
putc(ch,fp);
}
}
fclose(fp);r
return 0;
}

i'm getting this error:
The instruction at "0x004045e3" referenced memory at "0x0000006c". The
memory could not be read.

I can't make any headway with this error. please help..
 
S

Seebs

if((fp=fopen(argv[1],"r")) == NULL)
fprintf(stderr,"Error can't open file: %s",*argv[1]);

*argv[1] is the first character of argv[1]. %s is expecting a pointer
to the whole of argv[1].

Drop the *.
The instruction at "0x004045e3" referenced memory at "0x0000006c". The
memory could not be read.

6c is the ASCII value of 'l', so the first character of the file name was
'l'. Neat, huh!

-s
 
J

Jens Thoms Toerring

Maxx said:
Lately i'm having a problem running this program:
#include <stdio.h>
#include <ctype.h>

int main(int argc, char *argv[])
{
FILE *fp;
int ch;
if((fp=fopen(argv[1],"r")) == NULL)
fprintf(stderr,"Error can't open file: %s",*argv[1]);

The '*' in front of 'argv[1]' is wrong - '%s' expects a pointer
to the first element of a string char array). 'argv[1] is such a
pointer and by dereferencing it you pass it the first character
in that array instead, in your case that seems to be the chara-
cter 'l' (that's the 0x6c you get in the error message). You also
should check that 'argc' is at least 2, otherwise 'argv[1]' is
going to be a NULL pointer and then using it in the call of
fopen() or as a string argument for fprint() invokes undefined
behaviour.
while((ch=getc(fp)) !=EOF)
{
if(isalpha(ch) && islower(ch)) {

I would think that islower() alone will do just fine. The standard
actually says that isalpha() is defined to be true when islower()
or isupper() is true (plus a few other conditions).
ch=toupper(ch);
putc(ch,fp);
}
}
fclose(fp);
return 0;
}
i'm getting this error:
The instruction at "0x004045e3" referenced memory at "0x0000006c". The
memory could not be read.
Regards, Jens
 
T

Tim Prince

Lately i'm having a problem running this program:

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


int main(int argc, char *argv[])
{
FILE *fp;
int ch;
if((fp=fopen(argv[1],"r")) == NULL)
fprintf(stderr,"Error can't open file: %s",*argv[1]);

while((ch=getc(fp)) !=EOF)
{
if(isalpha(ch)&& islower(ch)) {
ch=toupper(ch);
putc(ch,fp);
}
}
fclose(fp);r
return 0;
}

i'm getting this error:
The instruction at "0x004045e3" referenced memory at "0x0000006c". The
memory could not be read.

I can't make any headway with this error. please help..

$ gcc -Wall maxx.c
maxx.c: In function `main':
maxx.c:10:3: warning: format `%s' expects type `char *', but argument 3
has type
`int' [-Wformat]
maxx.c:10:3: warning: format `%s' expects type `char *', but argument 3
has type
`int' [-Wformat]
maxx.c:19:13: error: `r' undeclared (first use in this function)
maxx.c:19:13: note: each undeclared identifier is reported only once for
each fu
nction it appears in
maxx.c:20:2: error: expected `;' before `return'
maxx.c:21:1: warning: control reaches end of non-void function
[-Wreturn-type]

Consult your fopen docs (e.g. "info fopen").
It seems unlikely you quoted your case correctly, or you could not have
avoided seeing some of those messages.
 
E

Eric Sosman

Lately i'm having a problem running this program:
[...]

In addition to the problems others have mentioned, ponder
if((fp=fopen(argv[1],"r")) == NULL)
[...]
putc(ch,fp);

Does anything strike you as odd about sending output to an
input-only stream?
 
K

Keith Thompson

Maxx said:
Lately i'm having a problem running this program:

I'll point out some errors beyond the obvious ones others have
mentioned.
#include <stdio.h>
#include <ctype.h>


int main(int argc, char *argv[])
{
FILE *fp;
int ch;
if((fp=fopen(argv[1],"r")) == NULL)

What happens if the program is invoked with no arguments? Don't try
to access argv[1] unless you've confirmed that it exists by checking
the value of argc.
fprintf(stderr,"Error can't open file: %s",*argv[1]);

The error message should end with a new-line. (The incorrect use
of *argv[1] rather than argv[1] has already been mentioned.)

After printing the error message, your program continues on its merry
way as if nothing had gone wrong. This would have been a good place
to call exit(EXIT_FAILURE).
while((ch=getc(fp)) !=EOF)
{
if(isalpha(ch) && islower(ch)) {

Somebody already pointed out that the isalpha() call is redundant.
ch=toupper(ch);
putc(ch,fp);
}

Is this doing what you intend it to do? All input characters that
are lower case letters are converted to upper case and printed;
all other input characters are discarded, so "abcXYZ123" yields
"ABC". I'm not saying it's wrong, but there's nothing to indicate
the program's intended behavior, so we can't tell.
}
fclose(fp);r

Where did that trailing 'r' come from? Did you copy-and-paste the exact
source file that you compiled? If not (for example, if you re-typed
it), then you're asking us to diagnose your typing errors in addition to
whatever problem your original program might have had.

If you did copy-and-paste your source code, and the 'r' was just an
accidental typo after the fact, then that's ok (but be careful).
return 0;
}

[...]
 
B

Barry Schwarz

Maxx said:
Lately i'm having a problem running this program:
#include <stdio.h>
#include <ctype.h>

int main(int argc, char *argv[])
{
FILE *fp;
int ch;
if((fp=fopen(argv[1],"r")) == NULL)
fprintf(stderr,"Error can't open file: %s",*argv[1]);

The '*' in front of 'argv[1]' is wrong - '%s' expects a pointer
to the first element of a string char array). 'argv[1] is such a
pointer and by dereferencing it you pass it the first character
in that array instead, in your case that seems to be the chara-
cter 'l' (that's the 0x6c you get in the error message). You also

This also is undefined behavior.
should check that 'argc' is at least 2, otherwise 'argv[1]' is
going to be a NULL pointer and then using it in the call of

It is possible for argc to be 0 in which case argv[1] will not even
exist. Just another possible undefined behavior.
fopen() or as a string argument for fprint() invokes undefined
behaviour.


I would think that islower() alone will do just fine. The standard
actually says that isalpha() is defined to be true when islower()
or isupper() is true (plus a few other conditions).

The entire if statement is probably superfluous since toupper will
also check the value of ch prior to converting it.

As noted elsewhere, putc is only defined for output streams.
Therefore, this would invoke undefined behavior if you ever get here.

It is very fortunate that the undefined behavior manifested itself in
such an unmistakable and hard to ignore fashion. It would have been
nice if the OP had isolated where the problem was occurring. There
are so many possible undefined behaviors it's hard to know where to
begin.
 
K

Keith Thompson

Barry Schwarz said:
The entire if statement is probably superfluous since toupper will
also check the value of ch prior to converting it.

It's not exactly superfluous, but it may or may not be logically
correct.

The program, if the errors are corrected, converts lower case letters to
upper case and prints the result; characters other than lower case
letters are not printed at all. If that's the intent, the if statement
is necessary.

We don't know what the intent was.

[...]
 
M

Maxx

Maxx said:
Lately i'm having a problem running this program:
#include <stdio.h>
#include <ctype.h>
int main(int argc, char *argv[])
{
        FILE *fp;
        int ch;
        if((fp=fopen(argv[1],"r")) == NULL)
                fprintf(stderr,"Error can't open file: %s",*argv[1]);

The '*' in front of 'argv[1]' is wrong - '%s' expects a pointer
to the first element of a string char array). 'argv[1] is such a
pointer and by dereferencing it you pass it the first character
in that array instead, in your case that seems to be the chara-
cter 'l' (that's the 0x6c you get in the error message). You also
should check that 'argc' is at least 2, otherwise 'argv[1]' is
going to be a NULL pointer and then using it in the call of
fopen() or as a string argument for fprint() invokes undefined
behaviour.
        while((ch=getc(fp)) !=EOF)
        {
                if(isalpha(ch) && islower(ch)) {

I would think that islower() alone will do just fine. The standard
actually says that isalpha() is defined to be true when islower()
or isupper() is true (plus a few other conditions).
                        ch=toupper(ch);
                        putc(ch,fp);
                }
        }
        fclose(fp);
        return 0;
}
i'm getting this error:
The instruction at "0x004045e3" referenced memory at "0x0000006c". The
memory could not be read.

                                Regards, Jens

Oh i get it. My mistake. Finally realized that the *argv[1] argument
actually dereferenced it. I was actually thinking *argv[1] would point
to the inner pointer which was actually supposed to be printed by
fprintf but i was wrong. Thanks for the help
 
M

Maxx

Lately i'm having a problem running this program:
#include<stdio.h>
#include<ctype.h>

int main(int argc, char *argv[])
{
   FILE *fp;
   int ch;
   if((fp=fopen(argv[1],"r")) == NULL)
           fprintf(stderr,"Error can't open file: %s",*argv[1]);
   while((ch=getc(fp)) !=EOF)
   {
           if(isalpha(ch)&&  islower(ch)) {
                   ch=toupper(ch);
                   putc(ch,fp);
           }
   }
   fclose(fp);r
   return 0;
}
i'm getting this error:
The instruction at "0x004045e3" referenced memory at "0x0000006c". The
memory could not be read.
I can't make any headway with this error. please help..

$ gcc -Wall maxx.c
maxx.c: In function `main':
maxx.c:10:3: warning: format `%s' expects type `char *', but argument 3
has type
  `int' [-Wformat]
maxx.c:10:3: warning: format `%s' expects type `char *', but argument 3
has type
  `int' [-Wformat]
maxx.c:19:13: error: `r' undeclared (first use in this function)
maxx.c:19:13: note: each undeclared identifier is reported only once for
each fu
nction it appears in
maxx.c:20:2: error: expected `;' before `return'
maxx.c:21:1: warning: control reaches end of non-void function
[-Wreturn-type]

Consult your fopen docs (e.g. "info fopen").
It seems unlikely you quoted your case correctly, or you could not have
avoided seeing some of those messages.

Yeah i was putting a * in front of argv[1], which spewed the error.
Thanks for the help
 
M

Maxx

Lately i'm having a problem running this program:
[...]

     In addition to the problems others have mentioned, ponder
   if((fp=fopen(argv[1],"r")) == NULL)
[...]
                   putc(ch,fp);

     Does anything strike you as odd about sending output to an
input-only stream?

No, i was supposed to send the characters to the file stream fp.
 
M

Maxx

Maxx said:
Lately i'm having a problem running this program:

I'll point out some errors beyond the obvious ones others have
mentioned.
#include <stdio.h>
#include <ctype.h>
int main(int argc, char *argv[])
{
   FILE *fp;
   int ch;
   if((fp=fopen(argv[1],"r")) == NULL)

What happens if the program is invoked with no arguments?  Don't try
to access argv[1] unless you've confirmed that it exists by checking
the value of argc.
           fprintf(stderr,"Error can't open file: %s",*argv[1]);

The error message should end with a new-line.  (The incorrect use
of *argv[1] rather than argv[1] has already been mentioned.)

After printing the error message, your program continues on its merry
way as if nothing had gone wrong.  This would have been a good place
to call exit(EXIT_FAILURE).
   while((ch=getc(fp)) !=EOF)
   {
           if(isalpha(ch) && islower(ch)) {

Somebody already pointed out that the isalpha() call is redundant.
                   ch=toupper(ch);
                   putc(ch,fp);
           }

Is this doing what you intend it to do?  All input characters that
are lower case letters are converted to upper case and printed;
all other input characters are discarded, so "abcXYZ123" yields
"ABC".  I'm not saying it's wrong, but there's nothing to indicate
the program's intended behavior, so we can't tell.
   }
   fclose(fp);r

Where did that trailing 'r' come from?  Did you copy-and-paste the exact
source file that you compiled?  If not (for example, if you re-typed
it), then you're asking us to diagnose your typing errors in addition to
whatever problem your original program might have had.

If you did copy-and-paste your source code, and the 'r' was just an
accidental typo after the fact, then that's ok (but be careful).
   return 0;
}

[...]

--
Keith Thompson (The_Other_Keith) (e-mail address removed)  <http://www.ghoti.net/~kst>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"


Yeah i have mistakenly omitted some obvious error checks at first.
Then learned that the * in front of argv[1] was causing the
dereferencing error as pointed by some authors. and that r was just a
typing mistake. My bad. Thanks for the help
 
M

Maxx

Maxx said:
Lately i'm having a problem running this program:
#include <stdio.h>
#include <ctype.h>
int main(int argc, char *argv[])
{
        FILE *fp;
        int ch;
        if((fp=fopen(argv[1],"r")) == NULL)
                fprintf(stderr,"Error can't open file: %s",*argv[1]);
The '*' in front of 'argv[1]' is wrong - '%s' expects a pointer
to the first element of a string char array). 'argv[1] is such a
pointer and by dereferencing it you pass it the first character
in that array instead, in your case that seems to be the chara-
cter 'l' (that's the 0x6c you get in the error message). You also

This also is undefined behavior.
should check that 'argc' is at least 2, otherwise 'argv[1]' is
going to be a NULL pointer and then using it in the call of

It is possible for argc to be 0 in which case argv[1] will not even
exist.  Just another possible undefined behavior.
fopen() or as a string argument for fprint() invokes undefined
behaviour.
I would think that islower() alone will do just fine. The standard
actually says that isalpha() is defined to be true when islower()
or isupper() is true (plus a few other conditions).

The entire if statement is probably superfluous since toupper will
also check the value of ch prior to converting it.



As noted elsewhere, putc is only defined for output streams.
Therefore, this would invoke undefined behavior if you ever get here.

It is very fortunate that the undefined behavior manifested itself in
such an unmistakable and hard to ignore fashion.  It would have been
nice if the OP had isolated where the problem was occurring.  There
are so many possible undefined behaviors it's hard to know where to
begin.


The intent was to convert all the lower case letters to upper case.
and yeah isalpha() is not really necessary, i wasn't too familiar with
the standard. Thanks for the help
 
E

Eric Sosman

Lately i'm having a problem running this program:
[...]

In addition to the problems others have mentioned, ponder
if((fp=fopen(argv[1],"r")) == NULL)
[...]
putc(ch,fp);

Does anything strike you as odd about sending output to an
input-only stream?

Please don't quote signature blocks.
No, i was supposed to send the characters to the file stream fp.

Nothing strikes you as odd about that? Nothing?

Your next assignment is to create a program that reads from the
screen and writes to the keyboard.
 
K

Keith Thompson

Maxx said:
The intent was to convert all the lower case letters to upper case.
and yeah isalpha() is not really necessary, i wasn't too familiar with
the standard. Thanks for the help

That still doesn't entirely explain what you were trying to do.

What do you want to do with input characters other than lower case
letters? And where do you want the output to go?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top