summaryrefslogtreecommitdiff
path: root/659/CH12
diff options
context:
space:
mode:
Diffstat (limited to '659/CH12')
-rwxr-xr-x659/CH12/EX12.1/exm12_1.sce24
-rwxr-xr-x659/CH12/EX12.1/exm12_1_output.PNGbin0 -> 9136 bytes
-rwxr-xr-x659/CH12/EX12.2/exm12_2.sce61
-rwxr-xr-x659/CH12/EX12.2/exm12_2_output.PNGbin0 -> 16947 bytes
-rwxr-xr-x659/CH12/EX12.3/exm12_3.sce31
-rwxr-xr-x659/CH12/EX12.3/exm12_3_output.PNGbin0 -> 16480 bytes
-rwxr-xr-x659/CH12/EX12.4/exm12_4.sce38
-rwxr-xr-x659/CH12/EX12.4/exm12_4_output.PNGbin0 -> 12861 bytes
-rwxr-xr-x659/CH12/EX12.5/exm12_5.sce31
-rwxr-xr-x659/CH12/EX12.5/exm12_5_output.PNGbin0 -> 13751 bytes
-rwxr-xr-x659/CH12/EX12.6/exm12_6.sce46
-rwxr-xr-x659/CH12/EX12.6/exm12_6_output.PNGbin0 -> 25667 bytes
12 files changed, 231 insertions, 0 deletions
diff --git a/659/CH12/EX12.1/exm12_1.sce b/659/CH12/EX12.1/exm12_1.sce
new file mode 100755
index 000000000..c5ab08b6a
--- /dev/null
+++ b/659/CH12/EX12.1/exm12_1.sce
@@ -0,0 +1,24 @@
+// Example 12.1
+// Write a program to read data from keyboard,write it to a file called INPUT,
+// again read the same data from the INPUT file and display it on the screen.
+
+warning('off');
+disp("Data Input");
+
+//Open the file INPUT
+f1=mopen('INPUT.txt','w');
+// Get character from keyboard
+c=read(%io(1),1,1,'(a)');
+mfprintf(f1,'%s',c);
+
+//close the file input
+mclose(f1);
+
+
+disp("Data Output");
+//Reopen the file INPUT
+f1=mopen('INPUT.txt','r');
+ txt=mgetl(f1);
+ printf("%s",text);
+//close the file input
+mclose(f1);
diff --git a/659/CH12/EX12.1/exm12_1_output.PNG b/659/CH12/EX12.1/exm12_1_output.PNG
new file mode 100755
index 000000000..568022792
--- /dev/null
+++ b/659/CH12/EX12.1/exm12_1_output.PNG
Binary files differ
diff --git a/659/CH12/EX12.2/exm12_2.sce b/659/CH12/EX12.2/exm12_2.sce
new file mode 100755
index 000000000..7c8bad3ad
--- /dev/null
+++ b/659/CH12/EX12.2/exm12_2.sce
@@ -0,0 +1,61 @@
+// Example12.2
+//A file named DATA contains a series of integer numbers. Code a program
+//to read these numbers and then write all 'odd' numbers to a file to be
+//called ODD and all 'even' numbers to a file to be called EVEN.
+
+warning('off');
+//Input numbers in the DATA.txt file
+printf("Contents of DATA file\n");
+f1=mopen('DATA.txt','wt');
+for i=1:30
+ number(i)=scanf("%d");
+ if(number(i) == -1)
+ break;
+ end
+ mfprintf(f1,'%d\n',number(i));
+end
+mclose(f1);
+
+f2=mopen('ODD.txt','wt');
+f3=mopen('EVEN.txt','wt');
+f1=mopen('DATA.txt','rt');
+//Read numbers from DATA file
+EOF=length(number);
+i=1;
+even=0;
+odd=0;
+while (i<EOF)
+ [n,number]=mfscanf(f1," %d")
+ if(pmodulo(number,2)==0)
+ mfprintf(f3,'%d\n',number);
+ even=even+1;
+ else
+ mfprintf(f2,'%d\n',number);
+ odd=odd+1;
+ end
+ i=i+1;
+end
+mclose(f1);
+mclose(f2);
+mclose(f3);
+//Write odd numbers in the ODD.txt file
+f2=mopen('ODD.txt','rt');
+printf("\nContents of ODD file\n");
+i=1;
+while (i<=odd)
+ [n,number]=mfscanf(f2,"%d")
+ printf("%4d",number);
+ i=i+1;
+end
+//Write even numbers in the EVEN.txt file
+f3=mopen('EVEN.txt','rt');
+printf("\nContents of EVEN file\n");
+i=1;
+while (i<=even)
+ [n,number]=mfscanf(f3,"%d")
+ printf("%4d",number);
+ i=i+1;
+end
+//close the files
+mclose(f2);
+mclose(f3);
diff --git a/659/CH12/EX12.2/exm12_2_output.PNG b/659/CH12/EX12.2/exm12_2_output.PNG
new file mode 100755
index 000000000..a436e4600
--- /dev/null
+++ b/659/CH12/EX12.2/exm12_2_output.PNG
Binary files differ
diff --git a/659/CH12/EX12.3/exm12_3.sce b/659/CH12/EX12.3/exm12_3.sce
new file mode 100755
index 000000000..8475affee
--- /dev/null
+++ b/659/CH12/EX12.3/exm12_3.sce
@@ -0,0 +1,31 @@
+// Example12.3
+//A program to open a file named INVENTORY and store in it the following
+// data: Item name Number Price Quantity
+// AAA-1 111 17.50 115
+// BBB-2 125 36.00 75
+// C-3 247 31.75 104
+//Extend the program to read this data from the file INVENTORY and display
+//inventory table with the value of each item.
+
+disp("Input file name");
+filename=scanf("%s"); //Read file name that is 'INVENTORY'
+fp=mopen(filename,'w'); //Open file in write mode,fp is file descriptor
+disp("Input inventory data");
+disp("Item name Number Price Quantity");
+for i=1:3
+ //read data from terminal
+ [n,item(i),number(i),price(i),quantity(i)]=mscanf("%s %d %f %d");
+ //write data to the file
+ mfprintf(fp,'%s\t%d\t%.2f\t%d\n',item(i),number(i),price(i),quantity(i));
+end
+mclose(fp); //close the file
+fp=mopen(filename,'r'); //open file in read mode
+disp("Item name Number Price Quantity Value");
+for i=1:3
+ //Read data from the file 'INVENTORY'
+ [n,item,number,price,quantity]=mfscanf(fp,"%s %d %f %d");
+ value=price*quantity; //Computes value
+ //Printing of the data
+ printf(' %s %7d %8.2f %8d %11.2f\n',item,number,price,quantity,value);
+end
+mclose(fp);
diff --git a/659/CH12/EX12.3/exm12_3_output.PNG b/659/CH12/EX12.3/exm12_3_output.PNG
new file mode 100755
index 000000000..9a83c48e9
--- /dev/null
+++ b/659/CH12/EX12.3/exm12_3_output.PNG
Binary files differ
diff --git a/659/CH12/EX12.4/exm12_4.sce b/659/CH12/EX12.4/exm12_4.sce
new file mode 100755
index 000000000..14262ed8e
--- /dev/null
+++ b/659/CH12/EX12.4/exm12_4.sce
@@ -0,0 +1,38 @@
+// Example12.4
+//Write a program to illustatre error handling in file operations.
+
+warning('off');
+fp1=mopen('TEST','w'); //Open file in write mode,fp1 is file descriptor
+for i=10:10:100
+ //write data to the file
+ mfprintf(fp1,'%d\n',i);
+end
+mclose(fp1);
+disp("Input file name");
+filename='a';
+while(filename~=' ')
+ filename=scanf("%s");
+ //Error handling
+ try
+ fp2=mopen(filename,'r');
+ if(fp2>0) ,
+ break; //Terminates the loop if file exist or opened
+ end
+
+ catch
+ //Messages to be displayed when error occured
+ printf("Can not open file.\n");
+ printf("Type file name again.\n");
+ end
+end
+//Code below runs while there is no error
+for i=1:20
+ number = mfscanf(fp2,"%d"); //Read data from file 'TEST'
+ if meof(fp2) then //Test for end of file
+ printf("Ran out of data");
+ break;
+ else
+ printf("%d\n",number); //prints the data
+ end
+end
+mclose(fp2); \ No newline at end of file
diff --git a/659/CH12/EX12.4/exm12_4_output.PNG b/659/CH12/EX12.4/exm12_4_output.PNG
new file mode 100755
index 000000000..b948c5dac
--- /dev/null
+++ b/659/CH12/EX12.4/exm12_4_output.PNG
Binary files differ
diff --git a/659/CH12/EX12.5/exm12_5.sce b/659/CH12/EX12.5/exm12_5.sce
new file mode 100755
index 000000000..0f8b82e2a
--- /dev/null
+++ b/659/CH12/EX12.5/exm12_5.sce
@@ -0,0 +1,31 @@
+// Example12.5
+//Write a program that uses function ftell(mtell) and fseek(mseek).
+
+warning('off');
+//Open file 'RANDOM' in write mode,fp is file descriptor
+fp=mopen('RANDOM','w');
+c=read(%io(1),1,1,'(a)');
+mfprintf(fp,'%s',c); //write data to the file
+printf("Number of characters entered = %d\n",mtell(fp));
+mclose(fp);
+
+//Open file 'RANDOM' in read mode
+fp=mopen('RANDOM','r');
+n=0;
+while(meof(fp)==0)
+//n is the offset from origin in number of bytes.
+//The new position is at the signed distance given by n bytes from the beginning
+ mseek(n,fp,'set');
+ //Print the chracter and its postion
+ printf("Position of %c is %d\n",ascii(mget(1,'c',fp)),mtell(fp));
+ n=n+5;
+end
+n=0; //Initial offset
+while(mtell(fp)>1)
+ //New position is at the signed distance given by n bytes from the end
+ mseek(n,fp,'end');
+ //Print the characters from the end
+ printf("%c",(ascii(mget(1,'c',fp))));
+ n=n-1;
+end
+mclose(fp);
diff --git a/659/CH12/EX12.5/exm12_5_output.PNG b/659/CH12/EX12.5/exm12_5_output.PNG
new file mode 100755
index 000000000..bb468ccca
--- /dev/null
+++ b/659/CH12/EX12.5/exm12_5_output.PNG
Binary files differ
diff --git a/659/CH12/EX12.6/exm12_6.sce b/659/CH12/EX12.6/exm12_6.sce
new file mode 100755
index 000000000..17cf059fc
--- /dev/null
+++ b/659/CH12/EX12.6/exm12_6.sce
@@ -0,0 +1,46 @@
+// Example 12.6
+//Write a program to append additional items to the file INVENTORY
+//created in Example 12.3 and print the total contents of the file.
+funcprot(0);
+warning('off');
+function[item] =append(product,fp)
+ printf("Item name:\n");
+ product.name=scanf("%s");
+ printf("Item number:.\n");
+ product.number=scanf("%d");
+ printf("Item price\n");
+ product.price=scanf("%f");
+ printf("Quantity:\n");
+ product.quantity=scanf("%d");
+ //Write data in the file
+ mfprintf(fp,'%s %d %.2f %d\n',product.name,product.number,product.price,product.quantity);
+ item=product;
+endfunction
+//Creating structure
+item=struct('name','0','number','0','price','0','quantity','0');
+//Read file name that is 'INVENTORY'
+disp("Type file name");
+filename=scanf("%s");
+//Open file in append mode,fp is file descriptor
+fp=mopen(filename,'a+');
+b=0;response=-1;
+//Read data
+while(response==1|b==0)
+ item=append(item,fp); //calling append() function
+ printf("Item %s appended.\n",item.name);
+ printf("Do you want to add another item\(1 for YES/0 for NO)?");
+ response=scanf("%d");
+ b=1;
+end
+n=mtell(fp); //position of last character
+mclose(fp);
+
+//Opening file in the read mode
+fp=mopen(filename,'r');
+while (mtell(fp) < n-2)
+ //read data from terminal
+ [g,item.name,item.number,item.price,item.quantity]=mfscanf(fp,"%s %d %f %d");
+ //Print Data to screen
+ printf('%s %7d %8.2f %8d\n',item.name,item.number,item.price,item.quantity);
+end
+mclose(fp);
diff --git a/659/CH12/EX12.6/exm12_6_output.PNG b/659/CH12/EX12.6/exm12_6_output.PNG
new file mode 100755
index 000000000..2ff06cfea
--- /dev/null
+++ b/659/CH12/EX12.6/exm12_6_output.PNG
Binary files differ