I can't seem to work out how to convert from an fopen to open. I don't have much c experience, so this is quite overwhelming for me.
Here is what it is on its own:
In the cache_reader.c file (just the open and close functions):
void cr_close(cr_file* f){
free(f->buffer);
fclose(f->file);
}
cr_file* cr_open(char * filename, int buffersize)
{
FILE* f;
if ((f = fopen(filename, "r")) == NULL){
fprintf(stderr, "Cannot open %s\n", filename);
return 0; }
cr_file* a=(cr_file*)malloc(sizeof(cr_file));
a->file=f;
a->bufferlength=buffersize;
a->usedbuffer=buffersize;
a->buffer=(char*)malloc(sizeof(char)*buffersize);
refill(a);
return a;
}
In the cache_reader.h file:
typedef struct{
FILE* file; //File being read
int bufferlength; //Fixed buffer length
int usedbuffer; //Current point in the buffer
char* buffer; //A pointer to a piece of memory
// same length as "bufferlength"
} cr_file;
//Open a file with a given size of buffer to cache with
cr_file* cr_open(char* filename, int buffersize);
//Close an open file
void cr_close(cr_file* f);
int refill(cr_file* buff);
In the cache_example.c file:
int main(){
char c;
//Open a file
cr_file* f = cr_open("text",20);
//While there are useful bytes coming from it
while((c=cr_read_byte(f))!=EOF)
//Print them
printf("%c",c);
//Then close the file
cr_close(f);
//And finish
return 0;
}
I know that I need to change fclose to close, fopen to open. But I do not understand most of the other stuff. I am getting a ton of errors for everything, and I am not sure how this works out with pointers, as I barely have any experience with them. I tried to use int fileno(FILE *stream), by saying int fd = fileno(FILE *f) and then fd = fopen(filename, "r")) == NULL). This didn't work, and all examples of the open function I could find just use the name of the file, rather than a pointer of chars to specify filename... I thought the cr_close function can be done by just changing fclose to close, but this doesn't work either. I am not sure if I need to edit cache_example.c file as well.
Could anyone provide some help to put me on the right path to do this..?