stackoverflow1453

Programming&&Games&&Life

Archive for December, 2013

Create a sequential file and store client info

//CREATING A SEQUENTIAL FILE

#include <stdio.h>

int main(){

	int accountNo;
	char name[10];
	double balance;

	FILE *fPtr;

	if ((fPtr = fopen("clients.bin", "a+")) == NULL)
	{
		puts("file can not be created!");
	}
	else
	{
		puts("Enter Account No, Name and Balance of client: ");
		scanf("%d %s %lf", &accountNo, name, &balance);
	}
	while (!feof(stdin))
	{
		puts("Enter Account No, Name and Balance of client: ");
		fprintf(fPtr, "%d %s %.2f\n", accountNo, name, balance);
		scanf("%d %s %lf", &accountNo, name, &balance);
	}

	fclose(fPtr);
	getch();

}

Write a code to show the usage of fgets fputs putchar fputc getchar fgetc

//program to show usage of file input output

#include <stdio.h>

int main(){

	char buffer[50];
	//getchar fgetc example

	getchar();

	fgetc(stdin);

	//putchar fputc example
	putchar('a');

	fputc('a', stdout);

	//fgets fputs example

	fgets(buffer, 50, stdin);

	fputs(buffer, stdout);

	getch();
	return 0;
}

Car data to file using scanf() and fprintf()

/*

Name:
Copyright: Deitel C How to Program
Author: StackOverflow1453
Date: 12/16/2013 3:46:35 PM
Description: Car data to file using scanf() and fprintf()

*/
#include <stdio.h>

int main(void){

	char carBrand[30];
	int maxSpeed;

	FILE *fPtr;

	fPtr=fopen("cardatabase.bin", "w");
	printf("Enter brand and Maxspeed:\n");
	scanf("%s %d", carBrand, &maxSpeed);

	while(!feof(stdin)){

		fprintf(fPtr, "%s %d\n", carBrand, maxSpeed);
		scanf("%s %d", carBrand, &maxSpeed);

	}

	getch();

	return 0;

}

How to use fwrite()?

/*

Name:
Copyright: 
Author: StackOverflow1453
Date: 12/11/2013 11:20:57 AM
Description: How to use fwrite()?

*/
#include <stdio.h>

int main(void){

	char myArray[]={'x','y','z','m','k','p'};

	FILE *pFile;
	pFile=fopen("myFile.txt","w");

	fwrite(myArray, sizeof(char), sizeof(myArray), pFile);

	fclose(pFile);

	return 0;

}

/*
size_t fwrite ( const void * ptr, size_t size, size_t count, FILE * stream );
Write block of data to stream
Writes an array of count elements, each one with a size of size bytes, from the block of memory pointed by ptr to the current position in the stream.

The position indicator of the stream is advanced by the total number of bytes written.

*/

How to copy a file using fprintf() and fscanf()?

/*

Name:
Copyright: 
Author: StackOverflow1453
Date: 12/10/2013 11:16:46 AM
Description: How to copy a file using fprintf and fscanf? 

*/
#include <stdio.h>

int main(void){

	int i;
	int a;
	char b[10];
	char *ptrMonths[] ={"January","February","Mach","April","May","June", "July","August","September","October","November","December"};
	FILE *ptrToMonthFile, *ptrToMonthCopyFile;

	//open in write mode
	ptrToMonthFile=fopen("Monthsrr.txt", "w");

	/*
	fill the file
	*/
	for (i = 0; i < 12; i++)
	{
		fprintf(ptrToMonthFile, "%d %s\n", i+1, ptrMonths[i]);
	}
	//close the file
	fclose(ptrToMonthFile);

	//open the file to be copied in read mode
	//open the file copied to in write mode
	ptrToMonthFile=fopen("Monthsrr.txt", "r");
	ptrToMonthCopyFile=fopen("MonthsrrCopy.txt", "w");

	/*
	Instead of writing into a file, 
	below code writes the contents of a file to an integer variable
	and to a char array by using fscanf.
	*/

	for (i = 0; i < 12; i++)
	{		
		fscanf(ptrToMonthFile,"%d %s\n", &a, b);
		fprintf(ptrToMonthCopyFile,"%d %s\n", a, b);
	}

	fclose(ptrToMonthFile);
	fclose(ptrToMonthCopyFile);
	puts("Copied");
	getch();
}

How to use fprintf() and fscanf()?

/*

Name:
Copyright: 
Author: StackOverflow1453
Date: 12/10/2013 11:16:46 AM
Description: How to use fprintf and fscanf? 

*/
#include <stdio.h>

int main(void){

	int i;
	int a;
	char b[10];
	char *ptrMonths[] ={"January","February","Mach","April","May","June", "July","August","September","October","November","December"};
	FILE *ptrToMonthFile;
	ptrToMonthFile=fopen("Monthsrr.txt", "w");

	/*
	This code fills the file with months and month number in front of it
	*/
	for (i = 0; i < 12; i++)
	{
		fprintf(ptrToMonthFile, "%d %s\n", i+1, ptrMonths[i]);
	}

	fclose(ptrToMonthFile);

	ptrToMonthFile=fopen("Monthsrr.txt", "r");

	/*
	Instead of writing into a file, 
	below code writes the contents of a file to an integer variable
	and to a char array by using fscanf.
	*/

	for (i = 0; i < 12; i++)
	{		
		fscanf(ptrToMonthFile,"%d %s\n", &a, b);
		fprintf(stdout, "%d %s\n", a, b);
	}

	fclose(ptrToMonthFile);

	getch();
}

How to use fprintf()?

/*

Name:
Copyright: 
Author: StackOverflow1453
Date: 12/10/2013 11:16:46 AM
Description: How to use fprintf? 

*/
#include <stdio.h>

int main(void){

	int i;

	char *ptrMonths[] ={"January","February","Mach","April","May","June", "July","August","September","October","November","December"};
	FILE *ptrToMonthFile;
	ptrToMonthFile=fopen("Monthsrr.txt", "w");

	for (i = 0; i < 12; i++)
	{
		fprintf(ptrToMonthFile, "%2d  %s \n", i+1, ptrMonths[i]);

	}

}