edit Found out my problem. I didn't realize the only allowable values for the origin parameter of fseek are SEEK_SET SEEK_CUR and SEEK_END, so i changed my call to fseek(diskPointer, startOfData, SEEK_SET);
I have a file open for reading and writing in binary mode, so I am using fseek, fread and fwrite on the file. Before my calls to fwrite throughout the code, I use fseek to set the file pointer to the right position, but it seems fseek is failing and returning a non-zero value. What reasons might there be for fseek failing?
here is my function which should write 'value' to the file. The value is being written, but at the start of the file rather than the position I would like fseek to get me to. 'startOfData' is a constant that indicates the sector i'm trying to write into.
int writeToData(int position, char *value){
if(!fseek(diskPointer, 0, startOfData)){
if (fwrite(value, sizeof(value), 1, diskPointer) != 1) {
return 0;
}
return 1;
}
return 0;
}
Here is main (the writeToData function above is called by the createroot function below):
#define startOfData 405504
int main(int argc, char *argv[]){
char *filepath;
int clearDisk;
if (argc != 3) {
printf("Usage: ./a.out [filepath of virtual disk] [0 to clear disk before use, any other number otherwise]\n");
}
else {
filepath = argv[1];
clearDisk = atoi(argv[2]);
}
//The virtual disk is opened for reading and writing
diskPointer = fopen(filepath, "r+b");
if (diskPointer != NULL)
{
if (clearDisk == 0) {
initializeDisk();
createRoot();
}
}
else
{
printf("Error preparing virtual disk; terminating program.\n");
return(1);
}
my initialize disk function:
//5MB disk
void initializeDisk(){
int i;
char *buffer = "0";
for (i = 0; i < 5000000; i++) {
fwrite(buffer, 1, 1, diskPointer);
}
rewind(diskPointer);
}