ERROR IN READING FILE : File Pointer 
Author Message
 ERROR IN READING FILE : File Pointer

HI forks,

I am just started to learn C programming and can't get an idea what's wrong
with my simple
codes below. Is there anyone help me ?

I created a input file called "ascii.txt" which contains all ascii
characters equivalent to
decimal. I was trying to open that ascii.txt file using FILE fptr
pointer then read a chacter one by one into an array named ASCII_letter[].
But it gives me complete garbage numbers (I am expecting to get a decimal
number for
each of those characters, something like if I read 'A' from the file, it
print '65' on the
screen)

#include <stdio.h>
#define MAX  93    /* ASCII numbers from 32 to 126 */
#define ASCII_TABLE "ascii.txt"

int ASCII_letter[MAX];

void main(void)
{
 char letter;
 int  i;
 FILE *fptr;

 if ((fptr = fopen("ascii.txt", "r")) == NULL )
  printf("\nCannot open the file, Read Failure : ascii.txt \n");
 else
  do
  {
   for (i = 0; i <= MAX ; i++)
   {
    fscanf(fptr, "%s", ASCII_letter[i]);
    fprintf(fptr, "%s", ASCII_letter[i]);
    fprintf(fptr, "%d", ASCII_letter[i]);
   }
  }while ((letter = (fptr, fgetc(ASCII_letter[i]))) != EOF);
 fclose(fptr);

Quote:
}

Do you have any idea what did I wrong ? Or this approach is not valuable ?
Please let me know if you get some idea.

Take care and thanks.

April



Sat, 04 Oct 2003 07:42:24 GMT  
 ERROR IN READING FILE : File Pointer

Quote:

> HI forks,

> I am just started to learn C programming and can't get an idea what's wrong
> with my simple
> codes below. Is there anyone help me ?

<snipped original code>

I'm probably going to get wacked for this, but see the following:

(NOTE: your biggest problem was invalid use of fgetc, as well as reading
 twice (fgetc and fscanf) then printf ("%s") where it should have been
 printf ("%c").  Also, note my use of fgetc to start the loop, not to
 end it. Finally, you did fprintf (fptr, ...), which puts your output
 into the same file as your input -- I guess this was actually your
 largest mistake).

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

#define ASCII_TABLE "ascii.txt"
#define MAX 93

char ASCII_letter[MAX];

int main(void)
{
   int  i, j;
   int  iRc;
   FILE *fptr;

/*
** open the file, error message if failure, set the return vaule to
EXIT_FAILURE
*/
   if ((fptr = fopen(ASCII_TABLE, "r")) == NULL )
   {
      fprintf(stdout, "Cannot open the file: %s (errno:%d)\n",
ASCII_TABLE, errno);
      iRc = EXIT_FAILURE;
   }
/*
** file opened correctly, process
*/
   else
   {
   /*
   ** initialize array counter
   */
      i = 0;
   /*
   ** read until EOF or maximum characters read
   */
      while ((ASCII_letter[i] = fgetc(fptr)) != EOF && i < MAX)
         i++;

   /*
   ** if we read to EOF, back off count by one
   */
      if (ASCII_letter[i] == EOF)
         i--;

   /*
   ** print results (for your own edification)
   */
      for (j = 0; j < i; j++)
      {
         fprintf(stdout, "%c ", ASCII_letter[j]);
         fprintf(stdout, "%d\n", ASCII_letter[j]);
      }

   /*
   ** close the file, set the return value from the program
   */
      fclose(fptr);
      iRc = EXIT_SUCCESS;
   }

/*
** return to caller
*/
   return iRc;

Quote:
}



Sat, 04 Oct 2003 08:33:32 GMT  
 ERROR IN READING FILE : File Pointer



Quote:


> > HI forks,

> > I am just started to learn C programming and can't get an idea what's
wrong
> > with my simple
> > codes below. Is there anyone help me ?

> <snipped original code>

> I'm probably going to get wacked for this, but see the following:

I fixed some errors...

Quote:

> (NOTE: your biggest problem was invalid use of fgetc, as well as reading
>  twice (fgetc and fscanf) then printf ("%s") where it should have been
>  printf ("%c").  Also, note my use of fgetc to start the loop, not to
>  end it. Finally, you did fprintf (fptr, ...), which puts your output
>  into the same file as your input -- I guess this was actually your
>  largest mistake).

> #include <stdio.h>
> #include <stdlib.h>
> #include <errno.h>

> #define ASCII_TABLE "ascii.txt"
> #define MAX 93

> char ASCII_letter[MAX];

--^^^^^
Bzzzt ASCII_letter[] _needs_ to be int so it can hold EOF
 (fgetc() returns an int )
  [ why did you change it? April Chung (the OP) had it correct! :-) ]

int ASCII_letter[MAX];

- Show quoted text -

Quote:

> int main(void)
> {
>    int  i, j;
>    int  iRc;
>    FILE *fptr;

> /*
> ** open the file, error message if failure, set the return vaule to
> EXIT_FAILURE
> */
>    if ((fptr = fopen(ASCII_TABLE, "r")) == NULL )
>    {
>       fprintf(stdout, "Cannot open the file: %s (errno:%d)\n",
> ASCII_TABLE, errno);
>       iRc = EXIT_FAILURE;
>    }
> /*
> ** file opened correctly, process
> */
>    else
>    {
>    /*
>    ** initialize array counter
>    */
>       i = 0;
>    /*
>    ** read until EOF or maximum characters read
>    */
>       while ((ASCII_letter[i] = fgetc(fptr)) != EOF && i < MAX)
>          i++;

while ( i < MAX && ((ASCII_letter[i] = fgetc(fptr)) != EOF))
          i++;
so you do not assign to ASCII_letter[MAX] which is out of bounds.

Quote:
>    /*
>    ** if we read to EOF, back off count by one
>    */
>       if (ASCII_letter[i] == EOF)
>          i--;

This test is not necessary. And you are reading off the end of your array
if i==MAX. Since the condition of the following for() loop is
j < i
the i-- is not needed in either case.

- Show quoted text -

Quote:

>    /*
>    ** print results (for your own edification)
>    */
>       for (j = 0; j < i; j++)
>       {
>          fprintf(stdout, "%c ", ASCII_letter[j]);
>          fprintf(stdout, "%d\n", ASCII_letter[j]);
>       }

>    /*
>    ** close the file, set the return value from the program
>    */
>       fclose(fptr);
>       iRc = EXIT_SUCCESS;
>    }

> /*
> ** return to caller
> */
>    return iRc;
> }

HTH

Roger Bliss



Sat, 04 Oct 2003 09:52:40 GMT  
 ERROR IN READING FILE : File Pointer
See annotations

On Tue, 17 Apr 2001 09:42:24 +1000, "April Chung"

Quote:

>HI forks,

>I am just started to learn C programming and can't get an idea what's wrong
>with my simple
>codes below. Is there anyone help me ?

>I created a input file called "ascii.txt" which contains all ascii
>characters equivalent to
>decimal. I was trying to open that ascii.txt file using FILE fptr
>pointer then read a chacter one by one into an array named ASCII_letter[].
>But it gives me complete garbage numbers (I am expecting to get a decimal
>number for
>each of those characters, something like if I read 'A' from the file, it
>print '65' on the
>screen)

>#include <stdio.h>
>#define MAX  93    /* ASCII numbers from 32 to 126 */
>#define ASCII_TABLE "ascii.txt"

>int ASCII_letter[MAX];

>void main(void)

main must return int.
Quote:
>{
> char letter;
> int  i;
> FILE *fptr;

> if ((fptr = fopen("ascii.txt", "r")) == NULL )
>  printf("\nCannot open the file, Read Failure : ascii.txt \n");
> else
>  do
>  {
>   for (i = 0; i <= MAX ; i++)
>   {
>    fscanf(fptr, "%s", ASCII_letter[i]);

If you want to read a character at a time, use %c as the format
specification and provide the address of the char as the third
parameter (&ASCII_letter[i] or ASCII_letter+i).
Quote:
>    fprintf(fptr, "%s", ASCII_letter[i]);

To print one character at a time, use %c.
Quote:
>    fprintf(fptr, "%d", ASCII_letter[i]);
>   }
>  }while ((letter = (fptr, fgetc(ASCII_letter[i]))) != EOF);

Surely you mistyped this statement in your message.  It will not
compile as written.  fgetc has the wrong number of parameters.  You
should paste the code in your message to avoid these problems.

fgetc returns an int, not a char, because EOF may not fit in a char.

Quote:
> fclose(fptr);

>}

<<Remove the del for email>>


Sat, 04 Oct 2003 13:03:22 GMT  
 ERROR IN READING FILE : File Pointer

Quote:

> HI forks,

High spoon!

[snip - what's wrong?]

Quote:
> #include <stdio.h>
> #define MAX  93    /* ASCII numbers from 32 to 126 */
> #define ASCII_TABLE "ascii.txt"

> int ASCII_letter[MAX];

[snip]
Quote:
>    for (i = 0; i <= MAX ; i++)

                 ^^^^^^^^

ASCII_letter[MAX] is not a legal value to assign to. You're overwriting your
array boundary!

Quote:
>    {
>     fscanf(fptr, "%s", ASCII_letter[i]);
>     fprintf(fptr, "%s", ASCII_letter[i]);
>     fprintf(fptr, "%d", ASCII_letter[i]);
>    }

Also, if you want to read one character at a time, fgetc may be what you want.
I'm not sure at all what the three statements above are meant to accomplish.
Also, I can appreciate your wanting to work with files, but a program like this
works much better from standard in (unless you can't redirect input). So, you'd
do

    % ./a.out < ascii.txt

rather than

    % ./a.out ascii.txt

Reading from stdin has the added bonus that you can interactively test your
program:

    % ./a.out
    A
    b
    c
    X
    3
    ...
    <eof>
    [output follows]

Of course, your program does not do anything with the input yet...keep working
at it.

        david

--
fortran was the language of choice
for the same reason that three-legged races are popular.
        -- Ken Thompson, "Reflections on Trusting Trust"



Sat, 04 Oct 2003 15:30:57 GMT  
 ERROR IN READING FILE : File Pointer

Quote:

> See annotations

> On Tue, 17 Apr 2001 09:42:24 +1000, "April Chung"

...
> >#define MAX  93    /* ASCII numbers from 32 to 126 */

If you want all printable ASCII character codes, 32 to 126,
the count of such codes is 126-32+1 = 95 not 93.

...

Quote:
> >   for (i = 0; i <= MAX ; i++)

< MAX (whatever it is) already caught by others

Quote:
> >   {
> >    fscanf(fptr, "%s", ASCII_letter[i]);
> If you want to read a character at a time, use %c as the format
> specification and provide the address of the char as the third
> parameter (&ASCII_letter[i] or ASCII_letter+i).

Almost; you can read a char with *scanf %c and the address
of a char variable, but ASCII_letter was declared as an array
(with bound MAX) of int.  Or you can call fgetc() or getc()
and assign the result (which is an int containing either
a character code in the range of unsigned char, or a
negative value given by standard macro EOF, usually -1,
but here won't be the latter if the input file is as expected).

Quote:
> >    fprintf(fptr, "%s", ASCII_letter[i]);
> To print one character at a time, use %c.

Or more simply use fputc() or putc().
(Note getc() and putc() may malfunction if the
file-pointer is given by a non-idempotent expression,
but here it is a simple variable which is fine.)

Quote:
> >    fprintf(fptr, "%d", ASCII_letter[i]);
> >   }
> >  }while ((letter = (fptr, fgetc(ASCII_letter[i]))) != EOF);
> Surely you mistyped this statement in your message.  It will not
> compile as written.  fgetc has the wrong number of parameters.  You
> should paste the code in your message to avoid these problems.

Actually it has the right number of parameters, one,
but of the wrong type (and very wrong value).  Moreover,
the OP probably doesn't want to read anything here,
but rather use the value already read in the code above.
Unless the input is one data character per line and this
is to discard the newline before the next data char.
In which case it is probably more important to check
the read above for EOF/input failure than this one.

Quote:
> fgetc returns an int, not a char, because EOF may not fit in a char.

Right.

--
- David.Thompson 1 now at worldnet.att.net



Sat, 11 Oct 2003 13:22:06 GMT  
 ERROR IN READING FILE : File Pointer
On Tue, 24 Apr 2001 05:22:06 GMT, "David Thompson"

snip

Sorry, must have been more brain dead than usual that night.

<<Remove the del for email>>



Sun, 12 Oct 2003 01:05:44 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. fatal error RC1121 : I/O error reading file

2. read/write structs, that use a pointer as a member, to a file (binary)

3. Pointer to read file...

4. error reading file

5. file read error

6. Blocking Error When Reading Text File

7. file read/write errors under unix/Solaris

8. Reading the rigstry error: There are no more files

9. Reading Text File error

10. Heap errors using FILE pointers

11. file pointer errors in a loop

12. read a file than produce 2 output files(HELP)

 

 
Powered by phpBB® Forum Software