Download presentation
Presentation is loading. Please wait.
Published byKristin Robbins Modified over 8 years ago
2
Dynamic Memory Allocation Linked Lists
4
void main() {{ ◦ FILE *file; ◦ char file_name[] = “file.txt”; ◦ if ((file = fopen(file_name, "w")) == NULL) printf("Cannot open file: %s\n", file_name); }} Number of simultaneously opened files is limited Where the opened file is?
5
File Opening ◦ FILE* fopen(char* file_name, char* mode) ◦ File name: “file.txt” ◦ mode “r” – open for reading “w” – open for writing “a” – open for appending ◦ Returns NULL if fails. File Closing ◦ fclose(FILE* pFile) What if file doesn’t exist? What if file exists?
6
fprintf(FILE* pf, char* format, …) ◦ Identical to printf but the first parameter fscanf(FILE* pf, char* format, …) ◦ Identical to scanf but the first parameter
7
#include void main(){ FILE *f1; f1 = fopen(“test1.txt","w"); if(!f1){ printf("Error opening file!\n"); return; } fprintf(f1,"hello world"); fclose(f1); }
8
#include void main(){ char ch; FILE *f1; f1 = fopen(“test2.txt","r"); if(!f1){ printf("Error opening file!\n"); return; } fscanf(f1,"%c",&ch); fclose(f1); }
9
A % in the printf/scanf string is replaced by the respective variable. %c – a character %d – an integer, %u – an unsigned integer. %f – a float %lf – a double %g – a nicer way to show a double (in printf) % - the ‘ % ’ character (in printf)
10
End Of File Marker EOF is a character which indicates the end of a file. It is returned by read commands of the getc and scanf families when they try to read beyond the end of a file. File path…
11
#include int main(int argc, char** argv) { int i = 0; if (!(f = fopen("D:\\Work\\Tech Docs\\Prog Lang\\C\\CTests\\Debug\\1.txt", "r"))) /* if (!(f = fopen("1.txt", "r"))) */ { perror("Failed to open file"); printf("Failed to open file: _errno %d, _doserrno %d\n", _errno, _doserrno); } do { ok = fscanf(f, "%d%d%d\n", &a,&b,&c); if (ok == 3) fprintf(stdout, "%d%d%d\n", a,b,c); } while( ok != EOF); }
12
%5c קולט 5 תווים %4d קולט עד 4 ספרות או עד סימן שאינו ספרה %6f קולט 6 תווים או עד תו שאינו חלק מ float %10s קולט 10 תווים או עד " תו לבן " מוסיף ‘\0’ %[A-Z] קולט תווים עד תו שאינו אות גדולה %[A-Za-z0-9] קולט עד תו שאינו אלפנומרי %[^?!] קולט עד שפוגש ? או ! %*4c דלג על 4 תוים ( לא נכנס לאף משתנה )
13
%4c מדפיס שלושה רווחים ואחריהם התו %-4c מדפיס את התו ואחריו שלושה רווחים %6d משלים ברווחים לפני המספר ל 6 תווים. אם המספר ארוך יותר מדפיס את המספר %-6d משלים ברווחים אחרי המספר %10f משלים ל 10 תווים. %.2f מדפיס רק 2 ספרות אחרי הנקודה ומעגל %10.2f 10 תווים שמתוכם 2 אחרי הנקודה
14
void main(){ int i=0,ok; FILE *f1; citizen neighborhood[100]; f1 = fopen("citizens.txt","r"); if(!f1){ printf("Error opening file!\n"); return; }
15
do{ ok=fscanf(f1,"%8d%20s%20s", & neighborhood [i].id, & neighborhood [i].name, & neighborhood [i].city); i++; } while (ok==3); fclose(f1); }
16
#include typedef struct citizen{ int id; char name[20]; char city[20]; }citizen; void main(){ int i; FILE *f1; citizen neighborhood[100]; f1 = fopen("citizens.txt","w"); if(!f1){ printf("Error opening file!\n"); return; }
17
for (i=0;i<100;i++){ printf("enter id, name and city\n"); scanf("%d%s%s", &neighborhood[i].id, &neighborhood[i].name, &neighborhood[i].city); } for (i=0; i<100; i++) fprintf(f1,"%8d%20s%20s", neighborhood[i].id, neighborhood[i].name, neighborhood[i].city); fclose(f1); }
18
link *loadList(FILE *f){ link *head, *temp; int ok, data; ok=fscanf(f,"%d%*c",&data); if (ok==1) head=createLink(data,NULL); else return NULL; temp=head; ok=fscanf(f,"%d%*c",&data); while (ok==1){ temp->next=createLink(data,NULL); temp=temp->next; ok=fscanf(f,"%d%*c",&data); } return head; }
19
void saveList(link *head, FILE *f){ while (head){ fprintf(f,"%d\n",head->data); head=head->next; }
20
fgets – gets fputs – puts c = fgetc(stdin) – c = getc() fputc(c, stdout) putchar(c) fgets and fputs retain the trailing newline character on the line they read or write, wheras gets and puts discard the newline
21
int main(int argc, char** argv) { int i = 0; for (i = 0; i < argc; i++) printf("Argument #%d is: %s\n", i, argv[i]); }
22
>type 1.txt|ctests.exe 6 7 8 ◦ Argument #0 is: ctests.exe ◦ Argument #1 is: 6 ◦ Argument #2 is: 7 ◦ Argument #3 is: 8
23
stdin stdout stderr
24
#include void main(){ printf("hello world"); fprintf(stdout,"hello world"); fprintf(stderr,"hello world"); }
25
fread(), fwrite(), fseek()
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.