C Programming Answers for chapter 14

22
ררררר ררררר רררר14 ררררר ררררC – ררררר ווו וווו: ו"ו ווווו וווו ווו וווו ? ווווו וו וווו)ווווו וווו( ווווו: וווו וו ווווו ווווו. וווווווו: וווו וווווו וו ווווווו, וו ווווו)ווו ווווו( וו ווווו ווו- וווו ווו ווווווו וווו, ווווו וווו ווווווו וו ווווווו ווווו וווו. וווו: וו ווו וווו וו ווווו וווווו וווווו ווו ווווווו וווו ווו ווווו וווווו? ווווו וווווו וו וווו: וווו וווווווו וווווו ווווו וו "ווווו" וווו ווווווו ווווו וווו ווווו וו. וווו וווווו וו "ווווו" וו ווווו. וווו וווווו וווווו ווווו וו ווווו וו וווו, וווווו ווווו וו ווווו וו וווווו וווווו ווווווו, וו ווו וווו וו וווווו וווו – ווווו ווו. וווו: ווווו20 ווווו ווווו ווו, ווו ווו וווווו ווווו- וווו וווווווו ווווו ו21 . וווו "וווו ווווו" ווווו, וו ווווו וו וו וווו וווו ווווו ווו, ווו וו וווו ווווו ווו, ווווו וו וווווווו ווווו ווווווו ווווו( ווווו וווו וווו וווווו.) וווווו וו ווו ווווו ו"ו ווווווו וווווו, ווו ווווו וו וווווו וווווו ווווו( וווווFILE - וווווו ו)* stdio.h . ווו וו, וווווווו וווווווווווו( וווו ווווו וווווו וווווו וו וווווFile manipulation functions - ווווווו ו) stdio.h . ווEOF ווווו- וstdio.h - - ו1 . וווווו ווו וווו ווווו וווווו וווו ווווו וווווו ווו ררררר רררר( ASCII ווווו וו( ) ררררר ווו וו וו וו ווו ווווו ווו ווו) ווווו וו ווווו( bat,txt,doc וו) ווו וווו ווווו ווו ווו ווווו ררררר ררררררווו וווו( ווו ווווווו וווווווו ווו ווו וווווווexe,com,bin,gif .)' ווו וווו1 וווו22

Transcript of C Programming Answers for chapter 14

14 ררררר ררררר רררר – ררררר C ררררר רררר

ווו וווו: ו"ו ווווו וווו

? ווו וווווווו וו ווווו וווו( )ווווו ווווו וו וווו ווווו:

ווווו. :וווווווו

וו ווווו )ווו ווווו( וווו וווווו וו ווווווו, וו ווווווווו ווו ווווווו וווו, ווווו וווו ווווווו וו ווווווו- ווו

ווווו וווו.וווו: וו ווו וווו וו ווווו וווווו וווווו ווו ווווווו

וווו ווו ווווו וווווו? :ווווו וווווו וו וווו

וווו וווווווו וווווו ווווו וו "ווווו" וווו וווווווווווו וווו ווווו וו.

.וווו וווווו וו "ווווו" וו ווווווווו וווווו וווווו ווווו וו ווווו וו וווו, וווווו ווווו

וו ווווו וו וווווו וווווו ווווווו, וו ווו וווו וו וווווווווו – ווווו ווו.

ווווו ווווו ווו, ווו ווו וווווו ווווו20וווו: ווווו .21וווו וווווווו ווווו ו-

וווו "וווו ווווו" ווווו, וו ווווו וו וו וווו ווווווווו ווו, ווו וו וווו ווווו ווו, ווווו וו וווווווווו ווווווו ווווו )ווו וווווו וווו וווו ווווו

וווווו וו ווו ווווו(.ווווו וווווו וווווו וו ווווו ווו וווווו, ווווווו ו"ו

. ווו וו, וווווווו ווווווstdio.h *( וווווו ו-FILEווווו ))וווווו ווווו וו וווווו וווווו ווווו Fileוווו

manipulation functions-ווווווו ו )stdio.h וו .EOFווווו .1 ו- -stdio.hו-

ררררר ררררוווווו ווו וווו ווווו וווווו וווו ווווו וווווו ווו

(ASCII ווו ווו וו וו וו ווו ווווו וווררררר( )ווווו וו )

ווו וווו ווווו ווו ווו ווווו( ווbat,txt,doc)ווווו וו ווווו

ווו ווווווו וווווווו ווו ווו ווווווו )ווו ווווררררר רררררר

exe,com,bin,gif.)'ווו

22 וווו 1וווו

14 ררררר ררררר רררר – ררררר C ררררר רררר

ווו וווו: ו"ו ווווו ווווווווו ווו וווו וווווו וווו וווו ווווו וווווו.

: ווווו-וווווו A 1ווווו 2 B

וווווו ווווו וווו :

ווו ווווו וווו ווווו וווו 49 65 50 66ווווו

וווו ווו ווו ווווו65 ו 1 וווו ווו ווו וווו וו 49וווווו. )

ווו וווו..(.Aוו ווו

.2B 1Aווווו וווווו

וווווו וווווו וווווו ווווו וווו ווווווו. וווו וווווו ווווו

וווווו וווו ווו ווו וווו ווווו ווו וווווו ווו וווווו וווו

ווווו ווווו ווווו.

וווו וווווו וווו וווו וו וווווו ווווו ווווו וווו וווו וווווווו

" ווווו וווו וווווווו ווווו :HELLOווווו וווו. וווווו "

79 76 76 69 72

ררררר/ררררר וו ווווו ו. ררררר ווווו : ו. 3ווווו ווווו וווו

וו ווווו.ררררר ווווו ו.

ווווו וווווו ווווו וווווו וווווו וווווו וווווווו וווו.Cוווו

ווווו וו ווווו

- ו stdio.h וווווו FILE- )וווו ווווו ווו וו ווווו ,

structureוווווו וווווו וווו. ווווו וווו, ווווווווו וווווו )

( ווו ווווווו ווו וווווווווFILEווווו ווווווו ווווווו ווווו )

וווווו וווו ווווווו.

וווו וו :FILEווווו

/* Definition of the control structure for streams

*/22 וווו 2וווו

14 ררררר ררררר רררר – ררררר C ררררר רררר

ווו וווו: ו"ו ווווו ווווtypedef struct

{

int level ; /* fill/empty

level of buffer */

unsigned flags ; /* File status

flags */

char fd ; /* File

descriptor */

unsigned char hold ; /* Ungetc char

if no buffer */

int bsize ; /* Buffer size

*/

unsigned char *buffer ; /* Data transfer

buffer */

unsigned char *curp ; /* Current active

pointer */

unsigned istemp ; /* Temporary

file indicator */

short token ; /* Used for

validity checking */

} FILE; /* This is

the FILE object */

22 וווו 3וווו

14 ררררר ררררר רררר – ררררר C ררררר רררר

ווו וווו: ו"ו ווווו וווו וווו וווו, וו10 וו 1 - וווו וווווו וווווו וו ווווווו 1ווווו

וווו וווווו ווווו.ווווו:

#include <stdio.h>int main)({FILE *file_output;int iCounter;file_output = fopen)"numbers.txt", "wt"(;if )file_output == NULL({printf)"ERROR: Cannot create output file.\n"(;return 1;}for )iCounter = 1; iCounter <= 10; ++iCounter({Fprintf)file_output, "%d\n", iCounter(;}fclose)file_output(;return 0;}

– וווו וווווו ווווו וו ווווווו וווווווו ווווווו2ווווו ( וווו ווווו, ווווו וווו ווווו וווווו וווו.1)וווווו

#include <stdio.h>#define MAX_SIZE 10int main)({FILE *fin;int iCounter;int numbers[MAX_SIZE];fin = fopen)"numbers.txt", "rt"(;if )fin == NULL({printf)"ERROR: Cannot open input file.\n"(;return 1;}for )iCounter = 0; iCounter < MAX_SIZE; ++iCounter({fscanf)fin, "%d\n", &numbers[iCounter](;printf)"%d\n", numbers[iCounter](;

22 וווו 4וווו

14 ררררר ררררר רררר – ררררר C ררררר רררר

ווו וווו: ו"ו ווווו וווו}fclose)fin(;return 0;}

– וווו וווווו וווווו וווו וווו וווו, ווווווו ווו וו3ווווו וווו ווווו ו"ו וווו וו וווו ווווו.

ווווו:#include <stdio.h>#include <stdlib.h>void main(({FILE *f=fopen)"Textfilesinc.doc","rt"(;long size;if)f==NULL(exit)1(;fseek)fp,0,SEEK_END(; // move to end of filesize=ftell)f(; /* get the current "place" of file in byts.printf)"%d", size(;}

– וווו וווווו וווווו וווו וווווווו וווווו ווו ווווו4ווווו וווווו ווווווו וו ווווווו ווווו.

ווווו: #include <stdio.h>int main)({ char name[50]; int marks,i,n; printf)"Enter number of students: "(; scanf)"%d",&n(; FILE *fptr; fptr=)fopen)"student.txt","w"((; if)fptr==NULL( { printf)"Error!"(; return 0;

22 וווו 5וווו

14 ררררר ררררר רררר – ררררר C ררררר רררר

ווו וווו: ו"ו ווווו וווו } for)i=0;i<n;++i( { printf)"For student%d\nEnter name: ",i+1(; scanf)"%s",name(; printf)"Enter marks: "(; scanf)"%d",&marks(; fprintf)fptr,"\nName: %s \nMarks=%d \n",name,marks(; } fclose)fptr(; return 0;}

– וווו וווווו וווווו וווו וווווווו וווווו ווו ווווו5ווווו וווו וווו וו וווו ווווווווו ווווווו וו ווווווו ווווו, ווווו וו

וווו ווווווו ווווווו ווווווו ווווו.ווווו:

#include <stdio.h>int main)({ char name[50]; int marks,i,n; printf)"Enter number of students: "(; scanf)"%d",&n(; FILE *fptr; fptr=)fopen)"student.txt","a"((; if)fptr==NULL( { printf)"Error!"(; return 0; } for)i=0;i<n;++i( { printf)"For student%d\nEnter name: ",i+1(; scanf)"%s",name(;

22 וווו 6וווו

14 ררררר ררררר רררר – ררררר C ררררר רררר

ווו וווו: ו"ו ווווו וווו printf)"Enter marks: "(; scanf)"%d",&marks(; fprintf)fptr,"\nName: %s \nMarks=%d \n",name,marks(; } fclose)fptr(; return 0;}

וווו ווווו ווווווו וו ווווו וווו וו ווווו ווווו. ווו – 6ווווו וו ווווווו וווו וו ווווווו וווווו וווו ווווו ווו ווו וו ווו

וווו וווו ווווו וווו וו ווו.ווווו:

#include <stdio.h>#define SIZE 5typedef struct students{

char name[50];char birthday[12]; // dd/mm/yyyyint age;char gender[8];// Male/Female

}students;int main)({ students Arr[SIZE],Brr[SIZE]; FILE *fptr; int i; fptr=fopen)"StudentsFile.txt","w"(;

printf)"%d Students File details:",SIZE(; for)i=0;i<SIZE;++i( { printf)"Enter student number %d details:\n",i+1(;

fflush)stdin(; printf)"Enter Name: "(; gets)Arr[i].name(; printf)"Enter Birthday: "(; gets)Arr[i].birthday(;

printf)"Enter Gender: "(;gets)Arr[i].gender(;printf)"Enter Age: "(;scanf)"%d",&Arr[i].age(;puts)""(;

22 וווו 7וווו

14 ררררר ררררר רררר – ררררר C ררררר רררר

ווו וווו: ו"ו ווווו ווווfprintf)fptr,"%s\n%s\n%d\n%s\r\

n",Arr[i].name,Arr[i].birthday,Arr[i].age,Arr[i].gender(;}

fclose)fptr(; fptr=fopen)"StudentsFile.txt","r"(;

puts)"\nStudent File details:"(; for)i=0;i<SIZE;++i( { printf)"Student number %d details:\n",i+1(;

fscanf)fptr,"%s%s%d%s",&Brr[i].name,&Brr[i].birthday,&Brr[i].age,&Brr[i].gender(;

printf)"Name: %s\nBirthday: %s\nAge: %d\nGender: %s\n\n",Brr[i].name,Brr[i].birthday,Brr[i].age,Brr[i].gender(;

} fclose)fptr(;}

– וווו וווווו וווווו וווווו ווווו וווווו ווווווו וו7ווווו ווווווו.ווווו:

#include <stdio.h>#include <stdlib.h>void main)({FILE *f=fopen)"numbers.txt","rb"(;int a[3];int check;if)f==NULL({printf)"error in opening"(;exit)1(;}check=fread)&a,sizeof(int),3,f(;if)check!=3({printf)"error in reading"(;exit)1(;}printf)"%d %d %d", a[0], a[1], a[2](;}

22 וווו 8וווו

14 ררררר ררררר רררר – ררררר C ררררר רררר

ווו וווו: ו"ו ווווו וווו

- ווווו וווווו וווווו ווווו וווווו, ווו וו ווווווו8ווווו ווווו וו וווו ווווווו.

#include <conio.h>#include <stdio.h>#include <stdlib.h>FILE *f;char file_name[15];char c;void write_2_file )char file_name[]({

f = fopen)file_name, "wt"(;if )f == NULL(

exit)1(;else{printf)"The file %s has been opened \n\r", file_name(;printf )"Enter text to the file \n\r"(;fflush )stdin(; //cleaning the bufferfor )fscanf)stdin, "%c", &c(; !feof )stdin(; fscanf

)stdin, "%c", &c((fprintf )f, "%c", c(;if )fclose)f((

exit )1(; //the closing wasnt sucssesfulelse

printf )"\n\rThe file %s has been closed\n\r", file_name(;

}}void read_from_file )char file_name[]({

FILE *f;printf)"Reading from the file %s :\n\r",file_name(;f = fopen )file_name, "rt"(;if )f == NULL({

22 וווו 9וווו

14 ררררר ררררר רררר – ררררר C ררררר רררר

ווו וווו: ו"ו ווווו ווווprintf )"Can't open the file %s ", file_name(;exit)1(;

}else{printf)"The file %s has been opened \n\r", file_name(;for )fscanf)f,"%c", &c(; !feof )f(; fscanf )f,"%c", &c((

fprintf )stdout, "%c", c(;if )fclose)f((

exit )1(; //the closing wasnt sucssesfulelse

printf )"\n\rThe file %s has been closed", file_name(;

}}void main)({

printf )"\n\rEnter file name to create: "(;scanf )"%s", file_name(;write_2_file )file_name(;read_from_file )file_name(;

}

- ווווו וווווו וווווו ווווו וווווו, ווו וו ווווווו9ווווו ווווו וו וווו ווווווו.

#include <stdio.h>#include <conio.h>#include <stdlib.h>FILE *from_f;FILE *to_f;char from_file[15];char to_file[15];char c;void coping )char from_file[], char to_file[]({

from_f = fopen )from_file, "rt"(;22 וווו 10וווו

14 ררררר ררררר רררר – ררררר C ררררר רררר

ווו וווו: ו"ו ווווו ווווto_f = fopen )to_file, "wt"(;if ))from_f == NULL( || )to_f == NULL((

exit)1(;else{

printf)"The files %s and %s has been opened \n\r", from_file, to_file(;

printf)"The text in the file %s\n\r", from_file(;for )fscanf)from_f, "%c", &c(; !feof )from_f(;

fscanf )from_f, "%c", &c(({

fprintf )to_f, "%c", c(;fprintf )stdout,"%c", c(;

}if ))fclose)to_f(( || )fclose )from_f(((

exit )1(; //the closing wasnt sucssesfulelse

printf )"\n\rThe files %s and %s has been closed\n\r", from_file, to_file(;

}}void main)({

printf )"\n\rEnter file name to copy: "(;scanf )"%s", from_file(;printf )"\n\rEnter a new file name: "(;scanf )"%s", to_file(;coping )from_file, to_file(;

}

- ווווווו ווווו וווו וו ווו ווווו וווווו ווו וו וווו10ווווו .ווווווווווו:

#include <stdio.h>#include <conio.h>#include <stdlib.h>

22 וווו 11וווו

14 ררררר ררררר רררר – ררררר C ררררר רררר

ווו וווו: ו"ו ווווו ווווFILE *f1;FILE *f2;char first_f[15];char second_f[15];char c1,c2;int flag = 1;void compare )char first_f[], char second_f[]({

f1 = fopen )first_f, "rt"(;f2 = fopen )second_f, "rt"(;

if ))f1 == NULL( || )f2 == NULL((exit)1(;

else{

printf)"The files %s and %s has been opened \n\r", first_f, second_f(;

for ) )fscanf)f1, "%c", &c1(( && )fscanf )f2, "%c", &c2((;

)!feof )f1(( && )!feof )f2((;)fscanf )f1, "%c", &c1((&& )fscanf )f2,

"%c", &c2(( ({

if )! c1 == c2({

printf )"\n\rThe two files are not the same "(;

flag = 0;break;

}fprintf )stdout,"%c", c1(;

}if )flag(

printf)"\n\rThe two files are the same"(;if ))fclose)f1(( || )fclose )f2(((

22 וווו 12וווו

14 ררררר ררררר רררר – ררררר C ררררר רררר

ווו וווו: ו"ו ווווו ווווexit )1(; //the closing wasnt sucssesful

elseprintf )"\n\rThe files %s and %s has been

closed\n\r", first_f, second_f(;}

}void main)({

printf )"\n\rEnter the first file name: "(;scanf )"%s", first_f(;printf )"\n\rEnter the second file name: "(;scanf )"%s", second_f(;compare )first_f, second_f(;

}

- וווווו וווווו וו וו' ווווווו וו וו וווו ווווו וווו.11ווווו ווווו:

#include <stdio.h>#include <conio.h>#include <stdlib.h>FILE *f;void count )char ch, char file_n[]({

int num = 0; char c;

if ))f = fopen )file_n, "rt"(( == NULL({

printf)"\n\rCan't open file %s ", file_n(;exit)1(;

}else{

printf)"The file %s has been opened \n\r", file_n(;for )c = fgetc)f(; !feof )f(; c = fgetc )f(({

if )c == ch(22 וווו 13וווו

14 ררררר ררררר רררר – ררררר C ררררר רררר

ווו וווו: ו"ו ווווו ווווnum++;

elsecontinue;

}printf)"\n\rThe are %d %c in the file %s",num ,ch,

file_n(;if )fclose)f((

exit )1(; //the closing wasnt sucssesfulelse

printf )"\n\rThe files %s has been closed\n\r",file_n(;

}}void main)({

char ch;char file_name[15];printf )"\n\rEnter the char : "(;scanf )"%c", &ch(;printf )"\n\rEnter the name of a file: "(;scanf )"%s", file_name(;count )ch, file_name(;

}

וווו וווווו ווו ווווו וווו וווו )וווו וווו(, - 12ווווו ווווווו וווו וו וווו.

:ווווו1. void main)({2. FILE *f1; : ווווו ווו ווווו3. char ch;4. int chReadOk;5. f1= fopen)"1.txt","r"(; : וווו ווווווווווו6. if)!f1({ : וווווו ווו ווווווווווו

22 וווו 14וווו

14 ררררר ררררר רררר – ררררר C ררררר רררר

ווו וווו: ו"ו ווווו וווו7. printf)"Error opening file!\n"(; : וו וו

ווווו וווו– 8. return;9. }

:וווו וו ווווו - וו וווו, ווווו וווווו וו ווו וו10. while ))chReadOk =fscanf)f1, "%c", &ch((==1(11. printf)"%c",ch(; 12. fclose)f1(; ווווו וו וווו :13. }

- ווו וו ווווווו וו וווווו וו וווו ווווו וווו וווו13ווווו ווו.

:ווווווו וווו וו וווווווו ווווו:

2. FILE * f1, *f2; ווו ווווווווווו ווווווו :

5.1. f2=fopen)"2.txt", "w"(; : וווו ווווווווו5.2. if)!f2({ וווווו ווו ווווו:וווווו5.3. printf)"Error opening file!\n"(;

ווווו וווו – וו וו :5.4. fclose)f1(;5.5 return;5.6. }11. fputc)ch,f2(; ווווו- וווווו ווווו

וווו ווווו : 12.1. fclose)f2(; ווווו וווווו:וווו

5.1וו וווו וו וווו וווווו וו ווווווו ווו ווו, וווו וו ווווווווו ווו:

5.1. f2=fopen)"2.txt","a"(;וווווו )ווו ווו ווו( ווf1 ווווו ווווו וו f2ווווו :ווווו

(. append modeווווו וווו ווווו )22 וווו 15וווו

14 ררררר ררררר רררר – ררררר C ררררר רררר

ווו וווו: ו"ו ווווו וווו - ווו וו ווווווו וו וווווו ווווו וווו וו ווווו14ווווו

ווווו )וווו ווו ווווו וווו ווווו ווווווו5ווווווו וווו ו-’(.nווו ‘\

:ווווו

#include <stdio.h>#include <string.h>

void main )({

FILE *f1, *f2;char buff[256]={'\0'};f1= fopen)"1.txt","r"(;f2= fopen)"2.txt","w"(;if)!f1(

{printf)"Error opening file!\n"(;return;

}if)!f2({

printf)"Error opening file!\n"(;fclose)f1(;return;

}

while )fgets)buff,256,f1( != NULL( if )strlen)buff( >5(

fputs)buff,f2(;

fclose)f1(;fclose)f2(;}

22 וווו 16וווו

רררררר ררררf1ררררר רררר רררר רררררר.

ררר רר ררררררר ררררר ר-5,

char *fgets)char *str, intווווו וווו וווווווו len, FILE *stream(:

ווווו' ווווו וווווו וווווו וו ווווו.1 וווווN )ווווו ו"ו וווו len-1ווווו

(. ווווו: וווו וווווlen=N+1וווו ו- '.0וווו ווו ‘\

ווווו' וווווו וווו ווווו וווווו.2’nוווו: )ו( וווו וווו ווווו ווו ‘\

ווו וווווו וווווווו )strוווו ו-’ . )ו( וווו0( וו ווו \getsווווו'

ווווו וו וו' וווווו וווווו. )ו( וווו. EOFוווווו ו-

14 ררררר ררררר רררר – ררררר C ררררר רררר

ווו וווו: ו"ו ווווו וווו1 - וווו ווווווו ווו ווווו ווו וווו וו ווווו ווווווו 15ווווו

וו ווווו ווווו וווווו וווו.1 וו וו ווווו וו-0וו וו וווו

:ווווו#include <stdio.h>#include <string.h>int diff)char* fname1,char* fname2({

FILE *f1, *f2;char c1,c2;f1= fopen)fname1,"r"(;f2= fopen)fname2,"r"(;if)!f1({

printf)"Error openning file %s!\n",fname1(;return -1;

}if)!f2({

printf)"Error openning file %s!\n",fname2(;fclose)f1(;return -1;

}

do{c1ReadOK = fscanf)f1,"%c",&c1(;c2ReadOK = fscanf)f2,"%c",&c2(;

}while )c1ReadOK==1 && c2ReadOK==1 && c1==c2(;fclose )f1(; fclose )f2(;return )c1==c2(;

}void main )({

char filename1[6]="1.txt";char filename2[6]="2.txt";int ans;if)ans=diff)filename1,filename2(({

22 וווו 17וווו

רררר רר ררררררר רר ררר

רר רררררררררר רררררר )ררררר

fscanfרררררר רררר

רר ררר

רר רררררר רררר, ר"ר רררררר רררר ררררררררר רררררר ררררררר רררר - ררר ררררר

1 .רר רררררר ררררר, ר"ר ררר ררר ררר ררר

14 ררררר ררררר רררר – ררררר C ררררר רררר

ווו וווו: ו"ו ווווו ווווif)ans==-1(

return;else

printf)"Identical Files!!!\n"(;}else

printf)"Different Files!!!\n "(;

}

- וווו ווו וווווווו, ווו ווווו וו וווו, ווווו ווווו וו16וווו intוו ווו ווווו, וווווו וו וווו ווווו ווווו. וווווו ווווו וו-

-וו ווו ווווו, וווווו וו וווו וווווintוו וווו, ווווו ווווו וו וו ווווו ווווו וווווו וווו,1וווו ווווו. וו וווו' ווווו -

.1וווו ווווו

:ווווו#include <stdio.h>#include <string.h>int saveArr)int* arr,int len, char* fname({

FILE *f;int i;f = fopen)fname,"w"(;if)!f({

printf)"Error opening file in function saveArr)(!\n"(;

return -1;}

for )i=0; i<len ; i++(fprintf)f,"%d,",arr[i](;

fclose)f(;return 1;

22 וווו 18וווו

14 ררררר ררררר רררר – ררררר C ררררר רררר

ווו וווו: ו"ו ווווו וווו}int loadArr)int* arr,int len, char* fname({

FILE *f;int i,readOk=1;f = fopen)fname,"r"(;if)!f({

printf)"Error opening file in function loadArr)(!\n"(;

return -1;}for )i=0; i<len && )readOk==1( ; i++(

readOk = fscanf)f,"%d %*c",arr+i(;

fclose)f(;return 1;

}

void main )({

int arr1[]={50,40,30,20,10};int arr2[10]={0};int i;if)saveArr)arr1,5,"1.txt"(==-1(

return;if)loadArr)arr2,4,"1.txt"(==-1(

return;/*Print the array that was loaded from the file onto

arr2:*/for )i=0; i<4 ; i++(

printf)"%d\n",*)arr2+i((;}

22 וווו 19וווו

רר ררררר ררררר רררר רררר רררר רררררר ררררר ר"ר רררררר רררררר רר רררררר רררררר ררררר. רררררר ררר רררר ררררר רררר רררר. ררררר, ררררר רר ררררר רר ררררר

ר- ררc)רררר רר ררררררר( רררר רררררר

14 ררררר ררררר רררר – ררררר C ררררר רררר

ווו וווו: ו"ו ווווו וווו

- וווו וווו ווווו וו ווווווווו וווווו וווווו וווווו17וווו

"(, וו וווו וו וווווו ווווווו וווו וווווmechonot.txt)ווווו "

ווווו: ו.ו, וו וווו ווו ווווו. וווו וווו וווווו ווווו "ווווו

וורררררר"(, ווו וו, וווו וו וווווו וווו c.txt" )ווווו "Cו-

ווווווו2ווווו, וווווו וווו וווווו וו וו' ו.ו. ווו , וווווו ו-

וווווו ווווו )ווווו ווו ווווווו ווווו וו וו וו' ווווו(.

וווווו ווווו 30%וווווווו וווווו ווווו וווווו וווווו70%

ווווו. ווו וווווו ווווווו ווווווו ווו ו.ו, ווווו )ווווו ווווו

ווווווו( ווווו )ווווו ווווו ווווווו(.

” וו ווווווו וווווו ווווו ווout.txtוו וווווו ווווו ווו ווווו “. ווו ווווו ווררררררוו ווווווווו וווווו וווווו וווו וו ווווו

וווו וווו וווו ווו, וווו וווווו ווווו וווווו ווווו )וווו וווווו ווווו וווווו(. ווו וו, וו וווו ווווו ווווו ווו וו' ו.ו.:

mechonot.txt c.txt out.txt

22 וווו 20וווו

1234 moshe cohen1345 dina levi

2345 robertredfort

4567 adinazahlawi

5678 fridrichnitche

1234 99 50 701235 24 33 802345 80 80 804567 90 95 1005555 12 44 34

….….….

moshe cohen 71.35robert redfort 80

adina zahlawi 97.75………

וו וווו וווווווווווו!!!

רר רררר רררררר רר ררררר ר-c!!!

14 ררררר ררררר רררר – ררררר C ררררר רררר

ווו וווו: ו"ו ווווו וווו#include <stdio.h>#include <string.h>void calcFinalGrades)char* deptFileName,char* courseFileName,char* outFileName({

FILE *depFile, *courseGradesFile, *outputFile;int idFromDept=0, idFromCourse=0, grades[3],ok1,ok2;char fname[256],lname[256];double avg;depFile = fopen)deptFileName,"r"(;courseGradesFile = fopen)courseFileName,"r"(;outputFile = fopen)outFileName,"w"(;if)!depFile || !courseGradesFile || !outputFile({

printf)"Error opening file!!! Exiting...."(;return;

}

do{

/*Write the current record to the output file: */if )idFromCourse==idFromDept && idFromCourse({

avg = 0.3*)))double()grades[0]+grades[1]((/2(+0.7*)double(grades[2];

fprintf)outputFile,"%s %s %f\n",fname,lname,avg(;idFromCourse = idFromDept = 0; /*We do this so that thenext two if- statements will read the next records fromboth the course and department files! */

}/*Read the next record from the course file: */if )!idFromCourse || idFromDept>idFromCourse(ok2 = fscanf)courseGradesFile,"%d %d %d %d",&idFromCourse,grades, grades+1,grades+2(;

/*Read the next record from the department file: */22 וווו 21וווו

:ווווו

:רררר ררררררררווווו וו ווו וווווו וווווו וווו ווו ווו ווו וווווו )ווווווו ווווווו( וווווו ווו וווווו' ו.ו. ווווו וווו. וו וווווווווו ווו ווווו וו וו' ו.ו.

ווווו וווווו וווו וווווו.ווווו וווווו וווו וו וווו ווווו ווווו וווו וווו וווווו

ווווו:וווו.1 ווווו ו.ו. וו'

14 ררררר ררררר רררר – ררררר C ררררר רררר

ווו וווו: ו"ו ווווו ווווif )!idFromDept || idFromCourse>idFromDept(

ok1 = fscanf)depFile,"%d %s %s",&idFromDept,fname,lname(;}while)ok1== 3 && ok2== 4(; fclose)depFile(;fclose)courseGradesFile(;fclose)outputFile(;

}void main )({

calcFinalGrades)"mechonot.txt","c.txt","out.txt"(;}

22 וווו 22וווו

!!!וווווו