Cs288 Intensive Programming in Linux

Slides:



Advertisements
Similar presentations
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Advertisements

What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Module R2 CS450. Next Week R1 is due next Friday ▫Bring manuals in a binder - make sure to have a cover page with group number, module, and date. You.
Functions Definition: Instruction block called by name Good design: Each function should perform one task and do it well Functions are the basic building.
. Plab – Tirgul 4 structs & arrays, file I/O, debugging memory errors.
COMP1170 Midterm Preparation (March 17 th 2009) Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education.
Testing a program Remove syntax and link errors: Look at compiler comments where errors occurred and check program around these lines Run time errors:
University of Calgary – CPSC 441. C PROGRAM  Collection of functions  One function “main()” is called by the operating system as the starting function.
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.
Functions, Pointers, Structures Keerthi Nelaturu.
Stack and Heap Memory Stack resident variables include:
File Handling Spring 2013Programming and Data Structure1.
CS 590 Programming Environments with UNIX. Computer Lab Account Course Homepage
1 Writing a Good Program 8. Elementary Data Structure.
File IO and command line input CSE 2451 Rong Shi.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
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:
(language, compilation and debugging) David 09/16/2011.
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:
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.
6/9/2016Course material created by D. Woit 1 CPS 393 Introduction to Unix and C START OF WEEK 10 (C-4)
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
Advanced Pointers and Structures Pointers in structures Memory allocation Linked Lists –Stacks and queues Trees –Binary tree example.
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.
Cs288 Intensive Programming in Linux
Cs288 Intensive Programming in Linux
LINKED LISTS.
Functions Students should understand the concept and basic mechanics of the function call/return pattern from CS 1114/2114, but some will not. A function.
Stack and Heap Memory Stack resident variables include:
Lesson #8 Structures Linked Lists Command Line Arguments.
Cs288 Intensive Programming in Linux
User-Written Functions
Function: Declaration
Linked List :: Basic Concepts
Chapter 6 CS 3370 – C++ Functions.
5.13 Recursion Recursive functions Functions that call themselves
Cs288 Intensive Programming in Linux
Sorting Tutorial Using C On Linux.
Cs288 Intensive Programming in Linux
ECE Application Programming
A bit of C programming Lecture 3 Uli Raich.
Data Structure Interview Question and Answers
Recitation 6: C Review 30 Sept 2016.
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.
UNIT-3 LINKED LIST.
Cs288 Intensive Programming in Linux
CSC215 Lecture Input and Output.
Plan for the Day: I/O (beyond scanf and printf)
Command-Line Arguments
C Basics.
Week 4 - Monday CS222.
Programmazione I a.a. 2017/2018.
Instructor: Ioannis A. Vetsikas
CS111 Computer Programming
Programmazione I a.a. 2017/2018.
LINKED LISTS.
Files I/O, Streams, I/O Redirection, Reading with fscanf
Topics Introduction to File Input and Output
CS 240 – Lecture 18 Command-line Arguments, Typedef, Union, Bit Fields, Pointers to Functions.
Stack and Queues Stack implementation using Array
CSC215 Lecture Input and Output.
Assembly Language Programming II: C Compiler Calling Sequences
Govt. Polytechnic,Dhangar
Programming and Data Structure
C What you Know* Objective: To introduce some of the features of C. This assumes that you are familiar with C++ or java and concentrates on the features.
File Handling.
Topics Introduction to File Input and Output
Functions Students should understand the concept and basic mechanics of the function call/return pattern from CS 1114/2114, but some will not. A function.
Intro to the Shell with Fork, Exec, Wait
Professor Jodi Neely-Ritz University of Florida
Presentation transcript:

Cs288 Intensive Programming in Linux Instructor: C F Yurkoski Christopher.f.yurkowski@njit.edu Section web site: https://web.njit.edu/~yurkowsk/cs288.html Class 9 27-10-16

Test revu Example using switch statements. Stacks More on linked lists Errnos getopts Binary trees (maybe next week).

Programming section Write a program which accepts one command line argument which is the name of the file to which it should write its output. (The output should be written to the beginning of the file each time, do not append to the file.) Your program should read a series of lines from standard input until an EOF is encountered. Each line of input consists of a command and an integer. The command is either ADD or DEL. If the command is ADD you should add the integer to a linked list sorted in numeric order. If the command is DEL you should delete that number from that list.

When the EOF is encountered, write the list to the file whose name is arg1, one integer per line. Ignore any duplicate adds. Ignore any deletes of items not in the list. For example, if the file input contains: ADD 10 ADD 8 ADD 11 DEL 8 DEL 9 ADD 9 And if your program is called myprog, After this is executed: cat input | myprog output output should contain: 9 10 11

1. a a

2. 234

Who invented C?

s

cfyfile=fopen(“/tmp/cfy”,”a”);

a z e t

24

A H G F E D

1

sample solution https://web.njit.edu/~yurkowsk/x/stack1.c

#include <stdlib.h> #include <stdio.h> #include <string.h> #define SIZE 20 main() { int *p, *q, fill; char cmd[10]; q=p=malloc(SIZE*sizeof(int)); if(p==NULL){ fprintf(stderr, "cannot allocate any more memory\n"); exit(-1); } 6-9-15

while(scanf("%s", cmd)!=EOF) if(!strcmp(cmd,"POP")){ fprintf(stderr,"%s\n", "POP"); p--; } else { scanf("%d",&fill); fprintf(stderr,"%s %d\n", p, fill); *p=fill; p++; while(q<p) printf("%d\n",*q++); 6-9-15

afsconnect1-56 x >: cat - | ./a.out 2>/dev/null PUSH 1 PUSH 2 POP PUSH 3 1 3 afsconnect1-57 x >:

Some issues Doesn’t check for overflow Doesn’t check for stack underflow Only handles frames of int, not a complex type 6-9-15 cfy

improved solution https://web.njit.edu/~yurkowsk/x/stack2.c

afsconnect1-67 x >: afsconnect1-65 x >: cat /tmp/input PUSH 1 PUSH a PUSH b PUSH c PUSH d PUSH e PUSH f PUSH 0 afsconnect1-66 x >: cat /tmp/input | ./a.out overflow afsconnect1-67 x >:

complex frame type https://web.njit.edu/~yurkowsk/x/stack4b.c

recall homework for next week should handle variable size frames.

hints for doing the variable frame size homework consider using a subroutine to print the frames. consider using a union to store items and pointer on the stack. have stack pointer point to cell containing previous stack pointer.

Singly linked lists 6-9-15 cfy

typedef struct list list; struct list { list *ptr; char word[26]; }; https://web.njit.edu/~yurkowsk/list.h Which contains: typedef struct list list; struct list { list *ptr; char word[26]; }; list *insert(list *lp, list *newp); list *append(list *lp, list *newp); list *delete(list *lp, list *newp); 6-9-15 cfy

It may be called multiple times. Assume the list is NULL terminated. Write an append() function with a signature as defined in the header file above. The append() function will get called from list.o and will be passed a pointer to a linked list of items and a new item to append to the list. It may be called multiple times. Assume the list is NULL terminated. Assume the ptr of the new item is NULL. The list may have no items in it. 6-9-15 cfy

To compile: cc -c append.c cc append.o list.o -o append 6-9-15 cfy

take 15 minutes to do this assignment

solution: #include <stdio.h> #include <stdlib.h> #include "list.h" list *append(list *lp, list *newp){ while(lp->ptr!=NULL) { lp=lp->ptr; } lp->ptr=newp; 6-9-15 cfy

#include <stdio.h> #include <stdlib.h> #include "list.h" main() { list head, *nextp, *lp, *lastp; head.ptr=NULL; lastp=&head; nextp=malloc(sizeof(list)); nextp->ptr=NULL; while((scanf("%s",nextp->word))>0) append(&head,nextp); } lp=&head; while(lp->ptr!=NULL){ lp=lp->ptr; printf("word %s\n",lp->word); 6-9-15

Some errnos from <errno.h> E2BIG Argument list too long. EACCES Permission denied. EADDRINUSE Address in use. EADDRNOTAVAIL Address not available. EAFNOSUPPORT Address family not supported. EBADF Bad file descriptor. EBADMSG Bad message. EBUSY Device or resource busy. 6-9-15 cfy

#include <errno.h> #include <stdio.h> #include <stdlib.h> main() { FILE *fp; fp=fopen("test","r"); printf("errno=%d\n",errno); switch(errno){ case 0: printf("no error\n"); break; case 2: printf("file does not exist\n"); case EACCES: printf("permission denied\n"); } 6-9-15

The item may occur once, multiple times or not at all. In class example #2 Using previous example as a basis, create a file containing a delete() function whose signature is as defined in the header file. It will be passed a pointer to a list and the list element whose word field contains the item to delete. New delete() function should delete all items from the list which match. The item may occur once, multiple times or not at all. Be sure to handle the case where the item to delete is the first or last item. Be sure to make sure the new list is terminated with a NULL pointer. Put your program in moodle as assignment 2. You can/should use the previous assignment to build a test list from which you can delete items. Take another 20 minutes to do this; you can do this in groups if you like.

sample output delete function

sample solution delete()

Doubly linked lists 6-9-15 cfy

Class 9 Homework #2. (add pointer to list.o) rewrite insert() using a doubly linked list and this header file: https://web.njit.edu/~yurkowsk/double.h afsconnect1-54 public_html >: cat double.h typedef struct list list; struct list { list *back; list *forward; char word[26]; }; list *insert(list *lp, list *newp); list *append(list *lp, list *newp); list *delete(list *lp, list *newp); 6-9-15

getopt man 3 getopt GETOPT(3) Linux Programmerâs Manual GETOPT(3) NAME getopt, getopt_long, getopt_long_only, optarg, optind, opterr, optopt - Parse command-line options SYNOPSIS #include <unistd.h> int getopt(int argc, char * const argv[], const char *optstring); extern char *optarg; extern int optind, opterr, optopt; #include <getopt.h> int getopt_long(int argc, char * const argv[], const char *optstring, const struct option *longopts, int *longindex); int getopt_long_only(int argc, char * const argv[],

while ((opt = getopt(argc, argv, "di:")) != -1) { switch (opt) { case 'd': debug = 1; break; case 'i': value = atoi(optarg); }

#include <stdlib.h> #include "list.h" #include <unistd.h> int debug=0; main(int argc, char *argv[]) { list head, *nextp, *lp, *lastp; int opt; while((opt=getopt(argc,argv,"d")) != -1){ switch(opt){ case 'd': debug=1; break; default: fprintf(stderr, "Usage: %s [-d]\n",argv[0]); exit(-1); } head.ptr=NULL; lastp=&head; nextp=malloc(sizeof(list)); 6-9-15

In class assignment #2 Write a program called myprog which uses getopts() and a switch statement a which accepts the following arguments: myprog [-d] [ -f output ] -i nn If the is no -i option or no value following it or any option not listed above it should print a usage statement. If there is a –d option if should set write trace statements to standard error. If is a -f option it should write the value of nn to the filename following the –f option. If there is no –f option it should write the value of nn to standard out. Solution should be ~50 lines of code.

Getopts solution https://web.njit.edu/~yurkowsk/x/myrog.c