3. File Handling
# include <stdio.h>
int main()
{
FILE *fp; // you can select any valid name of file.
int x = 10;
fp = fopen("File1.txt","w");
if (fp == NULL)
printf ("There is an error");
else
{
fprintf(fp, "%d", x);
printf("Data has been stored in the file");
fclose(fp);
}
getchar(); return 0; }
Dr. Yousaf, PIEAS
4. Modes for opening files
• The second argument of fopen is the mode in
which we open the file. There are three
• "r" opens a file for reading
• "w" opens a file for writing - and writes over all
previous contents (deletes the file so be careful!)
• "a" opens a file for appending - writing on the end of
the file
Dr. Yousaf, PIEAS
5. #include<stdio.h>
int main()
{
int i,j;
float x, y[1000];
i = 0;
for (x = 0; x<=10; x = x+0.1)
{
y[i] = x;
printf("%fn", y[i]);
i++;
}
getchar(); return 0;
}
/* We need to store y points in a file. So look at the following
program*/
Dr. Yousaf, PIEAS
6. // Graph of y versus x
#include<stdio.h>
int main()
{
int i;
float x, y[101];
FILE *fpx, *fpy;
fpx = fopen("x.txt","w");
fpy = fopen("y.txt","w");
i = 0;
for (x = 0; x<=10; x = x+0.1)
{
y[i] = x;
fprintf(fpx, "%fn", x);
fprintf(fpy, "%fn", y[i]);
i++;
}
printf("Data has been stored in the file");
getchar(); return 0; }
Dr. Yousaf, PIEAS
7. %%% Matlab Code
x = load('x.txt');
y = load('y.txt');
figure(1)
plot(x,y)
Dr. Yousaf, PIEAS
8. Y Versus X
Dr. Yousaf, PIEAS
0 2 4 6 8 10
0
1
2
3
4
5
6
7
8
9
10
X
Y
9. // Graph of y versus x^2
#include<stdio.h>
#include<math.h>
int main()
{
int i;
double x, y[101];
FILE *fpx, *fpy;
fpx = fopen("xs.txt","w");
fpy = fopen("ys.txt","w");
i = 0;
for (x = 0; x<=10; x = x+0.1)
{
y[i] = pow(x,2);
fprintf(fpx, "%lfn", x);
fprintf(fpy, "%lfn", y[i]);
i++;
}
printf("Data has been stored in the file");
getchar(); return 0; }
Dr. Yousaf, PIEAS
10. %%% Matlab Code
x = load('xs.txt');
y = load('ys.txt');
figure(1)
plot(x,y)
Dr. Yousaf, PIEAS
11. Dr. Yousaf, PIEAS
0 2 4 6 8 10
0
10
20
30
40
50
60
70
80
90
100
X
Y
Y Versus X2
12. /* Graph of sine(theta)
/* Graph of sine(theta)
Please read Page 378 for many
trigonometric functions*/
#include<stdio.h>
#include<math.h>
# define PI 22.0/7.0
int main()
{
double theta_deg, theta_rad, y[2000];
int i = 0;
FILE *fptheta, *fpsine;
fptheta = fopen("theta.txt","w");
fpsine = fopen("sine.txt","w");
for (theta_deg = 0;
theta_deg<=720; theta_deg++)
{
theta_rad =
((PI)/180.0)*theta_deg;
y[i] = sin(theta_rad);
fprintf(fptheta, "%lfn",
theta_deg);
fprintf(fpsine, "%lfn", y[i]);
i++;
}
printf("Data has been stored in
the file");
getchar(); return 0; }
Dr. Yousaf, PIEAS
13. %%%%Matlab code
x = load('theta.txt');
y = load('sine.txt');
figure(1)
plot(x,y)
Dr. Yousaf, PIEAS
16. //Linear Search
#include <stdio.h>
int main()
{
int array[100], search, c, n;
printf("Enter the number of elements in
arrayn");
scanf("%d",&n);
printf("Enter %d integer(s)n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("Enter the number to searchn");
scanf("%d", &search);
for (c = 0; c < n; c++)
{
if (array[c] == search)
{
printf("%d is present at location
%d.n", search, c+1);
break;
}
}
if (c == n)
printf("%d is not present in
array.n", search);
getchar();
return 0;
}
Dr. Yousaf, PIEAS
17. /*Binary Search (only for arrays which
are already sorted). */
#include <stdio.h>
int main()
{
int c, first, last, middle, n, search,
array[100];
printf("Enter number of elementsn");
scanf("%d",&n);
printf("Enter %d integers in asending
ordern", n);
for ( c = 0 ; c < n ; c++ )
scanf("%d",&array[c]);
printf("Enter value to findn");
scanf("%d",&search);
first = 0;
last = n - 1;
middle = (first+last)/2;
while( first <= last )
{
if ( array[middle] == search )
{
printf("%d found at location
%d.n", search, middle+1);
break;
}
else if ( array[middle] < search )
first = middle + 1;
else
last = middle - 1;
middle = (first + last)/2;
}
if ( first > last )
printf("Not found! %d is not present
in the list.n", search);
return 0;
}
Dr. Yousaf, PIEAS
21. //Selection Sort
#include <stdio.h>
int main()
{ int array[100], n, c, d, position, swap;
printf("Enter number of elementsn");
scanf("%d", &n);
printf("Enter %d integersn", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
for (c = 0; c < (n - 1); c++)
{ position = c;
for (d = c + 1; d < n; d++)
{ if (array[position] > array[d])
position = d;
}
if (position != c)
{ swap = array[c];
array[c] = array[position];
array[position] = swap; }
}
//Selection Sort
printf("Sorted list in ascending
order:n");
for (c = 0; c < n; c++)
printf("%dn", array[c]);
getchar();
getchar();
return 0;
}
Dr. Yousaf, PIEAS