Error with fgets

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • blindsey

    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,std out);
    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.
  • Peter Nilsson

    #2
    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,std out);
    return(0);
    BTW, you don't need parentheses in this return statement.
    }


    --
    Peter

    Comment

    • blindsey

      #3
      Re: Error with fgets

      On Apr 15, 7:06 pm, Peter Nilsson <ai...@acay.com .auwrote:
      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,std out);
               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.

      Comment

      • Bartc

        #4
        Re: Error with fgets


        "blindsey" <blindsey@dsicd i.comwrote in message
        news:ff029a70-ed6b-49a7-9aab-6505af1c8ab2@p2 5g2000hsf.googl egroups.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,std out);
        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


        Comment

        • Morris Dovey

          #5
          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,std out);
          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.
          You're telling the compiler that you want to read from stdin into
          the memory area pointed to by text1.

          You haven't initialized text1 to point anywhere in particular,
          where do you suppose fgets is putting that data? (Hint: 'memory
          at "0x00000003 "' probably doesn't belong to you.)

          --
          Morris Dovey
          DeSoto Solar
          DeSoto, Iowa USA

          Comment

          • D. Power

            #6
            Re: Error with fgets

            In article
            <ff029a70-ed6b-49a7-9aab-6505af1c8ab2@p2 5g2000hsf.googl egroups.com>,
            blindsey <blindsey@dsicd i.comwrote:
            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 haven't provided any memory for text1, you need to either allocate
            memory for it, i.e.

            text = malloc (150);

            and don't forget to #include <stdlib.h>, or you need to declare text1 as
            an array

            char text1[150];

            Regulars in this newsgroup will point out that using a magic number like
            150 is considered bad practice, so it would be better to add

            #define BUFFSIZE 150

            or the like, then use it as

            text = malloc (BUFSIZE);

            etc.
            >
            fgets(text1,150 ,stdin);
            fputs(text1,std out);
            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.
            HTH
            DP

            Comment

            • Keith Thompson

              #7
              Re: Error with fgets

              blindsey <blindsey@dsicd i.comwrites:
              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,std out);
              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_Keit h) 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"

              Comment

              • Flash Gordon

                #8
                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

                Comment

                • CarlosB

                  #9
                  Re: Error with fgets

                  try this:

                  #include <stdio.h>

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

                  fgets(text1,siz eof(text1)-1,stdin);
                  fputs(text1,std out);
                  }

                  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,siz eof(text1)-1,stdin);
                  f=fopen("test.t xt","w");
                  if(f) {
                  fputs(text1,std out);
                  fclose(f);
                  }
                  }


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

                  []'s

                  Comment

                  • CarlosB

                    #10
                    Re: Error with fgets

                    try this:

                    #include <stdio.h>

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

                    fgets(text1,siz eof(text1)-1,stdin);
                    fputs(text1,std out);
                    }

                    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,siz eof(text1)-1,stdin);
                    f=fopen("test.t xt","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

                    Comment

                    • Keith Thompson

                      #11
                      Re: Error with fgets

                      CarlosB <carlosbar@gmai l.comwrites:
                      try this:
                      >
                      #include <stdio.h>
                      >
                      void main(void)
                      Wrong. Make it "int main(void)".

                      The comp.lang.c FAQ is at <http://c-faq.com/>. Read questions 11.12a
                      through 11.15. Then read the rest of it.
                      {
                      char text1[151]={0};
                      The initialization is harmless, but pointless since you immediately
                      read into text1.
                      fgets(text1,siz eof(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,std out);
                      return 0;
                      }
                      [...]

                      --
                      Keith Thompson (The_Other_Keit h) 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"

                      Comment

                      • Peter Nilsson

                        #12
                        Re: Error with fgets

                        CarlosB wrote:
                        try this:
                        >
                        #include <stdio.h>
                        >
                        void main(void)
                        int main(void)
                        {
                        char text1[151]={0};
                        >
                        fgets(text1,siz eof(text1)-1,stdin);
                        if (!fgets(text1, sizeof text1, stdin))
                        {
                        /* error */
                        }
                        fputs(text1,std out);
                        }
                        --
                        Peter

                        Comment

                        • CBFalconer

                          #13
                          Re: Error with fgets

                          Flash Gordon 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);
                          >
                          <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.
                          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/>

                          --
                          [mail]: Chuck F (cbfalconer at maineline dot net)
                          [page]: <http://cbfalconer.home .att.net>
                          Try the download section.


                          ** Posted from http://www.teranews.com **

                          Comment

                          • vippstar@gmail.com

                            #14
                            Re: Error with fgets

                            On Apr 16, 8:17 am, CBFalconer <cbfalco...@yah oo.comwrote:
                            Flash Gordon 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);
                            >
                            <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.
                            >
                            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)

                            Comment

                            • CBFalconer

                              #15
                              Re: Error with fgets

                              vippstar@gmail. com wrote:
                              CBFalconer <cbfalco...@yah oo.comwrote:
                              >
                              .... snip ...
                              >>
                              >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)
                              Good points. However, ggets only returns 0, 1, or EOF, which
                              requires exactly an int. I did put it in public domain, so go
                              ahead and make the changes. I have some doubts about the ungetc,
                              since once memory is no longer available it is possible that ungetc
                              no longer works.

                              --
                              [mail]: Chuck F (cbfalconer at maineline dot net)
                              [page]: <http://cbfalconer.home .att.net>
                              Try the download section.


                              ** Posted from http://www.teranews.com **

                              Comment

                              Working...