Sign in to follow this  

C Help

Recommended Posts

ace84_84    151
I Have A Problem, What i need to do is in a simple to mimic the fgetc() function, but i dont know how to do it. These Are My Requirements - allocate and initialize the file stream buffer. You must use dynamic memory allocation and a predefined default buffer size of BUFFERSIZE. - load/reload the stream buffer when needed. Each read must read a buffer-full of data if available with system call read. - return one charactor at a time from the stream buffer. - return EOF when the end of end is reached. This Is What I Have Already #include <stdio.h> #include <stdlib.h> #include <unistd.h> #define BUFFERSIZE 256 //Prototype int myGetc (FILE *); int main(int argc, char *argv[]) { FILE *fp; int cnt, ch; if(argc != 2) { fprintf(stderr, "%s requires 1 argument.\n", argv[0]); exit(1); } if((fp = fopen(argv[1], "r")) == NULL) { perror("File Problem"); exit(1); } while( (ch = myGetc(fp)) != EOF) { putchar(ch); cnt++; } printf("The File Size Is %d\n", cnt); }//end main int myGetc(FILE *fp) { } Just A Little Help Please !!!!

Share this post

Link to post
Share on other sites
KulSeran    3267
do something like:

char buffer[BUFFERSIZE];
size_t buff_size=0;
size_t buff_pos=0;

char myGetc(FILE *fp)
if ( buff_size == 0 || buff_pos == buff_size )
{ //refill the buffer ( set the buff_size to the actual # of bytes that were read )
buff_pos = 0;
return ( buffer[buff_pos++] );

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this