Jump to content
  • Advertisement
Sign in to follow this  
pascalosti

writing random access

This topic is 4276 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
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!