Jump to content
  • Advertisement
Sign in to follow this  
ace84_84

C Help

This topic is 4627 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!