CS 241 Section Week #5 9/22/11. 2 Topics This Section File I/O Advanced C.

Slides:



Advertisements
Similar presentations
I/O means Input and Output. One way: use standard input and standard output. To read in data, use scanf() (or a few other functions) To write out data,
Advertisements

File and I/O system calls int open(const char* path, int flags, mode_t modes) int creat(const char *path, mode_t mode) ssize_t read(int fd, void *buf,
Threads CS241 Discussion Section Week 3 2/09/09 – 2/13/09.
January 13, Files – Chapter 2 Basic File Processing Operations.
CS 241 Section Week #5 2/23/12. 2 Topics This Section MP4 overview Function Pointers Pthreads File I/O.
Text File Input and Output. Overview Text File Buffered I/O Functions fopen Function Demo of File Write and Read.
Folk/Zoellick/Riccardi, File Structures 1 Objectives: To get familiar with Logical files vs. physical files File processing operations File processing.
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.
Today’s topic: –File operations –I/O redirection –Inter-process communication through pipes.
1 System Calls & Stdio. 2 Two processes open the same file Both keep writing to it What happens?
1 Advanced programming in UNIX 1 File I/O Hua LiSystems ProgrammingCS2690File I/O.
CS 311 – Lecture 10 Outline Review open() and close() Difference between fopen() and open() File management system calls – read() – write() – lseek() –
1 System Calls and Standard I/O Professor Jennifer Rexford
CSE 451 Section 4 Project 2 Design Considerations.
POSIX: Files Introduction to Operating Systems: Discussion 1 Read Solaris System Interface Guide: Ch. 5.1 Basic File I/O.
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.
CP104 Introduction to Programming File I/O Lecture 33 __ 1 File Input/Output Text file and binary files File Input/output File input / output functions.
Recitation 9: Error Handling, I/O, Man Andrew Faulring Section A 4 November 2002.
Operating Systems Recitation 1, March th, 2002.
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.
File Handling In C By - AJAY SHARMA. We will learn about- FFile/Stream TText vs Binary Files FFILE Structure DDisk I/O function OOperations.
1 Lecture09: File I/O 5/6/2013 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Introduction to Programming Using C Files. 2 Contents Files Working with files Sequential files Records.
UNIX Files File organization and a few primitives.
File IO and command line input CSE 2451 Rong Shi.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
File I/O, Project 1: List ADT Bryce Boe 2013/07/02 CS24, Summer 2013 C.
Week 12 - Wednesday.  What did we talk about last time?  File I/O  Binary trees  Lab 11.
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()
FILE IO in ‘C’ by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
24-2 Perform File I/O using file pointers FILE * data-type Opening and closing files Character Input and Output String Input and Output Related Chapter:
CSCI 330 UNIX and Network Programming Unit VII: I/O Management I.
C Programming Lecture 12 : File Processing
Recitation 9: Error Handling, I/O, Man Anubhav Gupta Section D.
January 7, 2003Serguei Mokhov, 1 File I/O System Calls Reference COMP 229, 444, 5201 Revision 1.2 Date: July 21, 2004.
CS 241 Section Week #8 (10/29/09). Outline MP5 Overview Files & I/O UNIX File Systems inodes Directories Links.
File I/O open close lseek read and write – unbuffered I/O dup and dup2.
OS interface: file and I/O system calls File operations in C/C++? –fopen(), fread(), fwrite(), fclose(), fseek() in C f.open(…), f.close(…) in C++ I/O.
Files A collection of related data treated as a unit. Two types Text
FILES IN C. File Operations  Creation of a new file  Opening an existing file  Reading from a file  Writing to a file  Moving to a specific location.
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.
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
Advanced Programming in the UNIX Environment Hop Lee.
Using System Calls (Unix) Have to tell compiler (if C/C++) where to find the headers, etc. – i.e., the “include” files May have to tell compiler where.
Real Numbers Device driver process within the operating system that interacts with I/O controller logical record 1 logical record 2 logical record 3.
Files in UNIX u UNIX deals with two different classes of files:  Special Files  Regular Files u Regular files are just ordinary data files on disk -
Error handling I/O Man pages
Sorting Tutorial Using C On Linux.
Chapter 4 File Processing
Lecture 11 File input/output
MP1 and MP2 - Threads CS241 Discussion Section Week 3
File I/O.
CGS 3460, Lecture 41 Apr 21, 2006 Hen-I Yang
Plan for the Day: I/O (beyond scanf and printf)
File Input/Output.
What you need for the 1st phase of project
C Programming Lecture-15 File I/O
Beginning C Lecture 11 Lecturer: Dr. Zhao Qinpei
CSE 333 – Section 3 POSIX I/O Functions.
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
FILE HANDLING IN C.
Text and Binary File Processing
File Input and Output.
CSE 333 – Section 3 POSIX I/O Functions.
CSE 333 – Section 3 POSIX I/O Functions.
Module 12 Input and Output
Standard I/O Library Implementation
CSc 352 File I/O Saumya Debray Dept. of Computer Science
Presentation transcript:

CS 241 Section Week #5 9/22/11

2 Topics This Section File I/O Advanced C

POP QUIZ!

POP QUIZ! (not really) What is type definition for this function: – int *foo(const int* input, char *msg) T/F When a thread is finished it should call exit. What is the one exception to grading where you can leak memory? (Case and amount) T/F Robin was wearing green in lecture yesterday.

POP QUIZ! (not really) What is type definition for this function: – int *foo(const int* input, char *msg) int *(*)(const int*, char*)

POP QUIZ! (not really) What is type definition for this function: – int *foo(const int* input, char *msg) T/F When a thread is finished it should call exit. False. It should call pthread_exit or simply return.

POP QUIZ! (not really) What is type definition for this function: – int *foo(const int* input, char *msg) T/F When a thread is finished it should call exit. What is the one exception to grading where you can leak memory? (Case and amount) If you call pthread_exit(); 5 blocks.

POP QUIZ! (not really) What is type definition for this function: – int *foo(const int* input, char *msg) T/F When a thread is finished it should call exit. What is the one exception to grading where you can leak memory? (Case and amount) T/F Robin was wearing green in lecture yesterday. If you missed this you haven’t been keeping up on lectures

File Input/Output

File I/O in C MP2 requires you to read and write text files in C. Two primary means of doing I/O in C: Through lightly-wrapped system calls open(), close(), read(), write(), etc Through C-language standards fopen(), fclose(), fread(), fwrite(), etc

File I/O in C Opening a file (Method #1): fopen(const char *filename, const char *mode); filename: path to file to open mode: what do you wish to do with the file? r : read only r+ : read and write (file must already exist) w : write (or overwrite) a file w+ : write (or overwrite) a file and allow for reading a : append to the end of the file (works for new files, too) a+ : appends to end of file and allows for reading anywhere in the file; however, writing will always occur as an append

File I/O in C Opening a file (Method #2): open(const char *filename, int flags, int mode); filename: path to file to open flags: what do you wish to do with the file? One of the following is required: O_RDONLY, O_WRONLY, O_RDWR And any number of these flags ( yo “add” these flags, simply binary-OR them together ) : O_APPEND: Similar to “a+” in fopen() O_CREAT: Allows creation of a file if it doesn’t exist O_SYNC: Allows for synchronous I/O (thread-safeness) mode: what permissions should the new file have? (S_IRUSR | S_IWUSR) creates a user read-write file.

Opening Files in C Return value of opening a file: Having called open() or fopen(), they will both create an entry in the OS’s file descriptor table. Specifics of a file descriptor table will be covered in-depth in the second-half of CS 241. Both open() and fopen() returns information about its file descriptor: open(): Returns an int. fopen(): Returns a (FILE *).

Reading Files in C Two ways to read files in C: fread(void *ptr, size_t size, size_t count, FILE *s); *ptr : Where should the data be read into? size : What is the size of each piece of data? count : How many pieces? *s : What (FILE *) do we read from? read(int fd, void *buf, size_t count); fd: What file do we read from? *buf: Where should the data be read into? count: How many bytes should be read?

Reading Files in C Reading more advancely… fscanf(FILE *stream, const char *format, …); Allows for reading at a semantic-level (eg: ints, doubles, etc) rather than a byte-level.The format string ( *format ) is of the same format as printf(). fgets(char *s, int size, FILE *stream); reads in at most size -1 characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A '\0’ is stored after the last character in the buffer.

Writing Files in C Writing is a lot like reading… fwrite(void *ptr, size_t size, size_t count, FILE *s); Writing of bytes with ( FILE * ). write(int fd, void *buf, size_t count); Writing of bytes with a file descriptor ( int ) fprintf(FILE *stream, const char *format, …); Formatted writing to files (works like printf() )

Closing Files in C Always close your files! fclose(FILE *stream); close(int fd); write(), and especially fwrite()/fprintf(), may be buffered before being written out to disk. If a file is never closed after writing: the new data may never be written on the actual file; the files may be corrupted.

Advanced C

19 Playing with Structs How do we reduce the size of the struct? typedef struct _name_t{ int size; int bool; } name_t;

20 Playing with Structs How do we initialize the struct in one line? typedef struct _name_t{ int size:31; //31 bits int bool:1; //1 bit } name_t;

21 Playing with Structs How do we initialize only bool? typedef struct _name_t{ int size:31; //31 bits int bool:1; //1 bit } name_t; name_t var = {0, 1}; //size = 0, bool = 1

22 Playing with Structs How do we initialize only bool? typedef struct _name_t{ int size:31; //31 bits int bool:1; //1 bit } name_t; name_t var = {.bool=1};