Cprograms
Cprograms
Cprograms
#include <stdio.h>
#define N 16
int main(void) {
int n; /* The current exponent */
int val = 1; /* The current power of 2
*/
printf("\t n \t 2^n\n");
printf("\t================\n");
for (n=0; n<=N; n++) {
printf("\t%3d \t %6d\n", n, val);
val = 2*val;
}
return 0;
}
/* It prints out :
n 2^n
================
0 1
1 2
2 4
3 8
4 16
5 32
6 64
7 128
8 256
9 512
10 1024
11 2048
12 4096
13 8192
14 16384
15 32768
16 65536
*/
/* homework1.c -- This is how the code for the
first homework
* appears when we have a single
block letter.
* In our Unix system you can
compile, link,
* load, and run this program
with the commands
* % cc homework1.c
* % a.out
*/
#include <stdio.h>
/* It prints out:
gggggg
g g
g
g ggg
g g
gggggg
*/
* homework1.c -- This is how the code for the
first homework
* appears when we have a single
block letter.
* In our Unix system you can
compile, link,
* load, and run this program
with the commands
* % cc homework1.c
* % a.out
*/
#include <stdio.h>
/* It prints out:
gggggg
g g
g
g ggg
g g
gggggg
*/
/* add2.c -- Add two numbers and print them out
together
with their sum
AUTHOR:
DATE:
*/
#include <stdio.h>
int main(void) {
int first, second;
#include <stdio.h>
int main(void) {
int n; /* The number of numbers to be
read */
int sum; /* The sum of numbers already
read */
int current; /* The number just read
*/
int lcv; /* Loop control variable, it
counts the number
of numbers already read */
#include <stdio.h>
void main ()
{
// Local data ...
int pennies; // input: count of
pennies
int nickels; // input: count of
nickels
int dimes; // input: count of
dimes
int quarters; // input: count of
quarters
int temp, left; // temporaries for
various
// computations
int main(void) {
int current;
#include <stdio.h>
int main(void) {
int n;
int i;
int flag;
if (flag)
printf("%d is prime\n", n);
else
printf("%d has %d as a factor\n", n, i);
}
/* factor1.c -- It prompts the user to enter an
integer N. It prints out
* if it is a prime or not. If not, it
prints out all of its
* proper factors.
*/
#include <stdio.h>
int main(void) {
int n,
lcv,
flag; /* flag initially is 1 and becomes 0
if we determine that n
is not a prime */
#include <stdio.h>
int main(void) {
printf("The value of 1<2 is %d\n", (1<2));
printf("The value of 2<1 is %d\n", (2<1));
}
*/
/* fibo.c -- It prints out the first N
Fibonacci
* numbers.
*/
#include <stdio.h>
int main(void) {
int n; /* The number of fibonacci
numbers we will print */
int i; /* The index of fibonacci
number to be printed next */
int current; /* The value of the (i)th
fibonacci number */
int next; /* The value of the (i+1)th
fibonacci number */
int twoaway; /* The value of the (i+2)th
fibonacci number */
I Fibonacci(I)
=====================
1 1
2 1
3 2
4 3
5 5
6 8
7 13
8 21
9 34
*/
/* funcs.c -- Examples of function
declarations, definitions, and use
*/
#include <stdio.h>
*/
/* funcs.c -- More examples of functions
*/
#include <stdio.h>
int getint(void); /*It prompts user to enter an
integer, which it returns*/
x = getint();
y = getint();
z = getint();
printf("The largest of %d, %d, and %d is
%d\n", x, y, z, getmax(x,y,z));
}
int getint(void) {
int a;
if (m<b)
m = b;
if (m<c)
m = c;
return(m);
}
/* scope1.c -- Simple example showing effects
of the scope rules
*/
#include <stdio.h>
int a=0; /* This is a global variable */
void foo(void);
int main(void) {
int a=2; /* This is a variable local to main
*/
int b=3; /* This is a variable local to main
*/
void foo(void){
int b=4; /* This is a variable local to foo
*/
#include <stdio.h>
int x = 2;
int y = 3;
int z = 4;
void moo(int x, int *y){
int z;
x = x+3;
*y = *y+3;
z = z+3; /*Here z is the local z. Notice
that it has not been
initialized. As you see from the
output below
in this case it was implicitly
initialized to 0.
In general that is not the case
and the compiler
should give you a warning
*/
printf("moo : x = %1d, *y = %1d, y = %1d, z
= %1d\n", x,*y,y,z);
}
int main(void){
moo(x, &y);
printf("main: x = %1d1, y = %1d, z = %1d\n",
x,y,z);
}
/* The output is
moo : x = 5, *y = 6, y = 1073742056, z = 3
main: x = 21, y = 6, z = 4
*/
/* array.c -- Operations on arrays
*/
#include <stdio.h>
int main(void) {
int a[2] = {1,2}; /* The aggregates like
{1,2} are literals for arrays */
int b[2] = {2,3};
int i;
#include <stdio.h>
#define N 10
void oneWay(void);
void anotherWay(void);
int main(void) {
printf("\noneWay:\n");
oneWay();
printf("\nantherWay:\n");
anotherWay();
}
oneWay:
i = 0 vect[i] = 1
i = 1 vect[i] = 2
i = 2 vect[i] = 3
i = 3 vect[i] = 4
i = 4 vect[i] = 5
i = 5 vect[i] = 6
i = 6 vect[i] = 7
i = 7 vect[i] = 8
i = 8 vect[i] = 9
i = 9 vect[i] = 0
antherWay:
i = 0 vect[i] = 1
i = 1 vect[i] = 2
i = 2 vect[i] = 3
i = 3 vect[i] = 4
i = 4 vect[i] = 5
i = 5 vect[i] = 6
i = 6 vect[i] = 7
i = 7 vect[i] = 8
i = 8 vect[i] = 9
i = 9 vect[i] = 10
*/
#define NMAX 10
int main(void) {
int x[NMAX];
int hmny;
do {
printf("Enter integer [%d to terminate] :
", sentinel);
scanf("%d", &temp);
if (temp==sentinel) break;
if (n==nmax)
printf("array is full\n");
else
a[n++] = temp;
}while (1);
return n;
}
for(i=0;i<n/2;i++){
intSwap(&a[i],&a[n-i-1]);
}
}
/* misc.c -- various C constructs */
#include <stdio.h>
int main(void) {
int answer;
short x = 1;
long y = 2;
float u = 3.0;
double v = 4.4;
long double w = 5.54;
char c = 'p';;
/*
The output from a run was:
*/
/* addresses.c -- Playing with addresses of
variables and their contents:
* what is done by C with
variables, addresses, and values.
*/
#include <stdio.h>
int main(void) {
int x;
int *y;
x=1;
y=&x;
printf("Address of x = %d, value of x =
%d\n", &x, x);
printf("Address of y = %d, value of y = %d,
value of *y = %d\n", &y, y, *y);
moo(9,y);
}
#include <stdio.h>
int main(void){
int c;
printf("\tCharacter Code\n"
"\t===============\n");
for (c=32; c<127; c++)
printf("\t %c %4d\n", c, c);
}
Character Code
===============
32
! 33
" 34
# 35
$ 36
% 37
& 38
' 39
( 40
) 41
* 42
+ 43
, 44
- 45
. 46
/ 47
0 48
1 49
2 50
3 51
4 52
5 53
6 54
7 55
8 56
9 57
: 58
; 59
< 60
= 61
> 62
? 63
@ 64
A 65
B 66
C 67
D 68
E 69
F 70
G 71
H 72
I 73
J 74
K 75
L 76
M 77
N 78
O 79
P 80
Q 81
R 82
S 83
T 84
U 85
V 86
W 87
X 88
Y 89
Z 90
[ 91
\ 92
] 93
^ 94
_ 95
` 96
a 97
b 98
c 99
d 100
e 101
f 102
g 103
h 104
i 105
j 106
k 107
l 108
m 109
n 110
o 111
p 112
q 113
r 114
s 115
t 116
u 117
v 118
w 119
x 120
y 121
z 122
{ 123
| 124
} 125
~ 126
*/
/* line.c -- It reads lines from input and
echoes them back.
*/
#include <stdio.h>
int main(void) {
char c;
int count;
for(;;){
count=0;
printf("Please enter a line [blank line to
terminate]> ");
do{
c=getchar();
putchar(c);
count++;
}while (c!='\n');
if(count==1)break;
}
}
#include <stdio.h>
#define NMAX 10
int main(void) {
int x[NMAX];
int hmny;
int who;
int where;
do {
printf("Enter integer [%d to terminate] :
", sentinel);
scanf("%d", &temp);
if (temp==sentinel) break;
if (n==nmax)
printf("array is full\n");
else
a[n++] = temp;
}while (1);
return n;
}
#include <stdio.h>
#define MAXN 8
int main(void) {
int table[MAXN]; /* array where we store the
sequence */
int howmany=0; /* number of elements in
sequence */
int amount; /* amount of shift */
getarray(table, &howmany);
if (howmany==0) {
printf("Sorry, you entered the null
sequence. Good bye.\n");
}else {
do {
printarray(table,howmany);
printf("By how much do you want to
shift[0 to terminate]? ");
scanf("%d",&amount);
if (amount!=0)
shift(table,howmany,amount);
}while(amount!=0);
}
}
if (m<0)
m = n-(abs(m)%n);
for (lcv=0;lcv<n;lcv++)
temp[(lcv+m)%n] = a[lcv];
for (lcv=0; lcv<n;lcv++)
a[lcv]=temp[lcv];
}
do {
answer = getint();
if (answer>0 && (i<MAXN))
a[i++]=answer;
}while(answer>0 && (i<MAXN));
*n = i;
}
for (lcv=0;lcv<n;lcv++){
printf(" %d",a[lcv]);
}
printf("\n");
}
/* sieve.c - It prompts the user to enter an
integer N. It prints out
* It prints out all the primes up to
N included.
* It uses a sieve method. As primes
are found they are
* stored in an array PRIMES and used
as possible factors
* for the next potential prime.
*/
#include <stdio.h>
int main(void) {
int n;
int i,j;
int flag;
int primes[NPRIMES]; /*It will contain the
primes smaller than n
*that we have already
encountered*/
int level; /*1+Number of primes
currently in PRIMES*/
/*Introduction*/
printf("Enter value of N > ");
scanf("%d",&n);
level = 0;
/*Main body*/
for(i=2;i<=n;i++) {
for(j = 0, flag = TRUE; j<level && flag; j+
+)
flag = (i%primes[j]);
if (flag) { /*I is a prime */
printf("%12d\n", i);
if (level < NPRIMES)
primes[level++] = i;
}
}
}
/* string1.c -- Simple string operations
String literals.
printf, scanf, %s, %c
strlen
strcpy
strcmp
*/
#include <stdio.h>
#define MAXBUFF 128
int main(void) {
char c[] = "012345";
char line[MAXBUFF];
int lcv;
int cmp;
sizeof(c)= 7
sizeof(line)= 128
c[lcv]= 48 = 0
c[lcv]= 49 = 1
c[lcv]= 50 = 2
c[lcv]= 51 = 3
c[lcv]= 52 = 4
c[lcv]= 53 = 5
c[lcv]= 0 =
Please enter a string : roses are red
strlen(line) = 5
line = [roses]
012345 is less than roses
012345 is equal to 012345
*/
/* getline.c -- Testing a function that reads a
line
* from input.
*/
#include <stdio.h>
#define MAXBUF 128
int main(void){
int len;
char buffer[MAXBUF];
while(1){
len = getline(buffer, MAXBUF);
if (len==0)break;
printf("len = %d, line = %s\n", len,
buffer);
};
}
#include <stdio.h>
if(argc!=2){
printf("Usage: %s filename\n", argv[0]);
exit(0);
}
if((fp=fopen(argv[1],"r"))==NULL){
perror("fopen");
exit(0);
}
nchars=nwords=nlines=lastnblank=0;
while((c=getc(fp))!=EOF){
nchars++;
if (c=='\n'){
if (lastnblank)
nwords++;
printf("words=%d, characters=%d\n",
nwords, nchars);
nchars=nwords=lastnblank=0;
nlines++;
}else{
if (((c==' ')||(c=='\t'))&(lastnblank))
nwords++;
lastnblank=((c!=' ')&&(c!='\t'));
}
}
printf("lines=%d\n", nlines);
fclose(fp);
}
/* cpfile.c -- Similar to Unix's cp command.
* This program will be called
with two parameters,
* the names of two files. It
copies the second to the first.
*/
#include <stdio.h>
if (argc!=3){
printf("Usage: %s fileout filein\n", argc);
exit(0);
}
if ((fin=fopen(argv[2],"r"))==NULL){
perror("fopen filein");
exit(0);
}
if ((fout=fopen(argv[1],"w"))==NULL){
perror("fopen fileout");
exit(0);
}
while ((c=getc(fin))!=EOF)
putc(c,fout);
fclose(fin);
fclose(fout);
}
/* enum1.c -- Starting to use enumerated
types: Printing for each
* day of the week, today,
yesterday, and tomorrow, both
* as a string and as a number.
*/
#include <stdio.h>
int main(void){
days today;
"============================================\n
");
for (today=monday;today<=sunday;today++)
printf("%s = %d \t %s = %d \t %s = %d\n",
thedays[today], today,
thedays[yesterday(today)],
yesterday(today),
thedays[tomorrow(today)],
tomorrow(today));
}
/*
The output is:
*/
/* enum2.c -- Starting to use enumerated
types: Printing for each
* day of the week, today,
yesterday, and tomorrow, both
* as a string and as a number. We
use typedef
*/
#include <stdio.h>
int main(void){
days today;
"============================================\n
");
for (today=monday;today<=sunday;today++)
printf("%s = %d \t %s = %d \t %s = %d\n",
thedays[today], today,
thedays[yesterday(today)],
yesterday(today),
thedays[tomorrow(today)],
tomorrow(today));
}
/*
The output is:
*/
/* binary.c -- Read a sorted integer array and
then do binary searches.
* [We do not check to make sure
that the array is sorted.]
*/
#include <stdio.h>
#define NMAX 10
int main(void) {
int x[NMAX];
int hmny;
int who;
int where;
do {
printf("Enter integer [%d to terminate] :
", sentinel);
scanf("%d", &temp);
if (temp==sentinel) break;
if (n==nmax)
printf("array is full\n");
else
a[n++] = temp;
}while (1);
return n;
}
while (low<high){
middle = (low+high)/2;
if (who<=a[middle])
high=middle;
else
low=middle+1;
}
if (who==a[low])
return low;
else
return -1;
}
* selection.c -- Read an integer array, print
it, then sort it and
* print it. Use the selection sort method.
*/
#include <stdio.h>
#define NMAX 10
int main(void) {
int x[NMAX];
int hmny;
int who;
int where;
do {
printf("Enter integer [%d to terminate] :
", sentinel);
scanf("%d", &temp);
if (temp==sentinel) break;
if (n==nmax)
printf("array is full\n");
else
a[n++] = temp;
}while (1);
return n;
}
void selectionSort(int a[], int n)
/* It sorts in non-decreasing order the first N
positions of A. It uses
* the selection sort method.
*/
{
int lcv;
int rh; /*Elements in interval rh..n-1
are in their final position*/
int where; /*Position where we have current
maximum*/
int temp; /*Used for swapping*/
for(rh=n-1;rh>0;rh--){
/*Find position of largest element in range
0..rh*/
where = 0;
for (lcv=1;lcv<=rh;lcv++)
if (a[lcv]>a[where])
where = lcv;
temp = a[where];
a[where] = a[rh];
a[rh] = temp;
}
}
/* bubble.c -- Read an integer array, print it,
then sort it and
* print it. Use the bubble sort method.
*/
#include <stdio.h>
#define NMAX 10
do {
printf("Enter integer [%d to terminate] :
", sentinel);
scanf("%d", &temp);
if (temp==sentinel) break;
if (n==nmax)
printf("array is full\n");
else
a[n++] = temp;
}while (1);
return n;
}
while (limit) {
lastChange = 0;
for (lcv=0;lcv<limit;lcv++)
/* Notice that the values in positions
LIMIT+1 .. N are in
* their final position, i.e. they are
sorted right */
if (a[lcv]>a[lcv+1]) {
temp = a[lcv];
a[lcv] = a[lcv+1];
a[lcv+1] = temp;
lastChange = lcv;
}
limit = lastChange;
}
}
/* number.c -- Reading and writing integer as
character sequences .
* Notice that the base for the
digits is the constant
* BASE that I have chosen to be
10. Of course, you
* use a different value. You might
even chose to
* have the base as a function
parameter.
*/
#include <stdio.h>
#include <assert.h>
#define BASE 10
#include <stdio.h>
#define SIZE 8
do {
printf("Enter integer [%d to terminate] :
", sentinel);
scanf("%d", &temp);
if (temp==sentinel) break;
if (n==nmax)
printf("array is full\n");
else
a[n++] = temp;
}while (1);
return n;
}
int main(void){
int x[SIZE], nx;
int y[SIZE], ny;
*/
/* hmw10.c -- Outline of solution for homework
10
*/
#include <stdio.h>
while ((c=getc(fd))!='\n'){
if(c==EOF)return EOF;
if(n<nmax)
buff[n++]=c;
}
buff[n]='\0';
return n;
}
if((fdu=fopen(filename,"r"))==NULL){
perror("fopen");
exit(1);
}
i=j=0;
while((c=getc(fdu))!=EOF){
if(i==nmax)break; /* We have filled up the
table */
if (c=='\n'){ /* We have finished reading a
line */
table[i++][j]='\0';
j=0;
}else if(j<LINESIZE)
table[i][j++]=c;
}
fclose(fdu);
return i;
}
void writeStringArray(char *filename, linetype
table[], int n)
/* It writes the first n lines of table
into the file
* called filename
*/
{
FILE *fdu; /* File descriptor used for
filename */
int i;
if((fdu=fopen(filename,"w"))==NULL){
perror("fopen");
exit(1);
}
for(i=0;i<n;i++)
fprintf(fdu, "%s\n", table[i]);
fclose(fdu);
}
int main(void){
int n;
linetype linearray[ARRAYSIZE];
linetype who;
int where;
FILE *fdw; /* FILE descriptor used for
"who.dat" */
FILE *fda; /* File descriptor used for
"answers.dat" */
#include <stdio.h>
#define SIZE 10
#define NAMESIZE 25
typedef struct {
char name[NAMESIZE];
int midterm;
int final;
int homeworks;
} student;
if(n<=0)
return;
if((fd=fopen(filename,"w"))==NULL){
perror("fopen");
exit(1);
}
for (i=0;i<n;i++){
fprintf(fd,"%s %d %d %d\n",
a->name, a->midterm, a->final, a-
>homeworks);
a++;
}
fclose(fd);
}
if((fd=fopen(filename,"r"))==NULL){
perror("fopen");
exit(1);
}
while(fscanf(fd,"%s %d %d %d",
a->name, &a->midterm, &a->final,
&a->homeworks)!=EOF){
if(++i==nmax)break; /* We have filled up
the table */
a++;
}
fclose(fd);
return i;
}
if(argc!=3){
printf("Usage: %s infile outfile\n",
argv[0]);
exit(0);
}
n = readStudentArray(argv[1],table,SIZE);
writeStudentArray(argv[2],table,n);
}
/* merge.c -- Given two sorted sequences of
integers, it creates
* a sorted sequence consisting of
all their numbers.
*/
#include <stdio.h>
#define NMAX 10
int main(void) {
int x[NMAX] = {1,3,5,6,7}; /* The first
sorted sequence */
int y[NMAX] = {2,3,4}; /* The second sorted
sequence */
int z[NMAX+NMAX]; /* The merge sequence */
int nz;
merge(z,&nz,x,5,y,3);
printIntArray(z,nz);
}
cursora=cursorb=cursorc=0;
while((cursora<na)&&(cursorb<nb))
if (a[cursora]<=b[cursorb])
c[cursorc++]=a[cursora++];
else
c[cursorc++]=b[cursorb++];
while(cursora<na)
c[cursorc++]=a[cursora++];
while(cursorb<nb)
c[cursorb++]=b[cursorb++];
*nc = cursorc;
}
/* stringmerge.c -- Given two sorted files of
strings, it creates
* a sorted file consisting of all
their elements.
* The names of the files are passed
as command
* line parameters.
*/
#include <stdio.h>
#define MAXBUFFER 128
while ((c=getc(fd))!='\n'){
if(c==EOF)return EOF;
if(n<nmax)
buff[n++]=c;
}
buff[n]='\0';
return n;
}
if ((fd1=fopen(filename1, "r"))==NULL) {
perror("fopen");
exit(1);
}
if ((fd2=fopen(filename2, "r"))==NULL) {
perror("fopen");
exit(1);
}
if ((fd3=fopen(filename3, "w"))==NULL) {
perror("fopen");
exit(1);
}
ln1 = getline(fd1,buffer1,MAXBUFFER-1);
ln2 = getline(fd2,buffer2,MAXBUFFER-1);
while (ln1!=EOF){
fprintf(fd3, "%s\n", buffer1);
ln1=getline(fd1,buffer1,MAXBUFFER-1);
n++;
}
while (ln2!=EOF){
fprintf(fd3, "%s\n", buffer2);
ln2=getline(fd2,buffer2,MAXBUFFER-1);
n++;
}
fclose(fd1);
fclose(fd2);
fclose(fd3);
return n;
}
#include <stdio.h>
#define SIZE 10
#define NAMESIZE 25
typedef struct {
char name[NAMESIZE];
int midterm;
int final;
int homeworks;
} student;
if(n<=0)
return;
if((fd=fopen(filename,"w"))==NULL){
perror("fopen");
exit(1);
}
for (i=0;i<n;i++){
fprintf(fd,"%s %d %d %d\n",
a->name, a->midterm, a->final, a-
>homeworks);
a++;
}
fclose(fd);
}
if((fd=fopen(filename,"r"))==NULL){
perror("fopen");
exit(1);
}
while(fscanf(fd,"%s %d %d %d",
a->name, &a->midterm, &a->final,
&a->homeworks)!=EOF){
if(++i==nmax)break; /* We have filled up
the table */
a++;
}
fclose(fd);
return i;
}
for(i=0;i<n1;i++){
table3[j++] = table1[i];
}
for(i=0;i<n2;i++){
table3[j++] = table2[i];
}
return n1+n2;
}
int main(int argc, char *argv[]){
int n1, /* Number of students in first
sequence */
n2, /* Number of students in second
sequence */
n3; /* Number of students in
resulting sequence */
if(argc!=4){
printf("Usage: %s unsfile1 unsfile2
outfile\n", argv[0]);
exit(0);
}
n1 = readStudentArray(argv[1],table1,SIZE);
sortStudentArray(table1,n1);
n2 = readStudentArray(argv[2],table2,SIZE);
sortStudentArray(table2,n2);
n3 = mergeStudentArray(table3, table1, n1,
table2, n2);
writeStudentArray(argv[3],table3,n3);
}
/* clean.c -- Given as command line parameter a
filename,
* it removes from that file all
occurrences of ^M
* If 'clean' is the executable
image of this
* program, you can use it as
follows:
* % clean dirtyfile >
cleanfile
*/
#include <stdio.h>
#define CONTROLM 13
if(argc!=2){
printf("Usage: %s filename\n", argv[0]);
exit(0);
}
if((fd = fopen(argv[1],"r"))==NULL){
perror("fopen");
exit(1);
}
while((c=getc(fd))!=EOF)
if (c!=CONTROLM)
putchar(c);
fclose(fd);
}
/* studentlist.c -- Reads a file containing a
sequence of records
* representing students,
places them into a linked
* list (a queue), then writes
that out to a new files.
* The names of the files are
passed in as command
* line parameters.
*/
#include <stdio.h>
#define NAMESIZE 25
typedef struct {
char name[NAMESIZE];
int midterm;
int final;
int homeworks;
} student;
typedef struct {
nodeptr head; /* Here we point to the oldest
element in the queue */
nodeptr tail; /* Here we point to the most
recent element in queue*/
} queue;
queue *init(void){
/* It creates, initializes, and returns a
queue */
queue *q;
if((q=(queue *)malloc(sizeof(queue)))==NULL){
perror("malloc");
exit(1);
}
q->head=NULL;
q->tail=NULL;
return q;
}
void final(queue *q){
/* It frees the queue q */
student s;
while(get(q,&s));
free(q);
}
if ((p=(nodeptr)malloc(sizeof(node)))==NULL){
perror("malloc");
exit(1);
}
p->value=*s;
p->next = NULL;
if (q->head == NULL)
q->head = p;
else
q->tail->next = p;
q->tail = p;
}
if(p=q->head){
q->head = q->head->next;
if((q->head)==NULL)
q->tail = NULL;
*s=p->value;
free(p);
return 1;
}else
return 0;
}
if((fd=fopen(filename,"w"))==NULL){
perror("fopen");
exit(1);
}
while(get(q, &s)){
fprintf(fd,"%s %d %d %d\n",
s.name,
s.midterm,
s.final,
s.homeworks);
}
fclose(fd);
}
if((fd=fopen(filename,"r"))==NULL){
perror("fopen");
exit(1);
}
while(fscanf(fd,"%s %d %d %d",
temp.name,
&temp.midterm,
&temp.final,
&temp.homeworks)!=EOF){
put(q,&temp);
i++;
}
fclose(fd);
return i;
}
q = init();
if(argc!=3){
printf("Usage: %s infile outfile\n",
argv[0]);
exit(0);
}
n = readStudentList(argv[1],q);
writeStudentList(argv[2],q);
final(q);
}
/* makebinfile.c - Reads a file containing a
sequence of text records
* and writes it out to a new
binary files.
* The names of the files are
passed in as command
* line parameters.
*/
#include <stdio.h>
#define SIZE 10
#define NAMESIZE 25
typedef struct {
char name[NAMESIZE];
int midterm;
int final;
int homeworks;
} student;
if(argc!=3){
printf("Usage: %s infile outfile\n",
argv[0]);
exit(0);
}
if((fdin=fopen(argv[1],"r"))==NULL){
perror("fopen");
exit(1);
}
if((fdout=fopen(argv[2],"w"))==NULL){
perror("fopen");
exit(1);
}
while(fscanf(fdin,"%s %d %d %d",
who.name, &who.midterm, &who.final,
&who.homeworks)!=EOF){
m = writeastudent(fdout, &who);
printf("m=%d\n", m);
n++;
}
printf("n=%d\n", n);
fclose(fdin);
fclose(fdout);
}