Final Exam C Program

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

Final Exam C Program

I. File :

 Library

 Example :

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

 Structure

 Example :

struct product{

int code;

char name[20];

float price;

};

 Write

 Example :

void write(char *name){

FILE *fp;

product a[100];int n;

fp=fopen(name,"wb");

if(fp==NULL){

printf("Can not open file:");

return;

printf("Number of product:");scanf("%d",&n);
for(int i=0;i<n;i++){

printf("code:");scanf("%d",&a[i].code);

printf("name:");fflush(stdin);gets(a[i].name);

printf("price:");scanf("%f",&a[i].price);

fwrite(a,sizeof(product),n,fp);

fclose(fp);

 Read

 Example :

void read(char *name){

FILE *fp;

product a[100];int n;

fp=fopen(name,"rb");

if(fp==NULL){

printf("Can not open file:");

return;

fseek (fp,0,SEEK_END);

n=ftell(fp)/sizeof(product);

rewind(fp);

fread(a,sizeof(product),n,fp);

fclose(fp);

for(int i=0;i<n;i++)

printf("%d\t%s\t%.2f\n",a[i].code,a[i].name,a[i].price);

}
 Append

 Example:

void append(char *name){

FILE *fp;

product a[100];int n;

fp=fopen(name,"ab");

if(fp==NULL){

printf("Can not open file:");

return;

printf("Number of product:");scanf("%d",&n);

for(int i=0;i<n;i++){

printf("code:");scanf("%d",&a[i].code);

printf("name:");fflush(stdin);gets(a[i].name);

printf("price:");scanf("%f",&a[i].price);

fwrite(a,sizeof(product),n,fp);

fclose(fp);

 Update

 Example:

void update(char *name){

FILE *fp;

product a[100],x;int n,c,pos;

fp=fopen(name,"rb");
if(fp==NULL){

printf("Can not open file:");

return;

fseek (fp,0,SEEK_END);

n=ftell(fp)/sizeof(product);

rewind(fp);

fread(a,sizeof(product),n,fp);

fclose(fp);

printf("Code to update:");scanf("%d",&c);

for(int i=0;i<n;i++)

if(a[i].code==c){pos=i;break;}

printf("New product:\n");

printf("New code:");scanf("%d",&x.code);

printf("New name:");fflush(stdin);gets(x.name);

printf("New price:");scanf("%f",&x.price);

a[pos]=x;

fp=fopen(name,"wb");

if(fp==NULL){

printf("Can not open file:");

return;

fwrite(a,sizeof(product),n,fp);

fclose(fp);
}

 Remove

 Example

void remove(char *name){

FILE *fp;

product a[100];int n,c;

fp=fopen(name,"rb");

if(fp==NULL){

printf("Can not open file:");

return;

fseek (fp,0,SEEK_END);

n=ftell(fp)/sizeof(product);

rewind(fp);

fread(a,sizeof(product),n,fp);

fclose(fp);

fp=fopen(name,"wb");

if(fp==NULL){

printf("Can not open file:");

return;

printf("Code to remove:");scanf("%d",&c);

for(int i=0;i<n;i++)

if(a[i].code!=c)fwrite(&a[i],sizeof(product),1,fp);

fclose(fp);
}

 Main :

 Example :

void main(){

char name[20],select;

printf("write(w),read(r),append(a),remove(v),update(u):\n");

scanf("%c",&select);

switch(select){

case 'w':printf("File name:");fflush(stdin);gets(name);

write(name);break;

case 'r':printf("File name:");fflush(stdin);gets(name);

read(name);break;

case 'a':printf("File name:");fflush(stdin);gets(name);

append(name);break;

case 'u':printf("File name:");fflush(stdin);gets(name);

update(name);break;

case 'v':printf("File name:");fflush(stdin);gets(name);

remove(name);break;

getch();

}
II. ប្រៀបធៀបលក្ខណៈខុសគ្នា ៖

 File and Array :

Feature File Array


Location Disk Memory
Data types Any type Single type
Size Variable Fixed
Access Sequential Direct
Operations Open, close, read, write, append Read, write

 Text File and Binary File :

Feature Text File Binary File


Data
representation ASCII or Unicode characters Binary format
Line endings Uses newline characters (\n) Does not use newline characters
Encoding Can be encoded in different ways Does not have an encoding
Compression Can be compressed to save space Cannot be compressed without losing data
Compatible with a wide range of May only be compatible with a specific
Compatibility applications application
III. Structure :

You might also like