Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Programming in C String processing without using <string.h> int my_fgetc (FILE *

ID: 3828611 • Letter: P

Question

Programming in C

String processing without using <string.h>

int my_fgetc (FILE *stream) - Reads the next character from the input pointed to by stream. If the input is at the end of file or a processing error occurs return MY_EOF; otherwise return the integer representation of the character read.

int my_fputc (int c, FILE *stream) - Writes the character c (converted to a character) to the output specified by stream. If the write is successful the ascii value of the character is returned; otherwise MY_EOF is returned.

These two I am stuck on. Please help.

Explanation / Answer

#include <stdio.h>
#define MY_EOF -1
/*int my_fgetc (FILE *stream) - Reads the next character from the input pointed to by stream.
If the input is at the end of file or a processing error occurs return MY_EOF;
otherwise return the integer representation of the character read.*/
int my_fgetc (FILE *stream)
{
    if(feof(stream))
        return MY_EOF;
    char ch;      
    fscanf(stream, "%c", &ch);
    return (int)ch;
}
/*int my_fputc (int c, FILE *stream) - Writes the character c (converted to a character)
to the output specified by stream. If the write is successful the ascii value of the
character is returned; otherwise MY_EOF is returned.*/
int my_fputc (int c, FILE *stream)
{
    int x = fprintf(stream, "%c", c);
    if(x == 0)
        return MY_EOF;
    return c;  
}