Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #12275

Re: no error by fscanf on reading from output file

From Ian Collins <ian-news@hotmail.com>
Newsgroups comp.lang.c
Subject Re: no error by fscanf on reading from output file
Date 2011-10-30 20:19 +1300
Message-ID <9h4c3fFgamU1@mid.individual.net> (permalink)
References <9b198cc9-d374-4a14-a100-91b74eb12d9e@a12g2000prb.googlegroups.com>

Show all headers | View raw


On 10/30/11 07:59 PM, V.Subramanian, India wrote:
> This question is for learning purpose only.
>
> Consider the program sample.c :
>
> #include<stdlib.h>
> #include<stdio.h>
> #include<errno.h>
> #include<string.h>
>
> int main()
> {
>          errno = 0;
>
>          FILE *fp = fopen("data.txt", "w");
>
>          if (fp == NULL)
>          {
>                  fprintf(stderr,
>                          "Could not open input file - data.txt\n%s\n",
>                          strerror(errno));
>
>                  return EXIT_FAILURE;
>          }
>
>          if (fprintf(fp, "test data")<  1)
>          {
>                  fprintf(stderr,
>                          "could not write message into data.txt\n");
>
>                  return EXIT_FAILURE;
>          }
>
>          fflush(fp);
>
>          int a;
>          int b;
>
>          errno = 0;
>
>          // I deliberately try to read from the file
>          // opened in 'write' mode
>          if (fscanf(fp, "%d%d",&a,&b) == 2)

You don't test the return of fscanf.  Do so and see what happens.

>          {
>                  fprintf(stdout,
>                          "a = %d, b= %d\n",
>                          a,
>                          b);
>                  fflush(stdout);
>                  fclose(fp);
>                  return EXIT_SUCCESS;
>          }
>
>          int err = errno;
>
>          if (ferror(fp))
>          {
>                  fprintf(stderr,
>                           "Error encountered while reading input"
>                           " from file\n%s\n",
>                           strerror(err));
>                  fclose(fp);
>
>                  return EXIT_FAILURE;
>          }
>
>          fclose(fp);
>
>          return EXIT_SUCCESS;
> }
>
> In this program I open a file 'data.txt' in 'write' mode.
> Subsequently, I write something into this file. Then I read
> from this file using 'fscanf'. This operation should fail.

But to fail to check!

> But when I test the error indicator for the stream by
> calling 'ferror(fp)', the statements inside the
>          if (ferror(fp))
>          {
>           }
> are NOT executed.
> Does this mean that error inidcator for the stream is not set
> by 'fscanf' ? Isn't the operation 'reading from a file
> opened in "write" mode' an error ?

No, because the FILE* argument isn't a valid input stream.  There's 
nothing wrong with the stream, you are just using it wrong.

> I am unable to understand this.

Try opening the stream "r+" and then you will see an error condition 
occur on the stream.

-- 
Ian Collins

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

no error by fscanf on reading from output file "V.Subramanian, India" <subramanian100in@yahoo.com> - 2011-10-29 23:59 -0700
  Re: no error by fscanf on reading from output file Ian Collins <ian-news@hotmail.com> - 2011-10-30 20:19 +1300
    Re: no error by fscanf on reading from output file Ike Naar <ike@iceland.freeshell.org> - 2011-10-30 08:18 +0000
      Re: no error by fscanf on reading from output file Ian Collins <ian-news@hotmail.com> - 2011-10-30 21:23 +1300
        Re: no error by fscanf on reading from output file Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-10-30 08:11 -0400
          Re: no error by fscanf on reading from output file Ian Collins <ian-news@hotmail.com> - 2011-10-31 07:38 +1300
            Re: no error by fscanf on reading from output file Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-10-30 19:40 +0000
              Re: no error by fscanf on reading from output file Ian Collins <ian-news@hotmail.com> - 2011-10-31 08:50 +1300
  Re: no error by fscanf on reading from output file "V.Subramanian, India" <subramanian100in@yahoo.com> - 2011-10-30 00:01 -0700
  Re: no error by fscanf on reading from output file Ike Naar <ike@iceland.freeshell.org> - 2011-10-30 08:39 +0000
  Re: no error by fscanf on reading from output file Carlo Dapor <catull@gmail.com> - 2011-10-30 19:16 -0700
    Re: no error by fscanf on reading from output file Ian Collins <ian-news@hotmail.com> - 2011-10-31 15:50 +1300
      Re: no error by fscanf on reading from output file "V.Subramanian, India" <subramanian100in@yahoo.com> - 2011-10-30 22:55 -0700
        Re: no error by fscanf on reading from output file pacman@kosh.dhis.org (Alan Curry) - 2011-10-31 07:14 +0000
          Re: no error by fscanf on reading from output file Ian Collins <ian-news@hotmail.com> - 2011-10-31 20:56 +1300
          Re: no error by fscanf on reading from output file Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-10-31 15:43 +0000
        Re: no error by fscanf on reading from output file Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-10-31 01:35 -0700
          Re: no error by fscanf on reading from output file Keith Thompson <kst-u@mib.org> - 2011-10-31 13:46 -0700
    Re: no error by fscanf on reading from output file Ike Naar <ike@iceland.freeshell.org> - 2011-10-31 07:02 +0000

csiph-web