Sign in to follow this  

writing random access

This topic is 4058 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

trying to write to random access file, I dont get errors but i dont believe its writing either.
// the struct im using
struct Vehicle
{
	int VIN;
	int Data;
	Vehicle* prev;
	Vehicle* next;
};



// writing to the file
bool WriteRantest(FILE *cfPtr){
	Vehicle* temp = head;

	if((cfPtr = fopen( "ranfile.txt", "wb")) == NULL){ // rb+ open for reading and writing
	printf("file could not be opened!\n");
	return false;
	}
else{
	while(temp->next) {
		
	fseek(cfPtr, (temp->VIN -1) * sizeof(struct Vehicle), SEEK_SET);
	// i dont believe i need this
	//fprintf(cfPtr,"%d %d\n", temp->VIN, temp->Data);
        // is this supposed to be just temp? or you have vin and data?
	fwrite(&temp->VIN, sizeof(struct Vehicle), 1, cfPtr);
			printf("Writing to ranfile.txt: \n");
// move on to the next link 
			temp = temp->next;
	
	}// end while
}// end else
	


	fclose(cfPtr);// close file
	return true;
}// end write seq

// reading from the file
// Load binary file, into linked list
bool LoadRan(FILE *cfPtr){
	int VIN = 0; 
	int Data = 0;
Vehicle *temp = NULL;
++counter;

Destroy();// delete current stuff and load ran file
	if((cfPtr = fopen("ranfile.txt", "rb")) == NULL){// rb = open file read in binary
					printf("File could not be opened.\n");
					return false;
					}// end if

else{
	printf("%-10s%-10s\n" , "VIN", "Data");
// i dont believe i need this
	//fscanf(cfPtr,"%d", &temp->VIN);

	int fileSize = -1;
while(!feof(cfPtr)){
	fgetc(cfPtr);
	fileSize++;}
rewind(cfPtr);
char *dataBuffer = new char [fileSize];

	while(!feof(cfPtr)){
		fread(&dataBuffer, fileSize, 1, cfPtr);
			fscanf(cfPtr,"%d%d", VIN, Data);
			printf( " %-8d%-8d\n", VIN, Data);
			PushFrontforFile(VIN, Data);
		
	}// end while
}// end else
fclose(cfPtr);

return true;
}

Share this post


Link to post
Share on other sites

This topic is 4058 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.

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