stackoverflow1453

Programming&&Games&&Life

Archive for March, 2014

Write a program which updates existing cars, adds new cars, deletes cars and stores a listing of all the current cars in a text file for printing.

/*

The program updates existing cars, adds new cars, deletes cars and stores a listing of all the current cars
in a text file for printing. 

*/
#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>

void addRecord(FILE *);//ok
void showRecords(FILE *); //ok
void createDatabase(FILE *); //ok
void updateRecord(FILE *);//ok
void deleteRecord(FILE *);
void menu(FILE *); //ok

int i;

struct carData{

	int carNo;
	char carBrand[15];
	double carPrice;

};

struct carData blankEntry = { 0, "", 0 };
struct carData entry = { 0, "", 0 };
char choice = 'a';

FILE *filePointer;

int main(){

	FILE *fPtr;

	fPtr = fopen("CARDATABASE.dat", "rb+");
	//ask user what to do

	createDatabase(fPtr);
	menu(fPtr);

}


//DELETES RECORDS
void deleteRecord(FILE *filePointer){

	int del = 10000;
	int valueFound = 0;
	int amount = 0;

	struct carData emptyEntry = { 0, "", 0 };

	filePointer = fopen("CARDATABASE.dat", "rb+");
	//ask user which record he wants to delete

	printf("Enter record number that you want to delete:");
	scanf("%d", &del);

	//check if that record exists
	rewind(filePointer);

	fseek(filePointer, (del - 1)*sizeof(struct carData), SEEK_SET);

	fread(&blankEntry, sizeof(struct carData), 1, filePointer);

	if (blankEntry.carNo == 0)
	{
		printf("This record does not exist!!!!\n\n");

	}

	else
	{

		fseek(filePointer, (del - 1)*sizeof(struct carData), SEEK_SET);
		fwrite(&emptyEntry, sizeof(struct carData), 1, filePointer);

		printf("Entry deleted.");
	}

	fclose(filePointer);
}

//UPDATES RECORDS
void updateRecord(FILE *filePointer){

	int rec = 10000;
	int valueFound = 0;
	int amount = 0;

	filePointer = fopen("CARDATABASE.dat", "rb+");
	//ask user which record he wants to update

	printf("Enter record number that you want to update:");
	scanf("%d", &rec);

	//check if that record exists
	rewind(filePointer);

	fseek(filePointer, (rec - 1)*sizeof(struct carData), SEEK_SET);

	fread(&blankEntry, sizeof(struct carData), 1, filePointer);

	if (blankEntry.carNo == 0)
	{
		printf("This record does not exist!!!!\n\n");

	}

	else
	{
		printf("Enter transaction amount:\n");
		scanf("%d", &amount);

		blankEntry.carPrice = blankEntry.carPrice + amount;

		fseek(filePointer, (rec - 1)*sizeof(struct carData), SEEK_SET);
		fwrite(&blankEntry, sizeof(struct carData), 1, filePointer);
	}

	fclose(filePointer);
}

//CREATES DEFAULT DATABASE
void createDatabase(FILE *filePointer){

	int i;
	//fill it
	for (i = 0; i < 100; i++)
	{
		fwrite(&blankEntry, sizeof(struct carData), 1, filePointer);
	}

	printf("file is created\n");
	fclose(filePointer);

}

//READS RECORDS
void showRecords(FILE *filePointer){

	filePointer = fopen("CARDATABASE.dat", "r");

	while (!feof(filePointer)){

		fread(&blankEntry, sizeof(struct carData), 1, filePointer);

		//Show only inputted data
		if (blankEntry.carNo != 0)
		{
			printf("%d %s %10.2f\n", blankEntry.carNo, blankEntry.carBrand, blankEntry.carPrice);

		}

	}

}

//ADD RECORDS
void addRecord(FILE *filePointer){

	filePointer = fopen("CARDATABASE.dat", "rb+");
	printf("Enter car no:");
	scanf("%d", &blankEntry.carNo);
	printf("Enter carBrand and carPrice: \n");
	//fscanf(stdin, "%s %.2f", &blankEntry.carBrand, &blankEntry.carPrice);   
	scanf("%s %lf", &blankEntry.carBrand, &blankEntry.carPrice);
	fseek(filePointer, (blankEntry.carNo - 1)*sizeof(struct carData), SEEK_SET);
	fwrite(&blankEntry, sizeof(struct carData), 1, filePointer);
	fclose(filePointer);

}

void menu(FILE *filePointer){

	while (choice != 0)
	{
		printf("What do you want to do?\n");

		puts("1- Add new car\n");
		puts("2- Update existing car\n");
		puts("3- Delete car\n");
		puts("4- Print all cars\n");
		puts("Press 0 for exit\n?");

		scanf("%c", &choice);

		//choices

		switch (choice)
		{

		case '0':
			printf("Nothing to do? Ok. bye");
			exit(0);
			break;

		case '1':
			addRecord(filePointer);
			break;

		case '2':
			updateRecord(filePointer);
			break;

		case '3':
			deleteRecord(filePointer);

		case '4':
			showRecords(filePointer);
			break;

		default:
			printf("You did not choose anything. Bye\n");
			break;
		}
	}

}

Create, fill and read a random access file.

/*

create,fill and read a random access file

*/
#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>

struct carData{

	int carNo;
	char carBrand[15];
	double carPrice;

};

int main(void){

	int i;

	FILE *filePtr;

	struct carData blankEntry = { 0, "a", 21 };

	//creation block

	filePtr = fopen("CARDATABASE.dat", "wb");

	for (i = 0; i < 100; i++)
	{
		fwrite(&blankEntry, sizeof(struct carData), 1, filePtr);
	}

	fclose(filePtr);

	//manually fill records

	filePtr = fopen("CARDATABASE.dat", "rb+");
	printf("Enter car no: ");
	scanf("%d", &blankEntry.carNo);

	while (blankEntry.carNo != 0){

		printf("Enter carBrand and carPrice: \n");
		//fscanf(stdin, "%s %f", &blankEntry.carBrand, &blankEntry.carPrice);   
		scanf("%s %f", &blankEntry.carBrand, &blankEntry.carPrice);
		fseek(filePtr, (blankEntry.carNo - 1)*sizeof(struct carData), SEEK_SET);
		fwrite(&blankEntry, sizeof(struct carData), 1, filePtr);
		printf("Enter car no: ");
		scanf("%d", &blankEntry.carNo);

	}

	fclose(filePtr);

	//read block

	filePtr = fopen("CARDATABASE.dat", "rb");
	while (!feof(filePtr)){
		fread(&blankEntry, sizeof(struct carData), 1, filePtr);

		//Show only inputted data
		if (blankEntry.carNo != 0)
		{
			printf("%d %s %f\n", blankEntry.carNo, blankEntry.carBrand, blankEntry.carPrice);

		}

	}

	fclose(filePtr);

	getch();
}

How to read from sequential file

//How to read a file 

#include <stdio.h>

int main(void){
	
	char arabaMarkası[30];
	int arabaFiyat;
	
	FILE *fPtr;
	if ((fPtr = fopen("database.txt", "r")) == NULL) 
	{
		printf("Dosya Okunamadi");
	}
	else
	{
		printf("Marka    Fiyat\n");
		fscanf(fPtr, "%s %d", arabaMarkası, &arabaFiyat);
		
		while (!feof(fPtr)){
			printf("%s %d\n", arabaMarkası, arabaFiyat);
			fscanf(fPtr, "%s %d", arabaMarkası, &arabaFiyat);
							
		}

		fclose(fPtr);
	}
	
	getch();

	return 0;
}