PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > Error with fgets
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Error with fgets

Réponse
 
LinkBack Outils de la discussion
Vieux 15/04/2008, 23h58   #1
blindsey
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Error with fgets

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.
  Réponse avec citation
Vieux 16/04/2008, 00h06   #2
Peter Nilsson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Error with fgets

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);
> fputs(text1,stdout);
> return(0);


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

> }


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

--
Peter
  Réponse avec citation
Vieux 16/04/2008, 00h07   #3
Flash Gordon
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Error with fgets

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.
--
Flash Gordon
  Réponse avec citation
Vieux 16/04/2008, 00h14   #4
blindsey
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Error with fgets

On Apr 15, 7:06pm, Peter Nilsson <ai...@acay.com.au> wrote:
> 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);
> > fputs(text1,stdout);
> > return(0);

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

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



Thanks!

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

Like I said, it had been a while.

  Réponse avec citation
Vieux 16/04/2008, 00h14   #5
Bartc
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Error with fgets


"blindsey" <blindsey@dsicdi.com> wrote in message
news:ff029a70-ed6b-49a7-9aab-6505af1c8ab2@p25g2000hsf.googlegroups.com...
> 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.

> 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.


--
Bart


  Réponse avec citation
Vieux 16/04/2008, 00h27   #6
Keith Thompson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Error with fgets

blindsey <blindsey@dsicdi.com> writes:
> 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.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <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"
  Réponse avec citation
Vieux 16/04/2008, 01h01   #7
CarlosB
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Error with fgets

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/c...dio/fputs.html

[]'s

  Réponse avec citation
Vieux 16/04/2008, 01h04   #8
CarlosB
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Error with fgets

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/c...dio/fputs.html

[]'s
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 06h25.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,25154 seconds with 16 queries