 Dynamic Memory Allocation  Linked Lists  void main() {{ ◦ FILE *file; ◦ char file_name[] = “file.txt”; ◦ if ((file = fopen(file_name, "w")) ==

Slides:



Advertisements
Similar presentations
CS115_ SENEM KUMOVA METIN 1 FILE OPERATIONS.
Advertisements

File Management in C. Console oriented Input/Output Console oriented – use terminal (keyboard/screen) scanf(“%d”,&i) – read data from keyboard printf(“%d”,i)
CSCI 171 Presentation 12 Files. Working with files File Streams – sequence of data that is connected with a specific file –Text Stream – Made up of lines.
Command Line arguments Main function: int main(argc, char *argv[]) –argc is the count of command line arguments –argv is an array of strings argv[0] is.
ISP - 2 nd Recitation Functions Pointers Structs Files Code Examples Homework!
File I/O.
An Introduction to C Programming (assuming that you already know Java; this is not an introduction to C++)
Files Programs and data are stored on disk in structures called files Examples Turbo C++ - binary file Word binary file lab1.c - text file lab1.data.
1 Homework Introduction to HW7 –Complexity similar to HW6 –Don’t wait until last minute to start on it File Access will be needed in HW8.
Lone Leth Thomsen Input / Output and Files. April 2006Basis-C-8/LL2 sprintf() and sscanf() The functions sprintf() and sscanf() are string versions of.
22. FILE INPUT/OUTPUT. File Pointers and Streams Declarations of functions that perform file I/O appear in. Each function requires a file pointer as a.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
1 Lecture09: File I/O 5/6/2013 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Characters and Strings File Processing Exercise C Programming:Part 3.
File IO and command line input CSE 2451 Rong Shi.
1 IPC144 Session 18 The C Programming Language. 2 Objectives List the file open and close functions, with their parameters and return values List the.
File I/O, Project 1: List ADT Bryce Boe 2013/07/02 CS24, Summer 2013 C.
ECE 103 Engineering Programming Chapter 44 File I/O Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material developed.
1 File Handling. 2 Storage seen so far All variables stored in memory Problem: the contents of memory are wiped out when the computer is powered off Example:
1 Lecture09: File I/O 11/19/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Memory Layout, File I/O Bryce Boe 2013/06/27 CS24, Summer 2013 C.
Chapter 11: Data Files and File Processing Files and streams Creating a sequential access file Reading data from a sequential access file Using fgetc()
Using Files Declare a variable, called file pointer, of type FILE * Use function fopen to open a named file and associate this file with the file pointer.
GAME203 – C Files stdio.h C standard Input/Output “getchar()”
Gramming An Introduction to C Programming (assuming that you already know Java; this is not an introduction to C++)
Files A collection of related data treated as a unit. Two types Text
Files. FILE * u In C, we use a FILE * data type to access files. u FILE * is defined in /usr/include/stdio.h u An example: #include int main() { FILE.
Connecting to Files In order to read or write to a file, we need to make a connection to it. There are several functions for doing this. fopen() – makes.
6/9/2016Course material created by D. Woit 1 CPS 393 Introduction to Unix and C START OF WEEK 10 (C-4)
 Last Lecture Reminder  Array Sorts  Exam Questions Examples  Open Questions ◦ Search Engines ◦ Operating Systems ◦ Multiple Files in Projects  Additional.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
Real Numbers Device driver process within the operating system that interacts with I/O controller logical record 1 logical record 2 logical record 3.
An Introduction to C Programming (assuming that you already know Java; this is not an introduction to C++)
Chapter 4 File Processing
Chapter 9 – File Input Output
Characters and Strings
Chapter 22 – part a Stream refer to any source of input or any destination for output. Many small programs, obtain all their input from one stream usually.
Lecture 11 File input/output
Command Line Arguments
An Introduction to C Programming
File I/O.
Session #5 File I/O Bit Masks, Fields & Bit Manipulations
CSC215 Lecture Input and Output.
Plan for the Day: I/O (beyond scanf and printf)
Computer Science 210 Computer Organization
CS111 Computer Programming
Programming in C Input / Output.
C: Primer and Advanced Topics
File Management in C.
Computer Science 210 Computer Organization
קבצים קרן כליף.
File I/O We are used to reading from and writing to the terminal:
רשימות מקושרות עבודה עם קבצים דוגמה
Lecture 15 Files.
CSC215 Lecture Input and Output.
CSE1320 Strings Dr. Sajib Datta
CSC215 Lecture Input and Output.
File Input and Output.
File Handling.
Programming in C Input / Output.
File Handling in C.
Chapter 5 File Handling in C
Module 12 Input and Output
File I/O & UNIX System Interface
Characters and Strings
CSc 352 File I/O Saumya Debray Dept. of Computer Science
File I/O.
I/O CS580U - Fall 2018.
File I/O We are used to reading from and writing to the terminal:
Files Chapter 8.
Presentation transcript:

 Dynamic Memory Allocation  Linked Lists

 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?

 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?

 fprintf(FILE* pf, char* format, …) ◦ Identical to printf but the first parameter  fscanf(FILE* pf, char* format, …) ◦ Identical to scanf but the first parameter

#include void main(){ FILE *f1; f1 = fopen(“test1.txt","w"); if(!f1){ printf("Error opening file!\n"); return; } fprintf(f1,"hello world"); fclose(f1); }

#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); }

 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)

 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…

#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); }

 %5c קולט 5 תווים  %4d קולט עד 4 ספרות או עד סימן שאינו ספרה  %6f קולט 6 תווים או עד תו שאינו חלק מ float  %10s קולט 10 תווים או עד " תו לבן " מוסיף ‘\0’  %[A-Z] קולט תווים עד תו שאינו אות גדולה  %[A-Za-z0-9] קולט עד תו שאינו אלפנומרי  %[^?!] קולט עד שפוגש ? או !  %*4c דלג על 4 תוים ( לא נכנס לאף משתנה )

 %4c מדפיס שלושה רווחים ואחריהם התו  %-4c מדפיס את התו ואחריו שלושה רווחים  %6d משלים ברווחים לפני המספר ל 6 תווים. אם המספר ארוך יותר מדפיס את המספר  %-6d משלים ברווחים אחרי המספר  %10f משלים ל 10 תווים.  %.2f מדפיס רק 2 ספרות אחרי הנקודה ומעגל  %10.2f 10 תווים שמתוכם 2 אחרי הנקודה

void main(){ int i=0,ok; FILE *f1; citizen neighborhood[100]; f1 = fopen("citizens.txt","r"); if(!f1){ printf("Error opening file!\n"); return; }

do{ ok=fscanf(f1,"%8d%20s%20s", & neighborhood [i].id, & neighborhood [i].name, & neighborhood [i].city); i++; } while (ok==3); fclose(f1); }

#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; }

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); }

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; }

void saveList(link *head, FILE *f){ while (head){ fprintf(f,"%d\n",head->data); head=head->next; }

 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

int main(int argc, char** argv) { int i = 0; for (i = 0; i < argc; i++) printf("Argument #%d is: %s\n", i, argv[i]); }

 >type 1.txt|ctests.exe ◦ Argument #0 is: ctests.exe ◦ Argument #1 is: 6 ◦ Argument #2 is: 7 ◦ Argument #3 is: 8

 stdin  stdout  stderr

#include void main(){ printf("hello world"); fprintf(stdout,"hello world"); fprintf(stderr,"hello world"); }

 fread(), fwrite(), fseek()