Presentation is loading. Please wait.

Presentation is loading. Please wait.

CGS 3460 More On File IO. CGS 3460 What is a File n A file is a package of information with a name attached to it. n Files are used for various purposes:

Similar presentations


Presentation on theme: "CGS 3460 More On File IO. CGS 3460 What is a File n A file is a package of information with a name attached to it. n Files are used for various purposes:"— Presentation transcript:

1 CGS 3460 More On File IO

2 CGS 3460 What is a File n A file is a package of information with a name attached to it. n Files are used for various purposes: lFiles can record data, such as text or numbers. lSome files record ways to perform various processing procedures on data. These are referred to as programs or commands. n Conceptually, a file is a sequence of characters, which resides somewhere on a disk.

3 CGS 3460 Access Files n To access a file lOpen lRead / Write lClose Your c code A data file Read/write

4 CGS 3460 Methods n FILE* - structure for holding file n fopen n Read - getc n Write - putc n fclose

5 CGS 3460 More reading - fscanf n Read like scanf does, just from a file n Takes an additional argument which is the file to read from n fscanf(inFilePtr, “%f”, &fv); n Returns number of arguments read and assigned or EOF if end of file is reached before anything is assigned

6 CGS 3460 More writing - fprintf n Writes like printf does, just to a file n Takes an additional argument which is the file to print to n fprintf(outFilePtr, “This is how its done\n”);

7 CGS 3460 Reading lines- fgets n fgets(buffer, n, filePtr) lbuffer is where the line is stored ln is the max number of characters to be stored in buffer lfilePtr is where to read from lReads characters from file and stores them in buffer lStops when ‘\n’ is reached or when n-1 characters have been read lReturns NULL on failure and buffer on success

8 CGS 3460 Writing strings - fputs n fputs(buffer, filePtr) n Writes the contents of buffer to filePtr n Writes each character until the ‘\0’ is reached lDoes not write ‘\0’ to the file

9 CGS 3460 Constant Files n Three files are automatically opened in every c program n Identified by constant file pointers defined in lstdin: standard input (console input from user) scanf lstdout: output to the terminal printf lstderr: normally outputs to terminal but different file

10 CGS 3460 Constant Files (cont) n scanf(…) = fscanf(stdin, …) n printf(…) = fprintf(stdout, …) n getc(stdin) = getchar() n putc(stdout) = putchar() n fgets(buffer, n, stdin) ≈ gets(buffer) lfgets includes ‘\n’ at the end of buffer n fputs(buffer, stdout) ≈ puts(buffer) lfputs does not append ‘\n’ to end of buffer lputs appends new line to end of buffer

11 CGS 3460 Arrays of Structures

12 CGS 3460 Structures n Structures in C allows us to represent a collection of variables (possibly of different types) under a single name. n It allows us to create record style data with various fields. n More generally, it allows us to model real-world entities. n An entity is something that has a distinct, seperate existence. n An entity has a set of attributes that describes it. n Any object in the real-world can be modeled as an entity. n A book is an entity that can be distinguished from say a car.

13 CGS 3460 Structure – Declaring n A collection of values (members) – type declaration struct struct_name { type1 data_member1; type2 data_member2; …; typeN data_memberN; }; n Declare a structure variable – instance declaration lstruct struct_name instance_name;

14 CGS 3460 Declarations n Three ways struct date{ int day; char month[10]; int year; }; struct date today; typedef struct { int day; char month[10]; int year; } date; date today; struct { int day; char month[10]; int year; } today;

15 CGS 3460 Initialization struct { int day; char month[10]; int year; } today = {15, “ June ”, 2007}; typedef struct { int day; char month[10]; int year; } date; date today = {15, “June”, 2007}; struct date{ int day; char month[10]; int year; }; struct date today = {15, “June”, 2007};

16 CGS 3460 How to use n To access the members in the structure lspecify the variable name, followed by a period and the member name today.day = 15; today.year = 2007; today.month[0]=‘J’; today.month[1]=‘u’; today.month[2]=‘n’; today.month[3]=‘e’; today.month[4]=‘\0’; lOR today.day = 15; today.year = 2007; today.month=“June”; 15 ‘J’ ‘u’ ‘n’ ‘e’ ‘\0’ 2007.month.day.year today

17 CGS 3460 How to use structure – cont. n Structure variable can be passed as a parameter to a function. n Structure variable can be returned from a function n Can have arrays of structures, and structures containing arrays or other structures. lstruct date list_of_days[10]; lstruct journalEntry { int location; struct date when; char entry[1000]; };

18 CGS 3460 Arrays of Structures n Just like integer arrays and character arrays, we can also have an array of structures. n An integer array allows us to associate a list of numbers with a single variable name. n Similarly, an array of structures is a way of associating a a number of structures (entities) with a single variable name. lA class of students

19 CGS 3460 Define the structure first n Before we can create an array of book structures, we need to declare a structure to represent a book typedef struct { char title[50]; char author[30]; float price; }book; lUsually placed at beginning of program (after #include statements)

20 CGS 3460 Arrays of Structures n To represent a collection of books, we can create an array of book structures. n The declaration is identical to how we would declare an integer array. lFor example, if we want to represent a set of 10 numbers we would declare: int numbers[10]; lSimilarly, to declare an array that can hold 10 book structures: book library[10]; Here library is the variable that we use to refer to the entire array. Each element in the array is of type book. Notice that book is a structure type that we declared earlier using the typedef struct syntax.

21 CGS 3460 Accessing elements n Recall accessing elements in an array of ints lnumbers[0]  first number in the array lnumbers[7]  eighth number in the array lcan manipulate numbers[ i ] just like a regular int for a valid index i Access it, assign to it, multiply by it, add to it, etc n Same is true for an array of structs llibrary[0]  first book in the array llibrary[5]  sixth book in the array lThen you can just add. to the end and whatever field you want to access llibray[2].author gets the author of the third book in the array

22 CGS 3460 Accessing elements (cnt) n Individual elements in the array can be accessed using the subscript operator []. n Consider the code snippet that initializes the first book element, located at index position 0 in the array: lstrcpy (b[0].title, "Harry Potter and the Sorcerer's Stone"); lstrcpy (b[0].author, "J.K.Rowling"); lb[0].price = 14.99; n Similary, we can initalize the remaining book elements: b[1], b[2], b[3] and so on till the last book element at b[9].

23 CGS 3460 Iterating over the array n Assume we have initialized all the book elements for the array b. Our goal is to now print the details of each book element in the array. n This can be done by using a loop: int i = 0; while (i < 10) { // display book information printf ("Title: %s \n", b[i].title); printf ("Author: %s \n", b[i].author); printf ("Price: %f \n", b[i].price); }

24 CGS 3460 Passing Structures to a Function n A structure variable or structure array has similar properties like an integer variable or integer array. n A function that receives an integer array along with the number of elements in the array will be declared as: lvoid function (int numbers[], int sz); n Similarly, a function that receives a book array along with the number of elements in the array can be declared as: l void function (book library[], int sz);


Download ppt "CGS 3460 More On File IO. CGS 3460 What is a File n A file is a package of information with a name attached to it. n Files are used for various purposes:"

Similar presentations


Ads by Google