Lab # 4. Files & Queues in C

Similar documents
File IO and command line input CSE 2451

File Access. FILE * fopen(const char *name, const char * mode);

Standard File Pointers

25.2 Opening and Closing a File

Programming in C. Session 8. Seema Sirpal Delhi University Computer Centre

This code has a bug that allows a hacker to take control of its execution and run evilfunc().

Content. Input Output Devices File access Function of File I/O Redirection Command-line arguments

Mode Meaning r Opens the file for reading. If the file doesn't exist, fopen() returns NULL.

File I/O. Arash Rafiey. November 7, 2017

PROGRAMMAZIONE I A.A. 2017/2018

C-Refresher: Session 10 Disk IO

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

ECE551 Midterm. There are 7 questions, with the point values as shown below. You have 75 minutes with a total of 75 points. Pace yourself accordingly.

Student Number: Instructor: Reid Section: L5101 (6:10-7:00pm)

CSCI 171 Chapter Outlines

File (1A) Young Won Lim 11/25/16

structs as arguments

Student Number: Instructor: Reid Section: L0101 (10:10-11:00am)

C for Engineers and Scientists: An Interpretive Approach. Chapter 14: File Processing

Intermediate Programming, Spring 2017*

HIGH LEVEL FILE PROCESSING

EL2310 Scientific Programming

CMPE-013/L. File I/O. File Processing. Gabriel Hugh Elkaim Winter File Processing. Files and Streams. Outline.

System Software Experiment 1 Lecture 7

Introduction to file management

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 12

CSci 4061 Introduction to Operating Systems. Input/Output: High-level

Section 3: File I/O, JSON, Generics. Meghan Cowan

Engineering program development 7. Edited by Péter Vass

CSI 402 Lecture 2 Working with Files (Text and Binary)

CS 261 Fall Mike Lam, Professor. Structs and I/O

Stream Model of I/O. Basic I/O in C

Accessing Files in C. Professor Hugh C. Lauer CS-2303, System Programming Concepts

Getting Started. Project 1

C Input/Output. Before we discuss I/O in C, let's review how C++ I/O works. int i; double x;

Lecture 9: File Processing. Quazi Rahman

UNIT-V CONSOLE I/O. This section examines in detail the console I/O functions.

CP2 Revision. theme: file access and unix programs

Input/Output and the Operating Systems

ECE 264 Exam 2. 6:30-7:30PM, March 9, You must sign here. Otherwise you will receive a 1-point penalty.

Advanced C Programming and Introduction to Data Structures

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

LSN 3 C Concepts for OS Programming

Data Representation and Storage

A Crash Course in C. Steven Reeves

Input / Output Functions

CSE 124 Discussion (10/3) C/C++ Basics

Lab # 6. Data Manipulation Language (DML)

CSI 402 Systems Programming LECTURE 4 FILES AND FILE OPERATIONS

Quick review of previous lecture Ch6 Structure Ch7 I/O. EECS2031 Software Tools. C - Structures, Unions, Enums & Typedef (K&R Ch.

ECE551 Midterm Version 1

Help Session 2. Programming Assignment 2

Files. Programs and data are stored on disk in structures called files Examples. a.out binary file lab1.c - text file term-paper.

Student Number: Instructor: Reid Section: L0201 (12:10-1:00pm)

Computer Programming: C++

CMPT 102 Introduction to Scientific Computer Programming. Input and Output. Your first program

Introduction to C Recursion, sorting algorithms, files

Programming Fundamentals

CSCI-243 Exam 2 Review February 22, 2015 Presented by the RIT Computer Science Community

High Performance Programming Programming in C part 1

Computer Programming Unit v

Lab # 9. Java to Database Connection

Lab # 1. Introduction to Oracle

File Handling. Reference:

Communication. Serial port programming

ECE551 Midterm Version 2

Input/Output: Advanced Concepts

CSE 333 SECTION 3. POSIX I/O Functions

ECE551 Midterm Version 1

Operating System Labs. Yuanbin Wu

C Programming Language: C ADTs, 2d Dynamic Allocation. Math 230 Assembly Language Programming (Computer Organization) Thursday Jan 31, 2008

Fundamentals of Programming. Lecture 10 Hamed Rasifard

C Programming 1. File Access. Goutam Biswas. Lect 29

EL2310 Scientific Programming

Signature: ECE 551 Midterm Exam

Week 8: Arrays and File I/O. BJ Furman 21OCT2009

Lecture 7: Files. opening/closing files reading/writing strings reading/writing numbers (conversion to ASCII) command line arguments

Fundamentals of Programming & Procedural Programming

CSE2421 Systems1 Introduction to Low-Level Programming and Computer Organization

CS240: Programming in C

MPATE-GE 2618: C Programming for Music Technology. Syllabus

2009 S2 COMP File Operations

C programming basics T3-1 -

DATA STRUCTURES USING C

CS1003: Intro to CS, Summer 2008

DS: CS Computer Sc & Engg: IIT Kharagpur 1. File Access. Goutam Biswas. ect 29

DataBase Lab JAVA-DATABASE CONNECTION. Eng. Haneen El-masry

C Syntax Arrays and Loops Math Strings Structures Pointers File I/O. Final Review CS Prof. Jonathan Ventura. Prof. Jonathan Ventura Final Review

Topic 8: I/O. Reading: Chapter 7 in Kernighan & Ritchie more details in Appendix B (optional) even more details in GNU C Library manual (optional)

CMPSC 311- Introduction to Systems Programming Module: Input/Output

UNIX input and output

File I/O Lesson Outline

Files and Streams Opening and Closing a File Reading/Writing Text Reading/Writing Raw Data Random Access Files. C File Processing CS 2060

CS113: Lecture 7. Topics: The C Preprocessor. I/O, Streams, Files

Contents. A Review of C language. Visual C Visual C++ 6.0

Recitation 2/18/2012

ECE264 Spring 2014 Exam 2, March 11, 2014

Tin học cơ sở 4$ Structures!

C Basics And Concepts Input And Output

SAE1A Programming in C. Unit : I - V

Transcription:

Islamic University of Gaza Faculty of Engineering Department of Computer Engineering ECOM 4010: Lab # 4 Files & Queues in C Eng. Haneen El-Masry October, 2013

2 FILE * Files in C For C File I/O you need to use a FILE pointer, which will let the program keep track of the file being accessed. fopen To open a file you need to use the fopen function, which returns a FILE pointer. Once you've opened a file, you can use the FILE pointer to let the compiler perform input and output functions on the file. fopen Modes FILE *fopen(const char *filename, const char *mode); The allowed modes for fopen are as follows: r: open for reading. w: open for writing (file need not exist). a: open for appending (file need not exist). r+: open for reading and writing, start at beginning. w+: open for reading and writing (overwrite file). a+: open for reading and writing (append if file exists). Note: It's possible for fopen to fail even if your program is perfectly correct: you might try to open a file specified by the user, and that file might not exist (or it might be writeprotected). In those cases, fopen will return NULL pointer. fclose When you're done working with a file, you should close it using the function. int fclose(file *a_file);

3 fclose returns zero if the file is closed successfully. Reading From File fscanf: Reads formatted input from a stream. int fscanf(file *stream, const char *format,...) getline: It is the recommended way to read lines from a stream. ssize_t getline (char **lineptr, size_t *n, FILE *stream) This function reads an entire line from stream, storing the text (including the newline and a terminating null character) in a buffer and storing the buffer address in *lineptr. Before calling getline, you should place in *lineptr the address of a buffer *n bytes long, allocated with malloc.

4 If you set *lineptr to a NULL pointer, and *n to zero, before the call, then getline allocates the initial buffer for you by calling malloc. When getline is successful, it returns the number of characters read (including the newline, but not including the terminating null). If an error occurs or end of file is reached without any bytes read, getline returns -1.

5 Note: If you need to convert String to integer, you can use atoi function. Writing to a File int num = atoi(s); fprintf: Sends formatted output to a stream. int fprintf(file *stream, const char *format,...)

6 Structures and typedef Structures In the C language structures are used to group together different types of variables under the same name. For example you could create a structure telephone : which is made up of a string (that is used to hold the name of the person) and an integer (that is used to hold the telephone number). Declaration With the declaration of the structure you have created a new type, called telephone. Access struct telephone { char *name; int number; }; To access the members of the structure telephone, you must use a dot between the structure name and the variable name.

7 #include<stdio.h> struct telephone{ char *name; int number; }; int main(){ struct telephone index; index.name = "Haneen"; index.number = 12345; printf("name: %s\n", index.name); printf("telephone number: %d\n",index.number); return 0; } Type definitions and Structures Type definitions make it possible to create your own variable types. e.g., To define a new data type as a pointer to an integer. typedef int *int_ptr; It is also possible to use type definitions with structures, to save us from always having to type 'struct SomeStruct'. #include<stdio.h> typedef struct telephone{ char *name; int number; }TELEPHONE; int main(){ TELEPHONE index; index.name = "Haneen"; index.number = 12345; printf("name: %s\n", index.name); printf("telephone number: %d\n",index.number); return 0; }

8 Pointer to Structures If you want a pointer to a structure you have to use the -> (infix operator) instead of a dot. Queues implementation using Singly Linked List in C

9

10

11 Best Wishes