• Advertisement
Sign in to follow this  

writing random access

This topic is 4153 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;
	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;

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

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

	int fileSize = -1;
char *dataBuffer = new char [fileSize];

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

return true;

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement