Error with fgets

B

blindsey

Trying to use c for the first time in years and blowing out in fgets.

I created this very simple program which still produces the error:

#include <stdio.h>

int main(int argc, char *argv[])
{
FILE *fp1;
char *text1;

fgets(text1,150,stdin);
fputs(text1,stdout);
return(0);
}

The error is "The instruction at "0x78022901" referenced memory at
"0x00000003". The memory could not be "written"." I get it after
typing in some text and hitting enter.

I'm using gcc.
 
F

Flash Gordon

blindsey wrote, On 15/04/08 23:58:
Trying to use c for the first time in years and blowing out in fgets.

I created this very simple program which still produces the error:

#include <stdio.h>

int main(int argc, char *argv[])
{
FILE *fp1;
char *text1;

fgets(text1,150,stdin);

<snip>

You have declared text1 as being a pointer, where does it point to? Go
to the comp.lang.c FAQ at http://c-faq.com/ and read about arrays and
pointers.
 
B

blindsey

blindsey said:
Trying to use c for the first time in years and blowing out in fgets.
I created this very simple program which still produces the error:
#include <stdio.h>
       int main(int argc, char *argv[])
       {
         FILE *fp1;
         char *text1;
         fgets(text1,150,stdin);
         fputs(text1,stdout);
         return(0);

BTW, you don't need parentheses in this return statement.
       }

 http://c-faq.com/aryptr/aryptr2.html


Thanks!

Ah, in the old days I never would have ...

Like I said, it had been a while.
 
B

Bartc

blindsey said:
Trying to use c for the first time in years and blowing out in fgets.

I created this very simple program which still produces the error:

#include <stdio.h>

int main(int argc, char *argv[])
{
FILE *fp1;
char *text1;

You should initialise text1 here, to point to an array of at least 150
chars, or using malloc to allocate at least 150 chars.

Otherwise it will contain an arbitrary (and illegal) address. Such as
0x00000003.
 
K

Keith Thompson

blindsey said:
Trying to use c for the first time in years and blowing out in fgets.

I created this very simple program which still produces the error:

#include <stdio.h>

int main(int argc, char *argv[])
{
FILE *fp1;

You never initialize fp1, but that's ok since you never use it.
char *text1;

text1 is a pointer. You never initialize it, so it points to some
arbitrary location in memory -- or it doesn't point anywhere at all.
fgets(text1,150,stdin);

Now you're trying to read up to 150 bytes of information from stdin
into some unspecified location. Kaboom.
fputs(text1,stdout);
return(0);
}

The error is "The instruction at "0x78022901" referenced memory at
"0x00000003". The memory could not be "written"." I get it after
typing in some text and hitting enter.

I'm using gcc.

One fix would be to declare text1 as an array rather than as a
pointer. Another would be to allocate some memory for text1 to point
to, perhaps using malloc().

The comp.lang.c FAQ is at <http://www.c-faq.com/>. Reading the whole
thing probably wouldn't be a bad use of your time.
 
C

CarlosB

try this:

#include <stdio.h>

void main(void)
{
char text1[151]={0};

fgets(text1,sizeof(text1)-1,stdin);
fputs(text1,stdout);
}

or, to save into a file whatever you type in the keyboard:

#include <stdio.h>

void main(void)
{
char text1[151]={0};
FILE *f;

fgets(text1,sizeof(text1)-1,stdin);
f=fopen("test.txt","w");
if(f) {
fputs(text1,stdout);
fclose(f);
}
}


there is a very alike example in: http://www.cplusplus.com/reference/clibrary/cstdio/fputs.html

[]'s
 
C

CarlosB

try this:

#include <stdio.h>

void main(void)
{
char text1[151]={0};

fgets(text1,sizeof(text1)-1,stdin);
fputs(text1,stdout);
}

or, to save into a file whatever you type in the keyboard:

#include <stdio.h>

void main(void)
{
char text1[151]={0};
FILE *f;

fgets(text1,sizeof(text1)-1,stdin);
f=fopen("test.txt","w");
if(f) {
fputs(text1,f);
fclose(f);
}
}

there is a very alike example in: http://www.cplusplus.com/reference/clibrary/cstdio/fputs.html

[]'s
 
K

Keith Thompson

CarlosB said:
try this:

#include <stdio.h>

void main(void)

Wrong. Make it "int main(void)".

{
char text1[151]={0};

The initialization is harmless, but pointless since you immediately
read into text1.
fgets(text1,sizeof(text1)-1,stdin);

Why subtract 1? fgets reads at most N-1 characters; it stores them in
the buffer followed by a '\0'.
fputs(text1,stdout);

return 0;

[...]
 
V

vippstar

Flash said:
blindsey wrote:
Trying to use c for the first time in years and blowing out in
fgets. I created this very simple program which still produces
the error:
#include <stdio.h>
int main(int argc, char *argv[]) {
FILE *fp1;
char *text1;
fgets(text1,150,stdin);

You have declared text1 as being a pointer, where does it point
to? Go to the comp.lang.c FAQ at http://c-faq.com/and read about
arrays and pointers.

Or, simpler, get ggets(), which is fully portable and written in
purely standard C. Available in source form at:

<http://cbfalconer.home.att.net/download/>
Reading the code in ggets.c I noticed that if you run out of memory
the byte read from the stream is dropped (lost). I suggest you use
ungetc() to push it back to the stream. Also, changing ix and cursize
to size_t wouldn't do any harm but good. (you can drop the casts in
realloc, and you'll be able to claim it's in standard C, since files
might have lines more than INT_MAX bytes long)
 

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

Similar Threads


Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,224
Latest member
BettieToom

Latest Threads

Top